1
0
mirror of https://github.com/guzzle/guzzle.git synced 2025-01-18 05:48:15 +01:00

[Service] Changing the ServiceBuilder::factory() method to accept a SimpleXMLElement or a string filename to create a ServiceBuilder. This will make it easier to integrate with other applications without requiring any special builders.

This commit is contained in:
Michael Dowling 2011-04-11 20:15:55 -05:00
parent 8395dca859
commit 4c2405ffed
2 changed files with 32 additions and 16 deletions

View File

@ -30,7 +30,8 @@ class ServiceBuilder implements \ArrayAccess
* Create a new ServiceBuilder using an XML configuration file to configure
* the registered ServiceBuilder builder objects
*
* @param string $filename Full path to the XML configuration file
* @param string|SimpleXMLElement $xml An instantiated SimpleXMLElement or
* the full path to a Guzzle XML configuration file
* @param CacheAdapterInterface $cacheAdapter (optional) Pass a cache
* adapter to cache the XML service configuration settings
* @param int $ttl (optional) How long to cache the parsed XML data
@ -39,24 +40,25 @@ class ServiceBuilder implements \ArrayAccess
* @throws RuntimeException if the file cannot be openend
* @throws LogicException when trying to extend a missing client
*/
public static function factory($filename, CacheAdapterInterface $cacheAdapter = null, $ttl = 86400)
public static function factory($xml, CacheAdapterInterface $cacheAdapter = null, $ttl = 86400)
{
// Compute the cache key for this service and check if it exists in cache
if ($cacheAdapter) {
$key = 'guz_service_' . md5($filename);
$cached = $cacheAdapter ? $cacheAdapter->fetch($key) : false;
if ($cached) {
return new self(unserialize($cached));
if (is_string($xml)) {
if ($cacheAdapter) {
// Compute the cache key for this service and check if it exists in cache
$key = str_replace('__', '_', 'guz_' . preg_replace('~[^\\pL\d]+~u', '_', strtolower(realpath($xml))));
if ($cached = $cacheAdapter->fetch($key)) {
return new self(unserialize($cached));
}
}
// Build the service config from the XML file if the file exists
if (is_file($xml)) {
$xml = new \SimpleXMLElement($xml, null, true);
} else {
throw new \RuntimeException('Unable to open service configuration file ' . $xml);
}
}
// Build the service config from the XML file if the file exists
if (!is_file($filename)) {
throw new \RuntimeException('Unable to open service configuration file ' . $filename);
}
$config = array();
$xml = new \SimpleXMLElement($filename, null, true);
// Create a client entry for each client in the XML file
foreach ($xml->clients->client as $client) {

View File

@ -116,8 +116,11 @@ EOT;
$s1 = ServiceBuilder::factory($this->tempFile, $adapter, 86400);
// Make sure it added to the cache
$this->assertNotEmpty($cache->getIds());
// Make sure it added to the cache with a proper cache key
$keys = $cache->getIds();
$this->assertNotEmpty($keys);
$this->assertEquals(0, strpos($keys[0], 'guz_'));
$this->assertFalse(strpos($keys[0], '__'));
// Load this one from cache
$s2 = ServiceBuilder::factory($this->tempFile, $adapter, 86400);
@ -228,4 +231,15 @@ EOT;
$b['michael.mock'] = new Client('http://www.test.com/');
$this->assertType('Guzzle\\Service\\Client', $b['michael.mock']);
}
/**
* @covers Guzzle\Service\ServiceBuilder::factory
*/
public function testFactoryCanCreateFromXml()
{
$b = ServiceBuilder::factory(new \SimpleXMLElement($this->xmlConfig));
$this->assertTrue($b->offsetExists('michael.mock'));
$this->assertTrue($b->offsetExists('billy.mock'));
$this->assertTrue($b->offsetExists('billy.testing'));
}
}