From 416274ed60e4b2403a937d67c6fc42d37a1f7b61 Mon Sep 17 00:00:00 2001 From: Don Gilbert Date: Thu, 15 Aug 2013 18:12:57 -0500 Subject: [PATCH 1/2] Add support for ServiceProviders to the DI Container Update test to use mock properly --- src/Joomla/DI/Container.php | 14 +++++++++++++ src/Joomla/DI/ServiceProviderInterface.php | 14 +++++++++++++ src/Joomla/DI/Tests/ContainerTest.php | 23 ++++++++++++++++++++++ 3 files changed, 51 insertions(+) create mode 100644 src/Joomla/DI/ServiceProviderInterface.php diff --git a/src/Joomla/DI/Container.php b/src/Joomla/DI/Container.php index e1e9c29b5..c68d46562 100644 --- a/src/Joomla/DI/Container.php +++ b/src/Joomla/DI/Container.php @@ -340,5 +340,19 @@ public function getNewInstance($key) { return $this->get($key, true); } + + /** + * Register a service provider to the container. + * + * @param ServiceProviderInterface $provider + * + * @return Container This object for chaining. + */ + public function registerServiceProvider(ServiceProviderInterface $provider) + { + $provider->register($this); + + return $this; + } } diff --git a/src/Joomla/DI/ServiceProviderInterface.php b/src/Joomla/DI/ServiceProviderInterface.php new file mode 100644 index 000000000..7fe4bae1a --- /dev/null +++ b/src/Joomla/DI/ServiceProviderInterface.php @@ -0,0 +1,14 @@ +assertNotSame($this->fixture->getNewInstance('foo'), $this->fixture->getNewInstance('foo')); } + + /** + * Test registering a service provider. Make sure register get's called. + * + * @return void + * + * @since 1.0 + */ + public function testRegisterServiceProvider() + { + $mock = $this->getMock('Joomla\\DI\\ServiceProviderInterface'); + + $mock->expects($this->once()) + ->method('register'); + + $returned = $this->fixture->registerServiceProvider($mock); + + $this->assertSame( + $returned, + $this->fixture, + 'When registering a service provider, the container instance should be returned.' + ); + } } From c1ee1af838b665a54b0994cb68f3e8565f5ffb38 Mon Sep 17 00:00:00 2001 From: Don Gilbert Date: Thu, 15 Aug 2013 18:28:53 -0500 Subject: [PATCH 2/2] Add DocBlocks to the ServiceProviderInterface. Thanks @eddieajau Fix docblock on container --- src/Joomla/DI/Container.php | 2 ++ src/Joomla/DI/ServiceProviderInterface.php | 14 ++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/src/Joomla/DI/Container.php b/src/Joomla/DI/Container.php index c68d46562..0b1c894be 100644 --- a/src/Joomla/DI/Container.php +++ b/src/Joomla/DI/Container.php @@ -347,6 +347,8 @@ public function getNewInstance($key) * @param ServiceProviderInterface $provider * * @return Container This object for chaining. + * + * @since 1.0 */ public function registerServiceProvider(ServiceProviderInterface $provider) { diff --git a/src/Joomla/DI/ServiceProviderInterface.php b/src/Joomla/DI/ServiceProviderInterface.php index 7fe4bae1a..e1a7da363 100644 --- a/src/Joomla/DI/ServiceProviderInterface.php +++ b/src/Joomla/DI/ServiceProviderInterface.php @@ -8,7 +8,21 @@ namespace Joomla\DI; +/** + * Defines the interface for a Service Provider. + * + * @since 1.0 + */ interface ServiceProviderInterface { + /** + * Registers the service provider with a DI container. + * + * @param Container $container The DI container. + * + * @return Container Returns itself to support chaining. + * + * @since 1.0 + */ public function register(Container $container); }