Final Class Yiisoft\Di\Container
| Inheritance | Yiisoft\Di\Container |
|---|---|
| Implements | Psr\Container\ContainerInterface |
Container implements a dependency injection container.
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\Di\Container | |
| get() | Returns an instance by either interface name or alias. | Yiisoft\Di\Container |
| has() | Returns a value indicating whether the container has the definition of the specified name. | Yiisoft\Di\Container |
Constants
| Constant | Value | Description | Defined By |
|---|---|---|---|
| ALLOWED_META | [ self::META_TAGS, self::META_RESET, ] | Yiisoft\Di\Container | |
| META_RESET | 'reset' | Yiisoft\Di\Container | |
| META_TAGS | 'tags' | Yiisoft\Di\Container |
Method Details
| public mixed __construct ( Yiisoft\Di\ContainerConfigInterface|null $config = null ) | ||
| $config | Yiisoft\Di\ContainerConfigInterface|null |
Container configuration. |
| throws | \Yiisoft\Definitions\Exception\InvalidConfigException |
If configuration is not valid. |
|---|---|---|
public function __construct(?ContainerConfigInterface $config = null)
{
$config ??= ContainerConfig::create();
$this->definitions = new DefinitionStorage(
[
ContainerInterface::class => $this,
StateResetter::class => StateResetter::class,
],
$config->useStrictMode()
);
$this->validate = $config->shouldValidate();
$this->setTags($config->getTags());
$this->addDefinitions($config->getDefinitions());
$this->addProviders($config->getProviders());
$this->setDelegates($config->getDelegates());
}
Returns an instance by either interface name or alias.
The same instance of the class will be returned each time this method is called.
| public mixed get ( string $id ) | ||
| $id | string |
The interface or an alias name that was previously registered. |
| return | mixed |
An instance of the requested interface. |
|---|---|---|
| throws | \Yiisoft\Definitions\Exception\CircularReferenceException | |
| throws | \Yiisoft\Definitions\Exception\InvalidConfigException | |
| throws | \Psr\Container\NotFoundExceptionInterface | |
| throws | \Yiisoft\Definitions\Exception\NotInstantiableException | |
| throws | Yiisoft\Di\BuildingException | |
public function get(string $id)
{
// Fast path: check if instance exists.
if (array_key_exists($id, $this->instances)) {
if ($id === StateResetter::class) {
return $this->prepareStateResetter();
}
return $this->instances[$id];
}
try {
$this->instances[$id] = $this->build($id);
} catch (NotFoundException $exception) {
// Fast path: if the exception ID matches the requested ID, no need to modify stack.
if ($exception->getId() === $id) {
// Try delegates before giving up.
try {
if ($this->delegates->has($id)) {
return $this->delegates->get($id);
}
} catch (Throwable $e) {
throw new BuildingException($id, $e, $this->definitions->getBuildStack(), $e);
}
throw $exception;
}
// Add current ID to build stack for better error reporting.
$buildStack = $exception->getBuildStack();
array_unshift($buildStack, $id);
throw new NotFoundException($exception->getId(), $buildStack);
} catch (NotFoundExceptionInterface $exception) {
// Try delegates before giving up
try {
if ($this->delegates->has($id)) {
return $this->delegates->get($id);
}
} catch (Throwable $e) {
throw new BuildingException($id, $e, $this->definitions->getBuildStack(), $e);
}
throw new NotFoundException($id, [$id], previous: $exception);
} catch (ContainerExceptionInterface $e) {
if (!$e instanceof InvalidConfigException) {
throw $e;
}
throw new BuildingException($id, $e, $this->definitions->getBuildStack(), $e);
} catch (Throwable $e) {
throw new BuildingException($id, $e, $this->definitions->getBuildStack(), $e);
}
// Handle StateResetter for newly built instances.
if ($id === StateResetter::class) {
return $this->prepareStateResetter();
}
return $this->instances[$id];
}
Returns a value indicating whether the container has the definition of the specified name.
See also \Yiisoft\Di\addDefinition().
| public boolean has ( string $id ) | ||
| $id | string |
Class name, interface name or alias name. |
| return | boolean |
Whether the container is able to provide instance of class specified. |
|---|---|---|
public function has(string $id): bool
{
try {
if ($this->definitions->has($id)) {
return true;
}
} catch (CircularReferenceException) {
return true;
}
if (TagReference::isTagAlias($id)) {
$tag = TagReference::extractTagFromAlias($id);
return isset($this->tags[$tag]);
}
return false;
}
Signup or Login in order to comment.