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

[Service] Adding ArrayAccess interface to ServiceBuilder

This commit is contained in:
Michael Dowling 2011-04-07 09:54:43 -05:00
parent e1301deaa9
commit ba29bb3820
2 changed files with 77 additions and 1 deletions

View File

@ -14,7 +14,7 @@ use Guzzle\Common\Cache\CacheAdapterInterface;
*
* @author michael@guzzlephp.org
*/
class ServiceBuilder
class ServiceBuilder implements \ArrayAccess
{
/**
* @var array Service builder configuration data
@ -134,4 +134,59 @@ class ServiceBuilder
return $client;
}
/**
* Register a client by name with the service builder
*
* @param string $offset Name of the client to register
* @param Client $value Client to register
*
* @return ServiceBuilder
*/
public function offsetSet($offset, $value)
{
$this->builderConfig[$offset] = $value;
return $this;
}
/**
* Remove a registered client by name
*
* @param string $offset Client to remove by name
*
* @return ServiceBuilder
*/
public function offsetUnset($offset)
{
if (isset($this->builderConfig[$offset])) {
unset($this->builderConfig[$offset]);
}
return $this;
}
/**
* Check if a client is registered with the service builder by name
*
* @param string $offset Name to check to see if a client exists
*
* @return bool
*/
public function offsetExists($offset)
{
return isset($this->builderConfig[$offset]);
}
/**
* Get a registered client by name
*
* @param string $offset Registered client name to retrieve
*
* @return Client
*/
public function offsetGet($offset)
{
return $this->get($offset);
}
}

View File

@ -9,6 +9,7 @@ namespace Guzzle\Tests\Service;
use Doctrine\Common\Cache\ArrayCache;
use Guzzle\Common\Cache\DoctrineCacheAdapter;
use Guzzle\Service\ServiceBuilder;
use Guzzle\Service\Client;
/**
* @author Michael Dowling <michael@guzzlephp.org>
@ -207,4 +208,24 @@ EOT;
$c = $s->get('michael.mock');
$this->assertType('Guzzle\\Tests\\Service\\Mock\\MockClient', $c);
}
/**
* @covers Guzzle\Service\ServiceBuilder::offsetSet
* @covers Guzzle\Service\ServiceBuilder::offsetGet
* @covers Guzzle\Service\ServiceBuilder::offsetUnset
* @covers Guzzle\Service\ServiceBuilder::offsetExists
*/
public function testUsedAsArray()
{
$b = ServiceBuilder::factory($this->tempFile);
$this->assertTrue($b->offsetExists('michael.mock'));
$this->assertFalse($b->offsetExists('not_there'));
$this->assertType('Guzzle\\Service\\Client', $b['michael.mock']);
unset($b['michael.mock']);
$this->assertFalse($b->offsetExists('michael.mock'));
$b['michael.mock'] = new Client('http://www.test.com/');
$this->assertType('Guzzle\\Service\\Client', $b['michael.mock']);
}
}