Skip to content
This repository has been archived by the owner on Nov 26, 2017. It is now read-only.

Commit

Permalink
Merge pull request #194 from dongilbert/DI
Browse files Browse the repository at this point in the history
Add support for ServiceProviders to the DI Container
  • Loading branch information
eddieajau committed Aug 15, 2013
2 parents 0fd4ba2 + c1ee1af commit 02bfb3b
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/Joomla/DI/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -340,5 +340,21 @@ 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.
*
* @since 1.0
*/
public function registerServiceProvider(ServiceProviderInterface $provider)
{
$provider->register($this);

return $this;
}
}

28 changes: 28 additions & 0 deletions src/Joomla/DI/ServiceProviderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* Part of the Joomla Framework DI Package
*
* @copyright Copyright (C) 2013 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/

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);
}
23 changes: 23 additions & 0 deletions src/Joomla/DI/Tests/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -735,4 +735,27 @@ public function testGetNewInstance()

$this->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.'
);
}
}

0 comments on commit 02bfb3b

Please sign in to comment.