-
-
Notifications
You must be signed in to change notification settings - Fork 45
Improve containers types to use iterable objects #299
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 5 commits
fa22b81
c758bd4
096dc0f
66942d8
ecefc60
a06b5ca
21a7526
0e0d104
3a87f77
b0ae63a
b7b4690
f2832b7
fef57b1
a55146a
20bcdde
6ddc142
eb447ed
0e4059d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |||
| use Closure; | ||||
| use InvalidArgumentException; | ||||
| use Psr\Container\ContainerInterface; | ||||
| use Traversable; | ||||
| use Yiisoft\Definitions\ArrayDefinition; | ||||
| use Yiisoft\Definitions\Exception\CircularReferenceException; | ||||
| use Yiisoft\Definitions\Exception\InvalidConfigException; | ||||
|
|
@@ -59,7 +60,7 @@ final class Container implements ContainerInterface | |||
|
|
||||
| /** | ||||
| * @var array Tagged service IDs. The structure is `['tagID' => ['service1', 'service2']]`. | ||||
| * @psalm-var array<string, list<string>> | ||||
| * @psalm-var array<string, iterable<string>> | ||||
| */ | ||||
| private array $tags; | ||||
|
|
||||
|
|
@@ -230,13 +231,14 @@ private function addDefinition(string $id, $definition): void | |||
| /** | ||||
| * Sets multiple definitions at once. | ||||
| * | ||||
| * @param array $config Definitions indexed by their IDs. | ||||
| * @param iterable $config Definitions indexed by their IDs. | ||||
| * | ||||
| * @throws InvalidConfigException | ||||
| */ | ||||
| private function addDefinitions(array $config): void | ||||
| private function addDefinitions(iterable $config): void | ||||
| { | ||||
| /** @var mixed $definition */ | ||||
| /** @psalm-suppress MixedAssignment */ | ||||
| foreach ($config as $id => $definition) { | ||||
| if ($this->validate && !is_string($id)) { | ||||
| throw new InvalidConfigException( | ||||
|
|
@@ -246,8 +248,8 @@ private function addDefinitions(array $config): void | |||
| ) | ||||
| ); | ||||
| } | ||||
| /** @var string $id */ | ||||
|
|
||||
| $id = (string) $id; | ||||
vjik marked this conversation as resolved.
Show resolved
Hide resolved
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not need type-casting. Above check that $id is string. |
||||
| $this->addDefinition($id, $definition); | ||||
| } | ||||
| } | ||||
|
|
@@ -258,11 +260,11 @@ private function addDefinitions(array $config): void | |||
| * Each delegate must is a callable in format "function (ContainerInterface $container): ContainerInterface". | ||||
| * The container instance returned is used in case a service can not be found in primary container. | ||||
| * | ||||
| * @param array $delegates | ||||
| * @param iterable $delegates | ||||
| * | ||||
| * @throws InvalidConfigException | ||||
| */ | ||||
| private function setDelegates(array $delegates): void | ||||
| private function setDelegates(iterable $delegates): void | ||||
vjik marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
| { | ||||
| $this->delegates = new CompositeContainer(); | ||||
| foreach ($delegates as $delegate) { | ||||
|
|
@@ -327,10 +329,12 @@ private function validateDefinition($definition, ?string $id = null): void | |||
| /** | ||||
| * @throws InvalidConfigException | ||||
| */ | ||||
| private function validateMeta(array $meta): void | ||||
| private function validateMeta(iterable $meta): void | ||||
vjik marked this conversation as resolved.
Show resolved
Hide resolved
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||
| { | ||||
| /** @var mixed $value */ | ||||
| /** @psalm-suppress MixedAssignment */ | ||||
| foreach ($meta as $key => $value) { | ||||
| $key = (string)$key; | ||||
arogachev marked this conversation as resolved.
Show resolved
Hide resolved
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Not need. |
||||
| if (!in_array($key, self::ALLOWED_META, true)) { | ||||
| throw new InvalidConfigException( | ||||
| sprintf( | ||||
|
|
@@ -359,10 +363,10 @@ private function validateMeta(array $meta): void | |||
| */ | ||||
| private function validateDefinitionTags($tags): void | ||||
| { | ||||
| if (!is_array($tags)) { | ||||
| if (!is_iterable($tags)) { | ||||
| throw new InvalidConfigException( | ||||
| sprintf( | ||||
| 'Invalid definition: tags should be array of strings, %s given.', | ||||
| 'Invalid definition: tags should be iterable object or array of strings, %s given.', | ||||
arogachev marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||
| $this->getVariableType($tags) | ||||
| ) | ||||
| ); | ||||
|
|
@@ -395,22 +399,22 @@ private function validateDefinitionReset($reset): void | |||
| /** | ||||
| * @throws InvalidConfigException | ||||
| */ | ||||
| private function setTags(array $tags): void | ||||
| private function setTags(iterable $tags): void | ||||
vjik marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
| { | ||||
| if ($this->validate) { | ||||
| foreach ($tags as $tag => $services) { | ||||
| if (!is_string($tag)) { | ||||
| throw new InvalidConfigException( | ||||
| sprintf( | ||||
| 'Invalid tags configuration: tag should be string, %s given.', | ||||
| $tag | ||||
| $this->getVariableType($tag) | ||||
| ) | ||||
| ); | ||||
| } | ||||
| if (!is_array($services)) { | ||||
| if (!is_iterable($services)) { | ||||
| throw new InvalidConfigException( | ||||
| sprintf( | ||||
| 'Invalid tags configuration: tag should contain array of service IDs, %s given.', | ||||
| 'Invalid tags configuration: tag should be iterable object or array of service IDs, %s given.', | ||||
arogachev marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||
| $this->getVariableType($services) | ||||
| ) | ||||
| ); | ||||
|
|
@@ -428,18 +432,26 @@ private function setTags(array $tags): void | |||
| } | ||||
| } | ||||
| } | ||||
| /** @psalm-var array<string, list<string>> $tags */ | ||||
| /** @psalm-var iterable<string, iterable<string>> $tags */ | ||||
|
|
||||
| $this->tags = $tags; | ||||
| $this->tags = $tags instanceof Traversable ? iterator_to_array($tags, true) : $tags ; | ||||
| } | ||||
|
|
||||
| /** | ||||
| * @psalm-param string[] $tags | ||||
| */ | ||||
| private function setDefinitionTags(string $id, array $tags): void | ||||
| private function setDefinitionTags(string $id, iterable $tags): void | ||||
| { | ||||
| foreach ($tags as $tag) { | ||||
| if (!isset($this->tags[$tag]) || !in_array($id, $this->tags[$tag], true)) { | ||||
| if (!isset($this->tags[$tag])) { | ||||
| $this->tags[$tag] = [$id]; | ||||
| continue; | ||||
| } | ||||
|
|
||||
| $tags = $this->tags[$tag]; | ||||
| $tags = $tags instanceof Traversable ? iterator_to_array($tags, true) : $tags; | ||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Better convert iterable to array before foreach |
||||
| if (!in_array($id, $tags, true)) { | ||||
| /** @psalm-suppress PossiblyInvalidArrayAssignment */ | ||||
| $this->tags[$tag][] = $id; | ||||
| } | ||||
| } | ||||
|
|
@@ -545,7 +557,7 @@ private function buildInternal(string $id) | |||
| * @throws CircularReferenceException | ||||
| * @throws InvalidConfigException | ||||
| */ | ||||
| private function addProviders(array $providers): void | ||||
| private function addProviders(iterable $providers): void | ||||
vjik marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
| { | ||||
| $extensions = []; | ||||
| /** @var mixed $provider */ | ||||
|
|
||||
Uh oh!
There was an error while loading. Please reload this page.