-
-
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 all 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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -9,6 +9,7 @@ | |||||
| use Psr\Container\ContainerInterface; | ||||||
| use Psr\Container\NotFoundExceptionInterface; | ||||||
| use Throwable; | ||||||
| use Traversable; | ||||||
| use Yiisoft\Definitions\ArrayDefinition; | ||||||
| use Yiisoft\Definitions\DefinitionStorage; | ||||||
| use Yiisoft\Definitions\Exception\CircularReferenceException; | ||||||
|
|
@@ -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; | ||||||
|
|
||||||
|
|
@@ -223,13 +224,14 @@ private function addDefinition(string $id, mixed $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( | ||||||
|
|
@@ -239,8 +241,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); | ||||||
| } | ||||||
| } | ||||||
|
|
@@ -251,11 +253,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(); | ||||||
| $container = $this->get(ContainerInterface::class); | ||||||
|
|
@@ -323,10 +325,12 @@ private function validateDefinition(mixed $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( | ||||||
|
|
@@ -353,10 +357,10 @@ private function validateMeta(array $meta): void | |||||
| */ | ||||||
| private function validateDefinitionTags(mixed $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 either iterable or array of strings, %s given.', | ||||||
| get_debug_type($tags) | ||||||
| ) | ||||||
| ); | ||||||
|
|
@@ -387,22 +391,22 @@ private function validateDefinitionReset(mixed $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 | ||||||
| get_debug_type($services) | ||||||
|
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
|
||||||
| ) | ||||||
| ); | ||||||
| } | ||||||
| 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 either iterable or array of service IDs, %s given.', | ||||||
| get_debug_type($services) | ||||||
| ) | ||||||
| ); | ||||||
|
|
@@ -420,18 +424,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; | ||||||
| } | ||||||
| } | ||||||
|
|
@@ -537,7 +549,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 */ | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -42,7 +42,7 @@ interface ServiceProviderInterface | |
| * @return array Definitions for the container. Each array key is the name of the service (usually it is | ||
| * an interface name), and a corresponding value is a service definition. | ||
| */ | ||
| public function getDefinitions(): array; | ||
| public function getDefinitions(): iterable; | ||
|
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. Need fix phpdoc. |
||
|
|
||
| /** | ||
| * Returns an array of service extensions. | ||
|
|
@@ -58,5 +58,5 @@ public function getDefinitions(): array; | |
| * @return array Extensions for the container services. Each array key is the name of the service to be modified | ||
| * and a corresponding value is callable doing the job. | ||
| */ | ||
| public function getExtensions(): array; | ||
| public function getExtensions(): iterable; | ||
|
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. Need fix phpdoc. |
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.