Permalink
Browse files

[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.
  • Loading branch information...
1 parent e09225e commit 118900120b6b4655dfbcc11f7032218648b3c564 @jmikola jmikola committed Mar 10, 2012
Showing with 31 additions and 22 deletions.
  1. +10 −6 Provider/ProviderRegistry.php
  2. +21 −16 Tests/Provider/ProviderRegistryTest.php
@@ -34,15 +34,17 @@ public function addProvider($index, $type, $providerId)
/**
* 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 @@ public function getAllProviders()
/**
* 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 @@ public function getIndexProviders($index)
$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;
@@ -31,27 +31,32 @@ public function setUp()
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',
+ );
+
+ $barProviders = array(
+ 'a' => 'provider.bar.a',
+ 'b' => 'provider.bar.b',
+ );
- $this->assertEquals(array(
- 'provider.bar.a',
- 'provider.bar.b',
- ), $this->registry->getIndexProviders('bar'));
+ $this->assertEquals($fooProviders, $this->registry->getIndexProviders('foo'));
+ $this->assertEquals($barProviders, $this->registry->getIndexProviders('bar'));
}
/**

0 comments on commit 1189001

Please sign in to comment.