[Provider] ProviderRegistry should return indexed provider arrays

Since Provider instances do not know the index or type for which they are responsible, it's helpful for the ProviderRegistry to include that information when returning multiple providers.
This commit is contained in:
Jeremy Mikola 2012-03-09 19:14:31 -05:00
parent e09225eb09
commit 118900120b
2 changed files with 31 additions and 22 deletions

View file

@ -34,15 +34,17 @@ class ProviderRegistry implements ContainerAwareInterface
/**
* Gets all registered providers.
*
* Providers will be indexed by "index/type" strings in the returned array.
*
* @return array of ProviderInterface instances
*/
public function getAllProviders()
{
$providers = array();
foreach ($this->providers as $indexProviders) {
foreach ($indexProviders as $providerId) {
$providers[] = $this->container->get($providerId);
foreach ($this->providers as $index => $indexProviders) {
foreach ($indexProviders as $type => $providerId) {
$providers[sprintf('%s/%s', $index, $type)] = $this->container->get($providerId);
}
}
@ -52,7 +54,9 @@ class ProviderRegistry implements ContainerAwareInterface
/**
* Gets all providers for an index.
*
* @param string $index
* Providers will be indexed by "type" strings in the returned array.
*
* @param string $index
* @return array of ProviderInterface instances
* @throws InvalidArgumentException if no providers were registered for the index
*/
@ -64,8 +68,8 @@ class ProviderRegistry implements ContainerAwareInterface
$providers = array();
foreach ($this->providers[$index] as $providerId) {
$providers[] = $this->container->get($providerId);
foreach ($this->providers[$index] as $type => $providerId) {
$providers[$type] = $this->container->get($providerId);
}
return $providers;

View file

@ -31,27 +31,32 @@ class ProviderRegistryTest extends \PHPUnit_Framework_TestCase
public function testGetAllProviders()
{
$this->assertEquals(array(
'provider.foo.a',
'provider.foo.b',
'provider.foo.c',
'provider.bar.a',
'provider.bar.b',
), $this->registry->getAllProviders());
$allProviders = array(
'foo/a' => 'provider.foo.a',
'foo/b' => 'provider.foo.b',
'foo/c' => 'provider.foo.c',
'bar/a' => 'provider.bar.a',
'bar/b' => 'provider.bar.b',
);
$this->assertEquals($allProviders, $this->registry->getAllProviders());
}
public function testGetIndexProviders()
{
$this->assertEquals(array(
'provider.foo.a',
'provider.foo.b',
'provider.foo.c',
), $this->registry->getIndexProviders('foo'));
$fooProviders = array(
'a' => 'provider.foo.a',
'b' => 'provider.foo.b',
'c' => 'provider.foo.c',
);
$this->assertEquals(array(
'provider.bar.a',
'provider.bar.b',
), $this->registry->getIndexProviders('bar'));
$barProviders = array(
'a' => 'provider.bar.a',
'b' => 'provider.bar.b',
);
$this->assertEquals($fooProviders, $this->registry->getIndexProviders('foo'));
$this->assertEquals($barProviders, $this->registry->getIndexProviders('bar'));
}
/**