2020-03-11 23:04:01 -04:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
/**
|
2020-03-12 11:45:11 -04:00
|
|
|
* Hummingbird Anime List Client
|
2020-03-11 23:04:01 -04:00
|
|
|
*
|
2020-03-12 11:45:11 -04:00
|
|
|
* An API client for Kitsu to manage anime and manga watch lists
|
2020-03-11 23:04:01 -04:00
|
|
|
*
|
2021-02-04 11:57:01 -05:00
|
|
|
* PHP version 8
|
2020-03-11 23:04:01 -04:00
|
|
|
*
|
2022-03-04 15:50:35 -05:00
|
|
|
* @copyright 2015 - 2022 Timothy J. Warren <tim@timshome.page>
|
2020-03-11 23:04:01 -04:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
2020-12-10 17:06:50 -05:00
|
|
|
* @version 5.2
|
2022-03-04 15:50:35 -05:00
|
|
|
* @link https://git.timshome.page/timw4mail/HummingBirdAnimeClient
|
2020-03-11 23:04:01 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Aviat\Ion\Di;
|
|
|
|
|
|
|
|
use Aviat\Ion\Di\Exception\{ContainerException, NotFoundException};
|
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dependency container
|
|
|
|
*/
|
2022-03-03 17:26:09 -05:00
|
|
|
class Container implements ContainerInterface
|
|
|
|
{
|
2020-03-11 23:04:01 -04:00
|
|
|
/**
|
|
|
|
* Array of object instances
|
|
|
|
*/
|
2021-02-10 17:17:51 -05:00
|
|
|
protected array $instances = [];
|
2020-03-11 23:04:01 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Map of logger instances
|
|
|
|
*/
|
2021-02-10 17:17:51 -05:00
|
|
|
protected array $loggers = [];
|
2020-03-11 23:04:01 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
2022-03-03 14:20:10 -05:00
|
|
|
* @param (callable)[] $container (optional)
|
|
|
|
*/
|
|
|
|
public function __construct(/**
|
|
|
|
* Array of container Generator functions
|
2020-03-11 23:04:01 -04:00
|
|
|
*/
|
2022-03-03 17:26:09 -05:00
|
|
|
protected array $container = []
|
|
|
|
) {
|
2020-03-11 23:04:01 -04:00
|
|
|
$this->loggers = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finds an entry of the container by its identifier and returns it.
|
|
|
|
*
|
|
|
|
* @param string $id - Identifier of the entry to look for.
|
|
|
|
*
|
|
|
|
* @throws ContainerException - Error while retrieving the entry.
|
2022-03-03 17:26:09 -05:00
|
|
|
* @throws NotFoundException - No entry was found for this identifier.
|
2020-03-11 23:04:01 -04:00
|
|
|
*
|
|
|
|
* @return mixed Entry.
|
|
|
|
*/
|
2021-02-26 14:42:07 -05:00
|
|
|
public function get(string $id): mixed
|
2020-03-11 23:04:01 -04:00
|
|
|
{
|
|
|
|
if ($this->has($id))
|
|
|
|
{
|
|
|
|
// Return an object instance, if it already exists
|
|
|
|
if (array_key_exists($id, $this->instances))
|
|
|
|
{
|
|
|
|
return $this->instances[$id];
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there isn't already an instance, create one
|
|
|
|
$obj = $this->getNew($id);
|
|
|
|
$this->instances[$id] = $obj;
|
2022-03-03 17:26:09 -05:00
|
|
|
|
2020-03-11 23:04:01 -04:00
|
|
|
return $obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new NotFoundException("Item '{$id}' does not exist in container.");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a new instance of the specified item
|
|
|
|
*
|
2022-03-03 17:26:09 -05:00
|
|
|
* @param string $id - Identifier of the entry to look for.
|
2021-02-26 14:42:07 -05:00
|
|
|
* @param array|null $args - Optional arguments for the factory callable
|
2020-03-11 23:04:01 -04:00
|
|
|
* @throws ContainerException - Error while retrieving the entry.
|
2022-03-03 17:26:09 -05:00
|
|
|
* @throws NotFoundException - No entry was found for this identifier.
|
2020-03-11 23:04:01 -04:00
|
|
|
*/
|
2021-02-26 14:42:07 -05:00
|
|
|
public function getNew(string $id, ?array $args = NULL): mixed
|
2020-03-11 23:04:01 -04:00
|
|
|
{
|
|
|
|
if ($this->has($id))
|
|
|
|
{
|
|
|
|
// By default, call a factory with the Container
|
|
|
|
$args = \is_array($args) ? $args : [$this];
|
2022-03-03 17:26:09 -05:00
|
|
|
$obj = ($this->container[$id])(...$args);
|
2020-03-11 23:04:01 -04:00
|
|
|
|
|
|
|
// Check for container interface, and apply the container to the object
|
|
|
|
// if applicable
|
|
|
|
return $this->applyContainer($obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new NotFoundException("Item '{$id}' does not exist in container.");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a factory to the container
|
|
|
|
*
|
2022-03-03 17:26:09 -05:00
|
|
|
* @param callable $value - a factory callable for the item
|
2020-03-11 23:04:01 -04:00
|
|
|
*/
|
2022-03-03 17:26:09 -05:00
|
|
|
public function set(string $id, callable $value): ContainerInterface
|
2020-03-11 23:04:01 -04:00
|
|
|
{
|
|
|
|
$this->container[$id] = $value;
|
2022-03-03 17:26:09 -05:00
|
|
|
|
2020-03-11 23:04:01 -04:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a specific instance in the container for an existing factory
|
|
|
|
*
|
|
|
|
* @throws NotFoundException - No entry was found for this identifier.
|
|
|
|
*/
|
2021-02-10 17:17:51 -05:00
|
|
|
public function setInstance(string $id, mixed $value): ContainerInterface
|
2020-03-11 23:04:01 -04:00
|
|
|
{
|
|
|
|
if ( ! $this->has($id))
|
|
|
|
{
|
|
|
|
throw new NotFoundException("Factory '{$id}' does not exist in container. Set that first.");
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->instances[$id] = $value;
|
2022-03-03 17:26:09 -05:00
|
|
|
|
2020-03-11 23:04:01 -04:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the container can return an entry for the given identifier.
|
|
|
|
* Returns false otherwise.
|
|
|
|
*
|
|
|
|
* @param string $id Identifier of the entry to look for.
|
|
|
|
*/
|
2021-02-26 14:42:07 -05:00
|
|
|
public function has(string $id): bool
|
2020-03-11 23:04:01 -04:00
|
|
|
{
|
|
|
|
return array_key_exists($id, $this->container);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine whether a logger channel is registered
|
|
|
|
*
|
2022-03-03 17:26:09 -05:00
|
|
|
* @param string $id The logger channel
|
2020-03-11 23:04:01 -04:00
|
|
|
*/
|
|
|
|
public function hasLogger(string $id = 'default'): bool
|
|
|
|
{
|
|
|
|
return array_key_exists($id, $this->loggers);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a logger to the Container
|
|
|
|
*
|
2022-03-03 17:26:09 -05:00
|
|
|
* @param string $id The logger 'channel'
|
2020-03-11 23:04:01 -04:00
|
|
|
*/
|
|
|
|
public function setLogger(LoggerInterface $logger, string $id = 'default'): ContainerInterface
|
|
|
|
{
|
|
|
|
$this->loggers[$id] = $logger;
|
2022-03-03 17:26:09 -05:00
|
|
|
|
2020-03-11 23:04:01 -04:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve a logger for the selected channel
|
|
|
|
*
|
2022-03-03 17:26:09 -05:00
|
|
|
* @param string $id The logger to retrieve
|
2020-03-11 23:04:01 -04:00
|
|
|
*/
|
|
|
|
public function getLogger(string $id = 'default'): ?LoggerInterface
|
|
|
|
{
|
|
|
|
return $this->hasLogger($id)
|
|
|
|
? $this->loggers[$id]
|
|
|
|
: NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if object implements ContainerAwareInterface
|
|
|
|
* or uses ContainerAware trait, and if so, apply the container
|
|
|
|
* to that object
|
|
|
|
*/
|
2021-02-10 17:17:51 -05:00
|
|
|
private function applyContainer(mixed $obj): mixed
|
2020-03-11 23:04:01 -04:00
|
|
|
{
|
2021-02-10 17:17:51 -05:00
|
|
|
$traitName = ContainerAware::class;
|
|
|
|
$interfaceName = ContainerAwareInterface::class;
|
2020-03-11 23:04:01 -04:00
|
|
|
|
2021-02-10 17:17:51 -05:00
|
|
|
$traits = class_uses($obj);
|
|
|
|
$traitsUsed = (is_array($traits)) ? $traits : [];
|
|
|
|
$usesTrait = in_array($traitName, $traitsUsed, TRUE);
|
2020-03-11 23:04:01 -04:00
|
|
|
|
2021-02-10 17:17:51 -05:00
|
|
|
$interfaces = class_implements($obj);
|
|
|
|
$implemented = (is_array($interfaces)) ? $interfaces : [];
|
|
|
|
$implementsInterface = in_array($interfaceName, $implemented, TRUE);
|
|
|
|
|
|
|
|
if ($usesTrait || $implementsInterface)
|
2020-03-11 23:04:01 -04:00
|
|
|
{
|
|
|
|
$obj->setContainer($this);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $obj;
|
|
|
|
}
|
|
|
|
}
|
2022-03-03 13:25:10 -05:00
|
|
|
|
2022-03-03 17:26:09 -05:00
|
|
|
// End of Container.php
|