123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- <?php
- declare(strict_types=1);
- namespace Hyperf\Di;
- use Hyperf\Contract\ContainerInterface as HyperfContainerInterface;
- use Hyperf\Di\Definition\DefinitionInterface;
- use Hyperf\Di\Definition\ObjectDefinition;
- use Hyperf\Di\Exception\InvalidArgumentException;
- use Hyperf\Di\Exception\NotFoundException;
- use Hyperf\Di\Resolver\ResolverDispatcher;
- use Psr\Container\ContainerInterface as PsrContainerInterface;
- class Container implements HyperfContainerInterface
- {
-
- private $resolvedEntries = [];
-
- private $fetchedDefinitions = [];
-
- private $definitionSource;
-
- private $definitionResolver;
-
- public function __construct(Definition\DefinitionSourceInterface $definitionSource)
- {
- $this->definitionSource = $definitionSource;
- $this->definitionResolver = new ResolverDispatcher($this);
-
- $this->resolvedEntries = [
- self::class => $this,
- PsrContainerInterface::class => $this,
- HyperfContainerInterface::class => $this,
- ];
- }
-
- public function make(string $name, array $parameters = [])
- {
- $definition = $this->getDefinition($name);
- if (! $definition) {
- throw new NotFoundException("No entry or class found for '{$name}'");
- }
- return $this->resolveDefinition($definition, $parameters);
- }
-
- public function set(string $name, $entry)
- {
- $this->resolvedEntries[$name] = $entry;
- }
-
- public function unbind(string $name)
- {
- if ($this->has($name)) {
- unset($this->resolvedEntries[$name]);
- }
- }
-
- public function define(string $name, $definition)
- {
- $this->setDefinition($name, $definition);
- }
-
- public function get($name)
- {
-
- if (isset($this->resolvedEntries[$name]) || array_key_exists($name, $this->resolvedEntries)) {
- return $this->resolvedEntries[$name];
- }
- return $this->resolvedEntries[$name] = $this->make($name);
- }
-
- public function has($name): bool
- {
- if (! is_string($name)) {
- throw new InvalidArgumentException(sprintf('The name parameter must be of type string, %s given', is_object($name) ? get_class($name) : gettype($name)));
- }
- if (array_key_exists($name, $this->resolvedEntries)) {
- return true;
- }
- $definition = $this->getDefinition($name);
- if ($definition === null) {
- return false;
- }
- if ($definition instanceof ObjectDefinition) {
- return $definition->isInstantiable();
- }
- return true;
- }
- public function getDefinitionSource(): Definition\DefinitionSourceInterface
- {
- return $this->definitionSource;
- }
-
- private function setDefinition(string $name, $definition): void
- {
-
- if (array_key_exists($name, $this->resolvedEntries)) {
- unset($this->resolvedEntries[$name]);
- }
- $this->fetchedDefinitions = [];
- $this->definitionSource->addDefinition($name, $definition);
- }
- private function getDefinition(string $name): ?DefinitionInterface
- {
-
- if (! array_key_exists($name, $this->fetchedDefinitions)) {
- $this->fetchedDefinitions[$name] = $this->definitionSource->getDefinition($name);
- }
- return $this->fetchedDefinitions[$name];
- }
-
- private function resolveDefinition(DefinitionInterface $definition, array $parameters = [])
- {
- return $this->definitionResolver->resolve($definition, $parameters);
- }
- }
|