From 48081ea402a453a269c80c6ee31b4947f29bdf34 Mon Sep 17 00:00:00 2001 From: dantleech Date: Wed, 29 Jun 2016 15:04:16 +0100 Subject: [PATCH 1/2] Removing build() method --- lib/Container.php | 122 +++++++++++++++++------------------ tests/Unit/ContainerTest.php | 5 -- 2 files changed, 58 insertions(+), 69 deletions(-) diff --git a/lib/Container.php b/lib/Container.php index 6cf34c3..025fc70 100644 --- a/lib/Container.php +++ b/lib/Container.php @@ -32,70 +32,7 @@ public function __construct(array $extensionClasses = [], array $userConfig = [] { $this->extensionClasses = $extensionClasses; $this->userConfig = $userConfig; - } - - /** - * Configure the container. This method will call the `configure()` method - * on each extension. Extensions must use this opportunity to register their - * services and define any default config. - * - * This method must be called before `build()`. - */ - public function init() - { - $extensions = []; - - if (empty($this->extensionClasses) && empty($this->userConfig)) { - return; - } - - foreach ($this->extensionClasses as $extensionClass) { - if (!class_exists($extensionClass)) { - throw new \InvalidArgumentException(sprintf( - 'Extension class "%s" does not exist', - $extensionClass - )); - } - - $extension = new $extensionClass(); - - if (!$extension instanceof ExtensionInterface) { - throw new \InvalidArgumentException(sprintf( - // add any manually specified extensions - 'Extension "%s" must implement the PhpBench\\Extension interface', - get_class($extension) - )); - } - - $extensions[] = $extension; - - $this->config = array_merge( - $this->config, - $extension->getDefaultConfig() - ); - } - - $diff = array_diff(array_keys($this->userConfig), array_keys($this->config)); - - if ($diff) { - throw new \InvalidArgumentException(sprintf( - 'Unknown configuration keys: "%s". Permitted keys: "%s"', - implode('", "', $diff), implode('", "', array_keys($this->config)) - )); - } - - $this->config = array_merge( - $this->config, - $this->userConfig - ); - - foreach ($extensions as $extension) { - $extension->load($this); - } - - foreach ($extensions as $extension) { - $extension->build($this); - } + $this->init(); } /** @@ -240,4 +177,61 @@ public function hasParameter($name) { return array_key_exists($name, $this->config); } + + private function init() + { + $extensions = []; + + if (empty($this->extensionClasses) && empty($this->userConfig)) { + return; + } + + foreach ($this->extensionClasses as $extensionClass) { + if (!class_exists($extensionClass)) { + throw new \InvalidArgumentException(sprintf( + 'Extension class "%s" does not exist', + $extensionClass + )); + } + + $extension = new $extensionClass(); + + if (!$extension instanceof ExtensionInterface) { + throw new \InvalidArgumentException(sprintf( + // add any manually specified extensions + 'Extension "%s" must implement the PhpBench\\Extension interface', + get_class($extension) + )); + } + + $extensions[] = $extension; + + $this->config = array_merge( + $this->config, + $extension->getDefaultConfig() + ); + } + + $diff = array_diff(array_keys($this->userConfig), array_keys($this->config)); + + if ($diff) { + throw new \InvalidArgumentException(sprintf( + 'Unknown configuration keys: "%s". Permitted keys: "%s"', + implode('", "', $diff), implode('", "', array_keys($this->config)) + )); + } + + $this->config = array_merge( + $this->config, + $this->userConfig + ); + + foreach ($extensions as $extension) { + $extension->load($this); + } + + foreach ($extensions as $extension) { + $extension->build($this); + } + } } diff --git a/tests/Unit/ContainerTest.php b/tests/Unit/ContainerTest.php index d7f51cb..4b51e78 100644 --- a/tests/Unit/ContainerTest.php +++ b/tests/Unit/ContainerTest.php @@ -103,7 +103,6 @@ public function testRegisterExtension() __NAMESPACE__ . '\\TestExtension', ]); - $container->init(); $object = $container->get('foobar'); $this->assertInstanceOf('stdClass', $object); $this->assertEquals('bar', $object->foobar); @@ -124,7 +123,6 @@ public function testRegisterExtensionWithUserConfig() ] ); - $container->init(); $object = $container->get('foobar'); $this->assertInstanceOf('stdClass', $object); $this->assertEquals('bazz', $object->foobar); @@ -165,7 +163,6 @@ public function testMergeParameterNonArray() public function testRegisterNotExistingExtension() { $container = new Container(['NotExistingExtension']); - $container->init(); } /** @@ -178,7 +175,6 @@ public function testRegisterNotExistingExtension() public function testRegisterNotImplementingExtension() { $container = new Container(['stdClass']); - $container->init(); } /** @@ -192,7 +188,6 @@ public function testUnknownUserConfig() $container = new Container([], [ 'not' => 'existing', ]); - $container->init(); } /** From 34b503111a7e81266206969dfdae2e8702644b18 Mon Sep 17 00:00:00 2001 From: dantleech Date: Wed, 29 Jun 2016 15:07:31 +0100 Subject: [PATCH 2/2] Updated README --- README.md | 30 ++++----- lib/Container.php | 118 ++++++++++++++++++----------------- lib/ExtensionInterface.php | 7 --- tests/Unit/ContainerTest.php | 11 ++-- 4 files changed, 78 insertions(+), 88 deletions(-) diff --git a/README.md b/README.md index ed30704..f17966e 100644 --- a/README.md +++ b/README.md @@ -4,12 +4,12 @@ PHPBench Service Container [![Build Status](https://travis-ci.org/phpbench/container.svg?branch=master)](https://travis-ci.org/phpbench/container) [![StyleCI](https://styleci.io/repos/55606670/shield)](https://styleci.io/repos/55606670) -This is a simple but powerful (of course its powerful) dependency injection container: +This is a simple but powerful dependency injection container: - Extendable (a.k.a service providers); - Configurable; +- Taggable services; - Extensions provide default configuration; -- Service tagging; This container was developed incidentally for PHPBench, and is not a very polished library, but I like it. @@ -18,7 +18,7 @@ Simple usage ------------ ```php -$container = new Container(); +$container = Container::create(); $container->register('foobar', function (Container $container) { return new \stdClass(); }); @@ -39,7 +39,6 @@ $container = new Container( 'foo.bar' => 'my_new_value', ] ); -$container->init(); // will trigger loading of the extensions. ``` ```php @@ -48,18 +47,25 @@ class MyExtension implements ExtensionInterface public function load(Container $container) { $container->register('my_service', function (Container $container) { - return new MyService( + $service = new MyService( $container->getParameter('foo_bar'), $container->get('some_other_service') ); + + // get all tagged services and add them to the instantiated object + foreach ($container->getServiceIdsForTag('my_tag') as $serviceId) { + $service->add($container->get($serviceId)); + } }); $container->register('tagged_service', function (Container $container) { - return new MyService( + $service = new MyService( $container->getParameter('foo_bar'), $container->get('some_other_service') ); - }, [ 'tag' => []); + + return $service; + }, [ 'my_tag' => [ 'attribute_1' => 'foo' ]); // optional tag attributes } /** @@ -73,16 +79,6 @@ class MyExtension implements ExtensionInterface 'foo_bar' => 'this is foo' ]; } - - /** - * Build the container. - */ - public function build(Container $container) - { - foreach ($container->getServiceIdsForTag() as $serviceId) { - $container->get('my_service')->add($container->get($serviceId)); - } - } } ``` diff --git a/lib/Container.php b/lib/Container.php index 025fc70..599a4f6 100644 --- a/lib/Container.php +++ b/lib/Container.php @@ -32,7 +32,66 @@ public function __construct(array $extensionClasses = [], array $userConfig = [] { $this->extensionClasses = $extensionClasses; $this->userConfig = $userConfig; - $this->init(); + } + + /** + * Configure the container. This method will call the `configure()` method + * on each extension. Extensions must use this opportunity to register their + * services and define any default config. + * + * This method must be called before `build()`. + */ + public function init() + { + $extensions = []; + + if (empty($this->extensionClasses) && empty($this->userConfig)) { + return; + } + + foreach ($this->extensionClasses as $extensionClass) { + if (!class_exists($extensionClass)) { + throw new \InvalidArgumentException(sprintf( + 'Extension class "%s" does not exist', + $extensionClass + )); + } + + $extension = new $extensionClass(); + + if (!$extension instanceof ExtensionInterface) { + throw new \InvalidArgumentException(sprintf( + // add any manually specified extensions + 'Extension "%s" must implement the PhpBench\\Extension interface', + get_class($extension) + )); + } + + $extensions[] = $extension; + + $this->config = array_merge( + $this->config, + $extension->getDefaultConfig() + ); + } + + $diff = array_diff(array_keys($this->userConfig), array_keys($this->config)); + + if ($diff) { + throw new \InvalidArgumentException(sprintf( + 'Unknown configuration keys: "%s". Permitted keys: "%s"', + implode('", "', $diff), implode('", "', array_keys($this->config)) + )); + } + + $this->config = array_merge( + $this->config, + $this->userConfig + ); + + foreach ($extensions as $extension) { + $extension->load($this); + } } /** @@ -177,61 +236,4 @@ public function hasParameter($name) { return array_key_exists($name, $this->config); } - - private function init() - { - $extensions = []; - - if (empty($this->extensionClasses) && empty($this->userConfig)) { - return; - } - - foreach ($this->extensionClasses as $extensionClass) { - if (!class_exists($extensionClass)) { - throw new \InvalidArgumentException(sprintf( - 'Extension class "%s" does not exist', - $extensionClass - )); - } - - $extension = new $extensionClass(); - - if (!$extension instanceof ExtensionInterface) { - throw new \InvalidArgumentException(sprintf( - // add any manually specified extensions - 'Extension "%s" must implement the PhpBench\\Extension interface', - get_class($extension) - )); - } - - $extensions[] = $extension; - - $this->config = array_merge( - $this->config, - $extension->getDefaultConfig() - ); - } - - $diff = array_diff(array_keys($this->userConfig), array_keys($this->config)); - - if ($diff) { - throw new \InvalidArgumentException(sprintf( - 'Unknown configuration keys: "%s". Permitted keys: "%s"', - implode('", "', $diff), implode('", "', array_keys($this->config)) - )); - } - - $this->config = array_merge( - $this->config, - $this->userConfig - ); - - foreach ($extensions as $extension) { - $extension->load($this); - } - - foreach ($extensions as $extension) { - $extension->build($this); - } - } } diff --git a/lib/ExtensionInterface.php b/lib/ExtensionInterface.php index 628a253..55066e9 100644 --- a/lib/ExtensionInterface.php +++ b/lib/ExtensionInterface.php @@ -26,11 +26,4 @@ public function load(Container $container); * @return array */ public function getDefaultConfig(); - - /** - * Called after all services in all extensions have been registered. - * - * @param Container $container - */ - public function build(Container $container); } diff --git a/tests/Unit/ContainerTest.php b/tests/Unit/ContainerTest.php index 4b51e78..1dec2cf 100644 --- a/tests/Unit/ContainerTest.php +++ b/tests/Unit/ContainerTest.php @@ -103,10 +103,10 @@ public function testRegisterExtension() __NAMESPACE__ . '\\TestExtension', ]); + $container->init(); $object = $container->get('foobar'); $this->assertInstanceOf('stdClass', $object); $this->assertEquals('bar', $object->foobar); - $this->assertEquals('barfoo', $object->setOnBuild); } /** @@ -123,6 +123,7 @@ public function testRegisterExtensionWithUserConfig() ] ); + $container->init(); $object = $container->get('foobar'); $this->assertInstanceOf('stdClass', $object); $this->assertEquals('bazz', $object->foobar); @@ -163,6 +164,7 @@ public function testMergeParameterNonArray() public function testRegisterNotExistingExtension() { $container = new Container(['NotExistingExtension']); + $container->init(); } /** @@ -175,6 +177,7 @@ public function testRegisterNotExistingExtension() public function testRegisterNotImplementingExtension() { $container = new Container(['stdClass']); + $container->init(); } /** @@ -188,6 +191,7 @@ public function testUnknownUserConfig() $container = new Container([], [ 'not' => 'existing', ]); + $container->init(); } /** @@ -221,9 +225,4 @@ public function load(Container $container) return $stdClass; }); } - - public function build(Container $container) - { - $container->get('foobar')->setOnBuild = 'barfoo'; - } }