Subiendo proyecto completo sin restricciones de git ignore

This commit is contained in:
Jose Sanchez
2023-08-17 11:44:02 -04:00
parent a0d4f5ba3b
commit 20f1c60600
19921 changed files with 2509159 additions and 45 deletions

View File

@@ -0,0 +1,145 @@
<?php
declare(strict_types=1);
namespace Faker\Container;
use Faker\Extension\Extension;
/**
* A simple implementation of a container.
*
* @experimental This class is experimental and does not fall under our BC promise
*/
final class Container implements ContainerInterface
{
/**
* @var array<string, callable|object|string>
*/
private $definitions;
private $services = [];
/**
* Create a container object with a set of definitions. The array value MUST
* produce an object that implements Extension.
*
* @param array<string, callable|object|string> $definitions
*/
public function __construct(array $definitions)
{
$this->definitions = $definitions;
}
/**
* Retrieve a definition from the container.
*
* @param string $id
*
* @throws \InvalidArgumentException
* @throws \RuntimeException
* @throws ContainerException
* @throws NotInContainerException
*/
public function get($id): Extension
{
if (!is_string($id)) {
throw new \InvalidArgumentException(sprintf(
'First argument of %s::get() must be string',
self::class,
));
}
if (array_key_exists($id, $this->services)) {
return $this->services[$id];
}
if (!$this->has($id)) {
throw new NotInContainerException(sprintf(
'There is not service with id "%s" in the container.',
$id,
));
}
$definition = $this->definitions[$id];
$service = $this->services[$id] = $this->getService($id, $definition);
if (!$service instanceof Extension) {
throw new \RuntimeException(sprintf(
'Service resolved for identifier "%s" does not implement the %s" interface.',
$id,
Extension::class,
));
}
return $service;
}
/**
* Get the service from a definition.
*
* @param callable|object|string $definition
*/
private function getService($id, $definition)
{
if (is_callable($definition)) {
try {
return $definition();
} catch (\Throwable $e) {
throw new ContainerException(
sprintf('Error while invoking callable for "%s"', $id),
0,
$e,
);
}
} elseif (is_object($definition)) {
return $definition;
} elseif (is_string($definition)) {
if (class_exists($definition)) {
try {
return new $definition();
} catch (\Throwable $e) {
throw new ContainerException(sprintf('Could not instantiate class "%s"', $id), 0, $e);
}
}
throw new ContainerException(sprintf(
'Could not instantiate class "%s". Class was not found.',
$id,
));
} else {
throw new ContainerException(sprintf(
'Invalid type for definition with id "%s"',
$id,
));
}
}
/**
* Check if the container contains a given identifier.
*
* @param string $id
*
* @throws \InvalidArgumentException
*/
public function has($id): bool
{
if (!is_string($id)) {
throw new \InvalidArgumentException(sprintf(
'First argument of %s::get() must be string',
self::class,
));
}
return array_key_exists($id, $this->definitions);
}
/**
* Get the bindings between Extension interfaces and implementations.
*/
public function getDefinitions(): array
{
return $this->definitions;
}
}

View File

@@ -0,0 +1,92 @@
<?php
declare(strict_types=1);
namespace Faker\Container;
use Faker\Core;
use Faker\Extension\BarcodeExtension;
use Faker\Extension\BloodExtension;
use Faker\Extension\ColorExtension;
use Faker\Extension\DateTimeExtension;
use Faker\Extension\FileExtension;
use Faker\Extension\NumberExtension;
use Faker\Extension\UuidExtension;
use Faker\Extension\VersionExtension;
/**
* @experimental This class is experimental and does not fall under our BC promise
*/
final class ContainerBuilder
{
/**
* @var array<string, callable|object|string>
*/
private $definitions = [];
/**
* @param callable|object|string $value
*
* @throws \InvalidArgumentException
*/
public function add($value, string $name = null): self
{
if (!is_string($value) && !is_callable($value) && !is_object($value)) {
throw new \InvalidArgumentException(sprintf(
'First argument to "%s::add()" must be a string, callable or object.',
self::class,
));
}
if ($name === null) {
if (is_string($value)) {
$name = $value;
} elseif (is_object($value)) {
$name = get_class($value);
} else {
throw new \InvalidArgumentException(sprintf(
'Second argument to "%s::add()" is required not passing a string or object as first argument',
self::class,
));
}
}
$this->definitions[$name] = $value;
return $this;
}
public function build(): ContainerInterface
{
return new Container($this->definitions);
}
/**
* Get an array with extension that represent the default English
* functionality.
*/
public static function defaultExtensions(): array
{
return [
BarcodeExtension::class => Core\Barcode::class,
BloodExtension::class => Core\Blood::class,
ColorExtension::class => Core\Color::class,
DateTimeExtension::class => Core\DateTime::class,
FileExtension::class => Core\File::class,
NumberExtension::class => Core\Number::class,
VersionExtension::class => Core\Version::class,
UuidExtension::class => Core\Uuid::class,
];
}
public static function getDefault(): ContainerInterface
{
$instance = new self();
foreach (self::defaultExtensions() as $id => $definition) {
$instance->add($definition, $id);
}
return $instance->build();
}
}

View File

@@ -0,0 +1,14 @@
<?php
declare(strict_types=1);
namespace Faker\Container;
use Psr\Container\ContainerExceptionInterface;
/**
* @experimental This class is experimental and does not fall under our BC promise
*/
final class ContainerException extends \RuntimeException implements ContainerExceptionInterface
{
}

View File

@@ -0,0 +1,13 @@
<?php
namespace Faker\Container;
use Psr\Container\ContainerInterface as BaseContainerInterface;
interface ContainerInterface extends BaseContainerInterface
{
/**
* Get the bindings between Extension interfaces and implementations.
*/
public function getDefinitions(): array;
}

View File

@@ -0,0 +1,14 @@
<?php
declare(strict_types=1);
namespace Faker\Container;
use Psr\Container\NotFoundExceptionInterface;
/**
* @experimental This class is experimental and does not fall under our BC promise
*/
final class NotInContainerException extends \RuntimeException implements NotFoundExceptionInterface
{
}