1
0
mirror of https://github.com/guzzle/guzzle.git synced 2025-02-25 02:22:57 +01:00

Adding alias support to service builders

This commit is contained in:
Michael Dowling 2013-01-19 19:03:06 -08:00
parent 6fa131581a
commit 2d2637ce11
2 changed files with 13 additions and 0 deletions

View File

@ -128,6 +128,12 @@ class ServiceBuilder extends AbstractHasDispatcher implements ServiceBuilderInte
public function get($name, $throwAway = false)
{
if (!isset($this->builderConfig[$name])) {
// Check aliases and return a match if found
foreach ($this->builderConfig as $actualName => $config) {
if (isset($config['alias']) && $config['alias'] == $name) {
return $this->get($actualName, $throwAway);
}
}
throw new ServiceNotFoundException('No service is registered as ' . $name);
}

View File

@ -21,6 +21,7 @@ class ServiceBuilderTest extends \Guzzle\Tests\GuzzleTestCase
),
),
'billy.mock' => array(
'alias' => 'Hello!',
'class' => 'Guzzle\Tests\Service\Mock\MockClient',
'params' => array(
'username' => 'billy',
@ -294,4 +295,10 @@ class ServiceBuilderTest extends \Guzzle\Tests\GuzzleTestCase
$this->assertEquals($this->arrayData['michael.mock'], $b->getData('michael.mock'));
$this->assertNull($b->getData('ewofweoweofe'));
}
public function testCanGetByAlias()
{
$b = new ServiceBuilder($this->arrayData);
$this->assertSame($b->get('billy.mock'), $b->get('Hello!'));
}
}