From 63575dcaf6efb6871480cbfd67d2cdcd3886b328 Mon Sep 17 00:00:00 2001 From: webimpress Date: Wed, 7 Feb 2018 18:18:43 +0000 Subject: [PATCH 1/4] Fix invokable configuration when key is not provided and docs update --- README.md | 2 +- src/Config.php | 4 ++++ test/ConfigTest.php | 15 +++++++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 6f1ce55..055e540 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,7 @@ The `dependencies` sub associative array can contain the following keys: - `services`: an associative array that maps a key to a specific service instance. - `invokables`: an associative array that map a key to a constructor-less service; i.e., for services that do not require arguments to the constructor. - The key and service name may be the same; if they are not, the name is treated + The key and service name may be the same; if they are not, the key is treated as an alias. - `factories`: an associative array that maps a service name to a factory class name, or any callable. Factory classes must be instantiable without arguments, diff --git a/src/Config.php b/src/Config.php index 3ec3929..c1821b2 100644 --- a/src/Config.php +++ b/src/Config.php @@ -83,6 +83,10 @@ private function injectInvokables(Container $container, array $dependencies) } foreach ($dependencies['invokables'] as $name => $object) { + if (is_int($name)) { + $name = $object; + } + $container[$name] = function (Container $c) use ($object) { return new $object(); }; diff --git a/test/ConfigTest.php b/test/ConfigTest.php index 842050b..30c1af0 100644 --- a/test/ConfigTest.php +++ b/test/ConfigTest.php @@ -334,4 +334,19 @@ public function testInjectMultipleDelegators() $service->injected ); } + + public function testInvokablesWithoutAlias() + { + $dependencies = [ + 'invokables' => [ + TestAsset\Service::class, + ], + ]; + + (new Config(['dependencies' => $dependencies]))->configureContainer($this->container); + + self::assertTrue($this->container->offsetExists(TestAsset\Service::class)); + $service = $this->container->offsetGet(TestAsset\Service::class); + self::assertInstanceOf(TestAsset\Service::class, $service); + } } From 47587cb731bb101a7992b0a566a35ba6dfcb2b60 Mon Sep 17 00:00:00 2001 From: webimpress Date: Wed, 7 Feb 2018 23:04:12 +0000 Subject: [PATCH 2/4] Always create alias for invokables The same behaviour we have in zend-servicemanager. Proper configuration should be: 'invokables' => [ Name\Service::class => Name\Service::class, ], or when we want to use alias: 'invokables' => [ 'alias' => Name\Service::class, ], then serivce will be available on: - alias: $container->get('alias'); - class name: $container->get(Name\Service::class); and it will be exactly the same instance. --- src/Config.php | 8 +++++--- test/ConfigTest.php | 22 +++++++++++++++++++++- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/Config.php b/src/Config.php index c1821b2..1b38107 100644 --- a/src/Config.php +++ b/src/Config.php @@ -83,11 +83,13 @@ private function injectInvokables(Container $container, array $dependencies) } foreach ($dependencies['invokables'] as $name => $object) { - if (is_int($name)) { - $name = $object; + if ($name !== $object) { + $container[$name] = function (Container $c) use ($object) { + return $c->offsetGet($object); + }; } - $container[$name] = function (Container $c) use ($object) { + $container[$object] = function (Container $c) use ($object) { return new $object(); }; } diff --git a/test/ConfigTest.php b/test/ConfigTest.php index 30c1af0..210fa7a 100644 --- a/test/ConfigTest.php +++ b/test/ConfigTest.php @@ -335,7 +335,7 @@ public function testInjectMultipleDelegators() ); } - public function testInvokablesWithoutAlias() + public function testInvokableWithAlias() { $dependencies = [ 'invokables' => [ @@ -348,5 +348,25 @@ public function testInvokablesWithoutAlias() self::assertTrue($this->container->offsetExists(TestAsset\Service::class)); $service = $this->container->offsetGet(TestAsset\Service::class); self::assertInstanceOf(TestAsset\Service::class, $service); + self::assertTrue($this->container->offsetExists('0')); + } + + public function testInvokableWithoutAlias() + { + $dependencies = [ + 'invokables' => [ + 'alias' => TestAsset\Service::class, + ], + ]; + + (new Config(['dependencies' => $dependencies]))->configureContainer($this->container); + + self::assertTrue($this->container->offsetExists('alias')); + $service = $this->container->offsetGet('alias'); + self::assertInstanceOf(TestAsset\Service::class, $service); + self::assertTrue($this->container->offsetExists(TestAsset\Service::class)); + $originService = $this->container->offsetGet(TestAsset\Service::class); + self::assertInstanceOf(TestAsset\Service::class, $originService); + self::assertSame($service, $originService); } } From 7bca1e9388fd7c3cb20a7f044dda915b90cc4918 Mon Sep 17 00:00:00 2001 From: webimpress Date: Wed, 7 Feb 2018 23:16:08 +0000 Subject: [PATCH 3/4] Updated initializers documentation in README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 055e540..f0f2cec 100644 --- a/README.md +++ b/README.md @@ -49,8 +49,8 @@ The `dependencies` sub associative array can contain the following keys: - `services`: an associative array that maps a key to a specific service instance. - `invokables`: an associative array that map a key to a constructor-less service; i.e., for services that do not require arguments to the constructor. - The key and service name may be the same; if they are not, the key is treated - as an alias. + The key and service name usually are the same; if they are not, the key is + treated as an alias. - `factories`: an associative array that maps a service name to a factory class name, or any callable. Factory classes must be instantiable without arguments, and callable once instantiated (i.e., implement the `__invoke()` method). From de1504c8d470c6ff9523d70b9b1eaffc36c2ebb8 Mon Sep 17 00:00:00 2001 From: webimpress Date: Thu, 8 Feb 2018 07:37:38 +0000 Subject: [PATCH 4/4] Fixed test names - per @xtreamwayz --- test/ConfigTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/ConfigTest.php b/test/ConfigTest.php index 210fa7a..cff96b6 100644 --- a/test/ConfigTest.php +++ b/test/ConfigTest.php @@ -335,7 +335,7 @@ public function testInjectMultipleDelegators() ); } - public function testInvokableWithAlias() + public function testInvokableWithoutAlias() { $dependencies = [ 'invokables' => [ @@ -351,7 +351,7 @@ public function testInvokableWithAlias() self::assertTrue($this->container->offsetExists('0')); } - public function testInvokableWithoutAlias() + public function testInvokableWithAlias() { $dependencies = [ 'invokables' => [