From fa22b81fe9fa74fd953e95a615828a12bdc8905c Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Tue, 19 Apr 2022 22:06:38 +0300 Subject: [PATCH 01/15] Improve containers types to use iterable objects --- src/Container.php | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/Container.php b/src/Container.php index 6280fac9..599b67b5 100644 --- a/src/Container.php +++ b/src/Container.php @@ -230,11 +230,11 @@ 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 */ foreach ($config as $id => $definition) { @@ -258,11 +258,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 { $this->delegates = new CompositeContainer(); foreach ($delegates as $delegate) { @@ -327,7 +327,7 @@ private function validateDefinition($definition, ?string $id = null): void /** * @throws InvalidConfigException */ - private function validateMeta(array $meta): void + private function validateMeta(iterable $meta): void { /** @var mixed $value */ foreach ($meta as $key => $value) { @@ -359,10 +359,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.', $this->getVariableType($tags) ) ); @@ -395,7 +395,7 @@ private function validateDefinitionReset($reset): void /** * @throws InvalidConfigException */ - private function setTags(array $tags): void + private function setTags(iterable $tags): void { if ($this->validate) { foreach ($tags as $tag => $services) { @@ -407,10 +407,10 @@ private function setTags(array $tags): void ) ); } - 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.', $this->getVariableType($services) ) ); @@ -436,7 +436,7 @@ private function setTags(array $tags): void /** * @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)) { @@ -545,7 +545,7 @@ private function buildInternal(string $id) * @throws CircularReferenceException * @throws InvalidConfigException */ - private function addProviders(array $providers): void + private function addProviders(iterable $providers): void { $extensions = []; /** @var mixed $provider */ From c758bd42615dc13ac4df3de773d3c613ef38d5ef Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Tue, 19 Apr 2022 22:27:38 +0300 Subject: [PATCH 02/15] Improve type checking --- src/Container.php | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/Container.php b/src/Container.php index 599b67b5..5542b2ed 100644 --- a/src/Container.php +++ b/src/Container.php @@ -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; @@ -428,9 +429,9 @@ private function setTags(iterable $tags): void } } } - /** @psalm-var array> $tags */ + /** @psalm-var array>|Traversable $tags */ - $this->tags = $tags; + $this->tags = $tags instanceof Traversable ? iterator_to_array($tags) : $tags ; } /** @@ -439,7 +440,14 @@ private function setTags(iterable $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) : $tags; + if (!in_array($id, $tags, true)) { $this->tags[$tag][] = $id; } } From 096dc0f35ca976b3de066521aef0677410fe60f7 Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Tue, 19 Apr 2022 22:27:41 +0300 Subject: [PATCH 03/15] Fix tests --- tests/Unit/ContainerTest.php | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/tests/Unit/ContainerTest.php b/tests/Unit/ContainerTest.php index cbffde53..b4bb427c 100644 --- a/tests/Unit/ContainerTest.php +++ b/tests/Unit/ContainerTest.php @@ -1092,6 +1092,30 @@ public function testTagsWithExternalDefinition(): void $this->assertSame(EngineMarkTwo::class, get_class($engines[0])); } + public function testTagsIterable(): void + { + $config = ContainerConfig::create() + ->withDefinitions([ + EngineMarkOne::class => [ + 'class' => EngineMarkOne::class, + 'tags' => ['engine'], + ], + EngineMarkTwo::class => [ + 'class' => EngineMarkTwo::class, + ], + ]) + ->withTags(['engine' => new ArrayIterator([EngineMarkTwo::class])]) + ->withValidate(true); + $container = new Container($config); + + $engines = $container->get('tag@engine'); + + $this->assertIsArray($engines); + $this->assertCount(2, $engines); + $this->assertSame(EngineMarkOne::class, get_class($engines[1])); + $this->assertSame(EngineMarkTwo::class, get_class($engines[0])); + } + public function testTagsWithExternalDefinitionMerge(): void { $config = ContainerConfig::create() @@ -1771,7 +1795,7 @@ public function testNonArrayTags(): void $this->expectException(InvalidConfigException::class); $this->expectExceptionMessage( - 'Invalid definition: tags should be array of strings, integer given.' + 'Invalid definition: tags should be iterable object or array of strings, integer given.' ); new Container($config); } @@ -1784,7 +1808,7 @@ public function dataInvalidTags(): array [42 => [EngineMarkTwo::class]], ], [ - 'Invalid tags configuration: tag should contain array of service IDs, integer given.', + 'Invalid tags configuration: tag should be iterable object or array of service IDs, integer given.', ['engine' => 42], ], [ From 66942d8eb3ec3e31929387b27f0f718aa2274ec1 Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Tue, 19 Apr 2022 22:49:41 +0300 Subject: [PATCH 04/15] Fix types notation --- src/Container.php | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/Container.php b/src/Container.php index 5542b2ed..772dd0a7 100644 --- a/src/Container.php +++ b/src/Container.php @@ -60,7 +60,7 @@ final class Container implements ContainerInterface /** * @var array Tagged service IDs. The structure is `['tagID' => ['service1', 'service2']]`. - * @psalm-var array> + * @psalm-var array> */ private array $tags; @@ -238,6 +238,7 @@ private function addDefinition(string $id, $definition): 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( @@ -247,8 +248,8 @@ private function addDefinitions(iterable $config): void ) ); } - /** @var string $id */ + $id = (string) $id; $this->addDefinition($id, $definition); } } @@ -331,7 +332,9 @@ private function validateDefinition($definition, ?string $id = null): void private function validateMeta(iterable $meta): void { /** @var mixed $value */ + /** @psalm-suppress MixedAssignment */ foreach ($meta as $key => $value) { + $key = (string)$key; if (!in_array($key, self::ALLOWED_META, true)) { throw new InvalidConfigException( sprintf( @@ -404,7 +407,7 @@ private function setTags(iterable $tags): void throw new InvalidConfigException( sprintf( 'Invalid tags configuration: tag should be string, %s given.', - $tag + $this->getVariableType($tag) ) ); } @@ -429,9 +432,9 @@ private function setTags(iterable $tags): void } } } - /** @psalm-var array>|Traversable $tags */ + /** @psalm-var iterable> $tags */ - $this->tags = $tags instanceof Traversable ? iterator_to_array($tags) : $tags ; + $this->tags = $tags instanceof Traversable ? iterator_to_array($tags, true) : $tags ; } /** @@ -446,8 +449,9 @@ private function setDefinitionTags(string $id, iterable $tags): void } $tags = $this->tags[$tag]; - $tags = $tags instanceof Traversable ? iterator_to_array($tags) : $tags; + $tags = $tags instanceof Traversable ? iterator_to_array($tags, true) : $tags; if (!in_array($id, $tags, true)) { + /** @psalm-suppress PossiblyInvalidArrayAssignment */ $this->tags[$tag][] = $id; } } From ecefc6063b43acc0dc7c033fd7c676e7552d93bf Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Tue, 19 Apr 2022 22:51:12 +0300 Subject: [PATCH 05/15] Fix test --- tests/Unit/ContainerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Unit/ContainerTest.php b/tests/Unit/ContainerTest.php index b4bb427c..d8c74250 100644 --- a/tests/Unit/ContainerTest.php +++ b/tests/Unit/ContainerTest.php @@ -1804,7 +1804,7 @@ public function dataInvalidTags(): array { return [ [ - 'Invalid tags configuration: tag should be string, 42 given.', + 'Invalid tags configuration: tag should be string, integer given.', [42 => [EngineMarkTwo::class]], ], [ From a06b5ca8aea428d48069604e7a6466be437867c9 Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Wed, 20 Apr 2022 10:22:34 +0300 Subject: [PATCH 06/15] Fix text --- src/Container.php | 4 ++-- tests/Unit/ContainerTest.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Container.php b/src/Container.php index 772dd0a7..6a6d7b4c 100644 --- a/src/Container.php +++ b/src/Container.php @@ -366,7 +366,7 @@ private function validateDefinitionTags($tags): void if (!is_iterable($tags)) { throw new InvalidConfigException( sprintf( - 'Invalid definition: tags should be iterable object or array of strings, %s given.', + 'Invalid definition: tags should be either iterable or array of strings, %s given.', $this->getVariableType($tags) ) ); @@ -414,7 +414,7 @@ private function setTags(iterable $tags): void if (!is_iterable($services)) { throw new InvalidConfigException( sprintf( - 'Invalid tags configuration: tag should be iterable object or array of service IDs, %s given.', + 'Invalid tags configuration: tag should be either iterable or array of service IDs, %s given.', $this->getVariableType($services) ) ); diff --git a/tests/Unit/ContainerTest.php b/tests/Unit/ContainerTest.php index d8c74250..98c24b79 100644 --- a/tests/Unit/ContainerTest.php +++ b/tests/Unit/ContainerTest.php @@ -1795,7 +1795,7 @@ public function testNonArrayTags(): void $this->expectException(InvalidConfigException::class); $this->expectExceptionMessage( - 'Invalid definition: tags should be iterable object or array of strings, integer given.' + 'Invalid definition: tags should be either iterable or array of strings, integer given.' ); new Container($config); } @@ -1808,7 +1808,7 @@ public function dataInvalidTags(): array [42 => [EngineMarkTwo::class]], ], [ - 'Invalid tags configuration: tag should be iterable object or array of service IDs, integer given.', + 'Invalid tags configuration: tag should be either iterable or array of service IDs, integer given.', ['engine' => 42], ], [ From 0e0d1045cf8419d988b58c73be4d6bfa5d255847 Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Sat, 3 Dec 2022 00:25:53 +0300 Subject: [PATCH 07/15] Fix tests --- src/Container.php | 2 +- tests/Unit/ContainerTest.php | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Container.php b/src/Container.php index 2341fae5..367139a1 100644 --- a/src/Container.php +++ b/src/Container.php @@ -361,7 +361,7 @@ private function validateDefinitionTags(mixed $tags): void throw new InvalidConfigException( sprintf( 'Invalid definition: tags should be either iterable or array of strings, %s given.', - $this->getVariableType($tags) + get_debug_type($tags) ) ); } diff --git a/tests/Unit/ContainerTest.php b/tests/Unit/ContainerTest.php index 03697a20..88acf967 100644 --- a/tests/Unit/ContainerTest.php +++ b/tests/Unit/ContainerTest.php @@ -1970,15 +1970,15 @@ public function dataInvalidTags(): array { return [ [ - '/^Invalid tags configuration: tag should be string, 42 given\.$/', + 'Invalid tags configuration: tag should be string, array given.', [42 => [EngineMarkTwo::class]], ], [ - 'Invalid tags configuration: tag should be either iterable or array of service IDs, integer given.', + 'Invalid tags configuration: tag should be either iterable or array of service IDs, int given.', ['engine' => 42], ], [ - '/^Invalid tags configuration: service should be defined as class string, (integer|int) given\.$/', + 'Invalid tags configuration: service should be defined as class string, int given.', ['engine' => [42]], ], ]; @@ -1993,7 +1993,7 @@ public function testInvalidTags(string $message, array $tags): void ->withTags($tags); $this->expectException(InvalidConfigException::class); - $this->expectExceptionMessageMatches($message); + $this->expectExceptionMessage($message); new Container($config); } } From 3a87f77cb75422bef49a13e3b0ae381b58066b70 Mon Sep 17 00:00:00 2001 From: rector-bot Date: Fri, 2 Dec 2022 21:26:30 +0000 Subject: [PATCH 08/15] [ci-review] Apply changes from Rector action. --- tests/Unit/ContainerTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Unit/ContainerTest.php b/tests/Unit/ContainerTest.php index 88acf967..ebc302cb 100644 --- a/tests/Unit/ContainerTest.php +++ b/tests/Unit/ContainerTest.php @@ -1107,8 +1107,8 @@ public function testTagsIterable(): void $this->assertIsArray($engines); $this->assertCount(2, $engines); - $this->assertSame(EngineMarkOne::class, get_class($engines[1])); - $this->assertSame(EngineMarkTwo::class, get_class($engines[0])); + $this->assertSame(EngineMarkOne::class, $engines[1]::class); + $this->assertSame(EngineMarkTwo::class, $engines[0]::class); } public function testTagsWithExternalDefinitionMerge(): void From b0ae63a9c0d42e8aadc157206d67be17c3626155 Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Sat, 3 Dec 2022 00:28:52 +0300 Subject: [PATCH 09/15] Add support for iterable --- src/ContainerConfig.php | 32 ++++++++++++++++---------------- src/ContainerConfigInterface.php | 14 +++++++------- 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/src/ContainerConfig.php b/src/ContainerConfig.php index 182ed2b3..2f8313f0 100644 --- a/src/ContainerConfig.php +++ b/src/ContainerConfig.php @@ -9,11 +9,11 @@ */ final class ContainerConfig implements ContainerConfigInterface { - private array $definitions = []; - private array $providers = []; - private array $tags = []; + private iterable $definitions = []; + private iterable $providers = []; + private iterable $tags = []; private bool $validate = true; - private array $delegates = []; + private iterable $delegates = []; private bool $useStrictMode = false; private function __construct() @@ -26,46 +26,46 @@ public static function create(): self } /** - * @param array $definitions Definitions to put into container. + * @param iterable $definitions Definitions to put into container. */ - public function withDefinitions(array $definitions): self + public function withDefinitions(iterable $definitions): self { $new = clone $this; $new->definitions = $definitions; return $new; } - public function getDefinitions(): array + public function getDefinitions(): iterable { return $this->definitions; } /** - * @param array $providers Service providers to get definitions from. + * @param iterable $providers Service providers to get definitions from. */ - public function withProviders(array $providers): self + public function withProviders(iterable $providers): self { $new = clone $this; $new->providers = $providers; return $new; } - public function getProviders(): array + public function getProviders(): iterable { return $this->providers; } /** - * @param array $tags Tagged service IDs. The structure is `['tagID' => ['service1', 'service2']]`. + * @param iterable $tags Tagged service IDs. The structure is `['tagID' => ['service1', 'service2']]`. */ - public function withTags(array $tags): self + public function withTags(iterable $tags): self { $new = clone $this; $new->tags = $tags; return $new; } - public function getTags(): array + public function getTags(): iterable { return $this->tags; } @@ -86,18 +86,18 @@ public function shouldValidate(): bool } /** - * @param array $delegates Container delegates. Each delegate is a callable in format + * @param iterable $delegates Container delegates. Each delegate 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. */ - public function withDelegates(array $delegates): self + public function withDelegates(iterable $delegates): self { $new = clone $this; $new->delegates = $delegates; return $new; } - public function getDelegates(): array + public function getDelegates(): iterable { return $this->delegates; } diff --git a/src/ContainerConfigInterface.php b/src/ContainerConfigInterface.php index 9e535e5b..a4788ece 100644 --- a/src/ContainerConfigInterface.php +++ b/src/ContainerConfigInterface.php @@ -12,17 +12,17 @@ interface ContainerConfigInterface /** * @return array Definitions to put into container. */ - public function getDefinitions(): array; + public function getDefinitions(): iterable; /** - * @return array Service providers to get definitions from. + * @return iterable Service providers to get definitions from. */ - public function getProviders(): array; + public function getProviders(): iterable; /** - * @return array Tagged service IDs. The structure is `['tagID' => ['service1', 'service2']]`. + * @return iterable Tagged service IDs. The structure is `['tagID' => ['service1', 'service2']]`. */ - public function getTags(): array; + public function getTags(): iterable; /** * @return bool Whether definitions should be validated immediately. @@ -30,11 +30,11 @@ public function getTags(): array; public function shouldValidate(): bool; /** - * @return array Container delegates. Each delegate is a callable in format + * @return iterable Container delegates. Each delegate 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. */ - public function getDelegates(): array; + public function getDelegates(): iterable; /** * @return bool If the automatic addition of definition when class exists and can be resolved is disabled. From b7b4690824730cae07256b7d622b49ebf410790f Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Sat, 3 Dec 2022 00:29:03 +0300 Subject: [PATCH 10/15] Remove unused variable --- tests/Unit/ContainerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Unit/ContainerTest.php b/tests/Unit/ContainerTest.php index 88acf967..a1aa79c4 100644 --- a/tests/Unit/ContainerTest.php +++ b/tests/Unit/ContainerTest.php @@ -1963,7 +1963,7 @@ public function testNonArrayArguments(): void $this->expectExceptionMessage( 'Invalid definition: incorrect method "setNumber()" arguments. Expected array, got "int". Probably you should wrap them into square brackets.', ); - $container = new Container($config); + new Container($config); } public function dataInvalidTags(): array From f2832b76ee3f2de051c4fd61aac61c4e1155cad9 Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Sat, 3 Dec 2022 00:32:31 +0300 Subject: [PATCH 11/15] Add test --- tests/Unit/ContainerTest.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/Unit/ContainerTest.php b/tests/Unit/ContainerTest.php index a1aa79c4..67b5d6c5 100644 --- a/tests/Unit/ContainerTest.php +++ b/tests/Unit/ContainerTest.php @@ -1996,4 +1996,25 @@ public function testInvalidTags(string $message, array $tags): void $this->expectExceptionMessage($message); new Container($config); } + + public function testSupportIterableDefinitions(): void + { + $config = ContainerConfig::create() + ->withDefinitions( + (function () { + yield from [ + EngineMarkOne::class => [ + 'class' => EngineMarkOne::class, + 'setNumber()' => [42], + ], + ]; + })() + ); + + $container = new Container($config); + + $this->assertTrue($container->has(EngineMarkOne::class)); + $engine = $container->get(EngineMarkOne::class); + $this->assertEquals(42, $engine->getNumber()); + } } From a55146afa0b3d9ea76367c5b34091a4d47d9393e Mon Sep 17 00:00:00 2001 From: Dmitriy Derepko Date: Mon, 5 Dec 2022 00:16:29 +0300 Subject: [PATCH 12/15] Update src/ContainerConfigInterface.php Co-authored-by: Sergei Predvoditelev --- src/ContainerConfigInterface.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ContainerConfigInterface.php b/src/ContainerConfigInterface.php index a4788ece..8e3f0396 100644 --- a/src/ContainerConfigInterface.php +++ b/src/ContainerConfigInterface.php @@ -10,7 +10,7 @@ interface ContainerConfigInterface { /** - * @return array Definitions to put into container. + * @return iterable Definitions to put into container. */ public function getDefinitions(): iterable; From 6ddc142a968bbe3d5c95a604b2fcff966098bcce Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Sat, 1 Jul 2023 20:22:32 +0300 Subject: [PATCH 13/15] Add changelog --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c11cac6d..e1d8c4fa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,8 @@ # Yii Dependency Injection Change Log -## 1.2.2 under development +## 2.0.0 under development -- no changes in this release. +- Chg #299: Improve containers types to use iterable objects (@xepozz) ## 1.2.1 December 23, 2022 From eb447ed51f672308b96b96ce7afccd2d00ab2df1 Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Sat, 1 Jul 2023 20:23:14 +0300 Subject: [PATCH 14/15] Change types in ServiceProviderInterface and classes --- src/ServiceProviderInterface.php | 4 +- tests/Support/CarExtensionProvider.php | 4 +- tests/Support/CarProvider.php | 4 +- .../ContainerInterfaceExtensionProvider.php | 4 +- tests/Support/NullCarExtensionProvider.php | 4 +- tests/Unit/ContainerTest.php | 40 +++++++++---------- tests/Unit/ServiceProviderTest.php | 10 ++--- 7 files changed, 35 insertions(+), 35 deletions(-) diff --git a/src/ServiceProviderInterface.php b/src/ServiceProviderInterface.php index 275b7b0d..56dd295a 100644 --- a/src/ServiceProviderInterface.php +++ b/src/ServiceProviderInterface.php @@ -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; /** * 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; } diff --git a/tests/Support/CarExtensionProvider.php b/tests/Support/CarExtensionProvider.php index 1a5f1d48..382b851c 100644 --- a/tests/Support/CarExtensionProvider.php +++ b/tests/Support/CarExtensionProvider.php @@ -9,12 +9,12 @@ final class CarExtensionProvider implements ServiceProviderInterface { - public function getDefinitions(): array + public function getDefinitions(): iterable { return []; } - public function getExtensions(): array + public function getExtensions(): iterable { return [ Car::class => static function (ContainerInterface $container, Car $car) { diff --git a/tests/Support/CarProvider.php b/tests/Support/CarProvider.php index 6b4e7018..58efa270 100644 --- a/tests/Support/CarProvider.php +++ b/tests/Support/CarProvider.php @@ -9,7 +9,7 @@ final class CarProvider implements ServiceProviderInterface { - public function getDefinitions(): array + public function getDefinitions(): iterable { return [ 'car' => Car::class, @@ -17,7 +17,7 @@ public function getDefinitions(): array ]; } - public function getExtensions(): array + public function getExtensions(): iterable { return [ Car::class => static function (ContainerInterface $container, Car $car) { diff --git a/tests/Support/ContainerInterfaceExtensionProvider.php b/tests/Support/ContainerInterfaceExtensionProvider.php index ab3ebe07..516c2c17 100644 --- a/tests/Support/ContainerInterfaceExtensionProvider.php +++ b/tests/Support/ContainerInterfaceExtensionProvider.php @@ -9,12 +9,12 @@ final class ContainerInterfaceExtensionProvider implements ServiceProviderInterface { - public function getDefinitions(): array + public function getDefinitions(): iterable { return []; } - public function getExtensions(): array + public function getExtensions(): iterable { return [ ContainerInterface::class => static fn (ContainerInterface $container, ContainerInterface $extended) => $container, diff --git a/tests/Support/NullCarExtensionProvider.php b/tests/Support/NullCarExtensionProvider.php index 6080429d..53a45f0e 100644 --- a/tests/Support/NullCarExtensionProvider.php +++ b/tests/Support/NullCarExtensionProvider.php @@ -9,13 +9,13 @@ final class NullCarExtensionProvider implements ServiceProviderInterface { - public function getDefinitions(): array + public function getDefinitions(): iterable { return [ ]; } - public function getExtensions(): array + public function getExtensions(): iterable { return [ Car::class => static fn (ContainerInterface $container, Car $car) => null, diff --git a/tests/Unit/ContainerTest.php b/tests/Unit/ContainerTest.php index 6eff7952..1722c302 100644 --- a/tests/Unit/ContainerTest.php +++ b/tests/Unit/ContainerTest.php @@ -10,14 +10,18 @@ use Psr\Container\NotFoundExceptionInterface; use RuntimeException; use stdClass; +use Yiisoft\Definitions\DynamicReference; +use Yiisoft\Definitions\Exception\CircularReferenceException; +use Yiisoft\Definitions\Exception\InvalidConfigException; +use Yiisoft\Definitions\Reference; use Yiisoft\Di\BuildingException; use Yiisoft\Di\CompositeContainer; use Yiisoft\Di\Container; use Yiisoft\Di\ContainerConfig; use Yiisoft\Di\ExtensibleService; use Yiisoft\Di\NotFoundException; -use Yiisoft\Di\StateResetter; use Yiisoft\Di\ServiceProviderInterface; +use Yiisoft\Di\StateResetter; use Yiisoft\Di\Tests\Support\A; use Yiisoft\Di\Tests\Support\B; use Yiisoft\Di\Tests\Support\Car; @@ -41,15 +45,11 @@ use Yiisoft\Di\Tests\Support\PropertyTestClass; use Yiisoft\Di\Tests\Support\SportCar; use Yiisoft\Di\Tests\Support\TreeItem; -use Yiisoft\Di\Tests\Support\UnionTypeInConstructorSecondTypeInParamResolvable; -use Yiisoft\Di\Tests\Support\UnionTypeInConstructorSecondParamNotResolvable; -use Yiisoft\Di\Tests\Support\UnionTypeInConstructorParamNotResolvable; use Yiisoft\Di\Tests\Support\UnionTypeInConstructorFirstTypeInParamResolvable; +use Yiisoft\Di\Tests\Support\UnionTypeInConstructorParamNotResolvable; +use Yiisoft\Di\Tests\Support\UnionTypeInConstructorSecondParamNotResolvable; +use Yiisoft\Di\Tests\Support\UnionTypeInConstructorSecondTypeInParamResolvable; use Yiisoft\Di\Tests\Support\VariadicConstructor; -use Yiisoft\Definitions\DynamicReference; -use Yiisoft\Definitions\Exception\CircularReferenceException; -use Yiisoft\Definitions\Exception\InvalidConfigException; -use Yiisoft\Definitions\Reference; use Yiisoft\Injector\Injector; /** @@ -1397,7 +1397,7 @@ public function testResetterInProviderDefinitions(bool $strictMode): void ]) ->withProviders([ new class () implements ServiceProviderInterface { - public function getDefinitions(): array + public function getDefinitions(): iterable { return [ StateResetter::class => static function (ContainerInterface $container) { @@ -1412,7 +1412,7 @@ public function getDefinitions(): array ]; } - public function getExtensions(): array + public function getExtensions(): iterable { return []; } @@ -1442,12 +1442,12 @@ public function testResetterInProviderExtensions(): void ]) ->withProviders([ new class () implements ServiceProviderInterface { - public function getDefinitions(): array + public function getDefinitions(): iterable { return []; } - public function getExtensions(): array + public function getExtensions(): iterable { return [ StateResetter::class => static function ( @@ -1622,7 +1622,7 @@ public function testResetterInCompositeContainer(): void public function testCircularReferenceExceptionWhileResolvingProviders(): void { $provider = new class () implements ServiceProviderInterface { - public function getDefinitions(): array + public function getDefinitions(): iterable { return [ // E.g. wrapping container with proxy class @@ -1630,7 +1630,7 @@ public function getDefinitions(): array ]; } - public function getExtensions(): array + public function getExtensions(): iterable { return []; } @@ -1655,14 +1655,14 @@ public function getExtensions(): array public function testDifferentContainerWithProviders(): void { $provider = new class () implements ServiceProviderInterface { - public function getDefinitions(): array + public function getDefinitions(): iterable { return [ ContainerInterface::class => static fn (ContainerInterface $container) => new Container(ContainerConfig::create()), ]; } - public function getExtensions(): array + public function getExtensions(): iterable { return []; } @@ -1870,12 +1870,12 @@ public function testIntegerKeyInExtensions(): void $config = ContainerConfig::create() ->withProviders([ new class () implements ServiceProviderInterface { - public function getDefinitions(): array + public function getDefinitions(): iterable { return []; } - public function getExtensions(): array + public function getExtensions(): iterable { return [ 23 => static fn (ContainerInterface $container, StateResetter $resetter) => $resetter, @@ -1894,12 +1894,12 @@ public function testNonCallableExtension(): void $config = ContainerConfig::create() ->withProviders([ new class () implements ServiceProviderInterface { - public function getDefinitions(): array + public function getDefinitions(): iterable { return []; } - public function getExtensions(): array + public function getExtensions(): iterable { return [ ColorPink::class => [], diff --git a/tests/Unit/ServiceProviderTest.php b/tests/Unit/ServiceProviderTest.php index 73007e5b..b1ced10b 100644 --- a/tests/Unit/ServiceProviderTest.php +++ b/tests/Unit/ServiceProviderTest.php @@ -6,21 +6,21 @@ use PHPUnit\Framework\TestCase; use Psr\Container\ContainerInterface; +use Yiisoft\Definitions\Exception\InvalidConfigException; use Yiisoft\Di\Container; use Yiisoft\Di\ContainerConfig; use Yiisoft\Di\ServiceProviderInterface; use Yiisoft\Di\Tests\Support\Car; -use Yiisoft\Di\Tests\Support\CarProvider; use Yiisoft\Di\Tests\Support\CarExtensionProvider; -use Yiisoft\Di\Tests\Support\ContainerInterfaceExtensionProvider; +use Yiisoft\Di\Tests\Support\CarProvider; use Yiisoft\Di\Tests\Support\ColorRed; +use Yiisoft\Di\Tests\Support\ContainerInterfaceExtensionProvider; use Yiisoft\Di\Tests\Support\EngineInterface; use Yiisoft\Di\Tests\Support\EngineMarkOne; use Yiisoft\Di\Tests\Support\EngineMarkTwo; use Yiisoft\Di\Tests\Support\MethodTestClass; use Yiisoft\Di\Tests\Support\NullCarExtensionProvider; use Yiisoft\Di\Tests\Support\SportCar; -use Yiisoft\Definitions\Exception\InvalidConfigException; final class ServiceProviderTest extends TestCase { @@ -174,12 +174,12 @@ public function testClassMethodsWithExtensible(): void ]) ->withProviders([ new class () implements ServiceProviderInterface { - public function getDefinitions(): array + public function getDefinitions(): iterable { return []; } - public function getExtensions(): array + public function getExtensions(): iterable { return [ 'method_test' => static fn (ContainerInterface $container, MethodTestClass $class) => $class, From 0e4059dc1a725687c6ad23b4ec852e9d7d03b73e Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Sun, 2 Jul 2023 07:22:29 +0300 Subject: [PATCH 15/15] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e1d8c4fa..be05a545 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ## 2.0.0 under development -- Chg #299: Improve containers types to use iterable objects (@xepozz) +- Chg #299: Improve types to use iterable objects (@xepozz) ## 1.2.1 December 23, 2022