/** * Create a new ServiceBuilder using configuration data sourced from an * array, .json|.js file, SimpleXMLElement, or .xml file. * * @param array|string|\SimpleXMLElement $data An instantiated * SimpleXMLElement containing configuration data, the full path to an * .xml or .js|.json file, or an associative array of data * @param array $globalParameters Array of global parameters to * pass to every service as it is instantiated. * * @return ServiceBuilderInterface * @throws ServiceBuilderException if a file cannot be opened * @throws ServiceNotFoundException when trying to extend a missing client */ public static function factory($config = null, array $globalParameters = null) { // @codeCoverageIgnoreStart if (!self::$defaultFactory) { self::$defaultFactory = new ServiceBuilderAbstractFactory(); } // @codeCoverageIgnoreEnd return self::$defaultFactory->build($config, $globalParameters); }
/** * Parse an XML document into an array * * @param string $filename Path to the file * * @return array */ protected function parseXmlFile($filename) { $result = array('services' => array()); $xml = $filename instanceof \SimpleXMLElement ? $filename : new \SimpleXMLElement($filename, null, true); // Account for old style service builder config files $services = isset($xml->services) ? $xml->services->service : $xml->clients->client; foreach ($services as $service) { $row = array(); foreach ($service->param as $param) { $row[(string) $param->attributes()->name] = (string) $param->attributes()->value; } $result['services'][(string) $service->attributes()->name] = array('class' => (string) $service->attributes()->class, 'extends' => (string) $service->attributes()->extends, 'params' => $row); } // Include any XML files under the includes elements if (isset($xml->includes->include)) { // You can only extend other services when using a file if ($filename instanceof \SimpleXMLElement) { throw new ServiceBuilderException('You can not extend other SimpleXMLElement services'); } foreach ($xml->includes->include as $include) { $path = (string) $include->attributes()->path; if ($path[0] != DIRECTORY_SEPARATOR) { $path = dirname($filename) . DIRECTORY_SEPARATOR . $path; } // Merge the two configuration files together using union merges $result = ServiceBuilderAbstractFactory::combineConfigs($this->parseXmlFile($path), $result); } } // Grab the class name if it's set if (isset($xml->class)) { $result['class'] = str_replace('.', '\\', (string) $xml->class); } return $result; }
/** * {@inheritdoc} * Adds special handling for JSON configuration merging */ protected function mergeJson(array $jsonA, array $jsonB) { return ServiceBuilderAbstractFactory::combineConfigs($jsonA, $jsonB); }
/** * @expectedException Guzzle\Service\Exception\ServiceBuilderException * @expectedExceptionMessage Must pass a file name, array, or SimpleXMLElement */ public function testThrowsExceptionWhenUnknownTypeIsPassed() { $factory = new ServiceBuilderAbstractFactory(); $factory->build(new \stdClass()); }
/** * @dataProvider configProvider */ public function testCombinesConfigs($a, $b, $c) { $this->assertEquals($c, ServiceBuilderAbstractFactory::combineConfigs($a, $b)); }