Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a 'signature' method to ServiceProviderInterface #93

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/ServiceProvider/AbstractServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,12 @@ public function provides($alias = null)

return $this->provides;
}

/**
* {@inheritdoc}
*/
public function signature()
{
return get_class($this);
}
}
4 changes: 2 additions & 2 deletions src/ServiceProvider/ServiceProviderAggregate.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ public function register($service)
$provider = $this->providers[$service];

// ensure that the provider hasn't already been invoked by any other service request
if (in_array(get_class($provider), $this->registered)) {
if (in_array($provider->signature(), $this->registered)) {
return;
}

$provider->register();

$this->registered[] = get_class($provider);
$this->registered[] = $provider->signature();
}
}
9 changes: 9 additions & 0 deletions src/ServiceProvider/ServiceProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,13 @@ public function provides($service = null);
* @return void
*/
public function register();

/**
* The signature of the service provider uniquely identifies it, so
* that we can quickly determine if it has already been registered.
* Defaults to get_class($provider).
*
* @return string
*/
public function signature();
}
13 changes: 12 additions & 1 deletion tests/Asset/SharedServiceProviderFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,22 @@ class SharedServiceProviderFake extends AbstractServiceProvider
*/
private $item;

/**
* @var string
*/
private $signature;

/**
* @param string $alias
* @param mixed $item
*/
public function __construct($alias, $item)
public function __construct($alias, $item, $signature = false)
{
$this->alias = $alias;
$this->item = $item;

$this->provides[] = $alias;
$this->signature = $signature ?: get_class($this);
}

public function register()
Expand All @@ -36,4 +42,9 @@ public function register()

return true;
}

public function signature()
{
return $this->signature;
}
}
45 changes: 45 additions & 0 deletions tests/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,51 @@ public function testGetReturnsSharedItemFromServiceProvider()
$this->assertSame($item, $container->get($alias));
}

/**
* Asserts that the same service provider class cannot be used to
* register two different sets of services.
*/
public function testSameServiceProviderClassCannotBeUsedTwice()
{
$alias = 'foo';
$item = new \stdClass;

$alias2 = 'bar';
$item2 = new \stdClass;

$container = new Container;
$container->addServiceProvider(new Asset\SharedServiceProviderFake($alias, $item));
$container->addServiceProvider(new Asset\SharedServiceProviderFake($alias2, $item2));

$this->assertSame($item, $container->get($alias));

$this->setExpectedException('League\Container\Exception\NotFoundException');

$container->get($alias2);
}

/**
* Asserts that the same service provider class cannot be used to
* register two different sets of services.
*/
public function testSameServiceProviderClassCanBeUsedTwiceWithDifferentSignatures()
{
$alias = 'foo';
$item = new \stdClass;
$signature1 = 'foo';

$alias2 = 'bar';
$item2 = new \stdClass;
$signature2 = 'bar';

$container = new Container;
$container->addServiceProvider(new Asset\SharedServiceProviderFake($alias, $item, $signature1));
$container->addServiceProvider(new Asset\SharedServiceProviderFake($alias2, $item2, $signature2));

$this->assertSame($item, $container->get($alias));
$this->assertSame($item2, $container->get($alias2));
}

/**
* Asserts that the container to which is delegated can resolve items from the delegating container.
*/
Expand Down