diff options
Diffstat (limited to 'vendor/guzzle/guzzle/src/Guzzle/Service/Builder')
3 files changed, 318 insertions, 0 deletions
diff --git a/vendor/guzzle/guzzle/src/Guzzle/Service/Builder/ServiceBuilder.php b/vendor/guzzle/guzzle/src/Guzzle/Service/Builder/ServiceBuilder.php new file mode 100644 index 0000000..38150db --- /dev/null +++ b/vendor/guzzle/guzzle/src/Guzzle/Service/Builder/ServiceBuilder.php @@ -0,0 +1,189 @@ +<?php + +namespace Guzzle\Service\Builder; + +use Guzzle\Common\AbstractHasDispatcher; +use Guzzle\Service\ClientInterface; +use Guzzle\Service\Exception\ServiceBuilderException; +use Guzzle\Service\Exception\ServiceNotFoundException; +use Symfony\Component\EventDispatcher\EventSubscriberInterface; + +/** + * {@inheritdoc} + * + * Clients and data can be set, retrieved, and removed by accessing the service builder like an associative array. + */ +class ServiceBuilder extends AbstractHasDispatcher implements ServiceBuilderInterface, \ArrayAccess, \Serializable +{ + /** @var array Service builder configuration data */ + protected $builderConfig = array(); + + /** @var array Instantiated client objects */ + protected $clients = array(); + + /** @var ServiceBuilderLoader Cached instance of the service builder loader */ + protected static $cachedFactory; + + /** @var array Plugins to attach to each client created by the service builder */ + protected $plugins = array(); + + /** + * Create a new ServiceBuilder using configuration data sourced from an + * array, .js|.json or .php file. + * + * @param array|string $config The full path to an .json|.js or .php file, or an associative array + * @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 = array()) + { + // @codeCoverageIgnoreStart + if (!static::$cachedFactory) { + static::$cachedFactory = new ServiceBuilderLoader(); + } + // @codeCoverageIgnoreEnd + + return self::$cachedFactory->load($config, $globalParameters); + } + + /** + * @param array $serviceBuilderConfig Service configuration settings: + * - name: Name of the service + * - class: Client class to instantiate using a factory method + * - params: array of key value pair configuration settings for the builder + */ + public function __construct(array $serviceBuilderConfig = array()) + { + $this->builderConfig = $serviceBuilderConfig; + } + + public static function getAllEvents() + { + return array('service_builder.create_client'); + } + + public function unserialize($serialized) + { + $this->builderConfig = json_decode($serialized, true); + } + + public function serialize() + { + return json_encode($this->builderConfig); + } + + /** + * Attach a plugin to every client created by the builder + * + * @param EventSubscriberInterface $plugin Plugin to attach to each client + * + * @return self + */ + public function addGlobalPlugin(EventSubscriberInterface $plugin) + { + $this->plugins[] = $plugin; + + return $this; + } + + /** + * Get data from the service builder without triggering the building of a service + * + * @param string $name Name of the service to retrieve + * + * @return array|null + */ + public function getData($name) + { + return isset($this->builderConfig[$name]) ? $this->builderConfig[$name] : null; + } + + public function get($name, $throwAway = false) + { + if (!isset($this->builderConfig[$name])) { + + // Check to see if arbitrary data is being referenced + if (isset($this->clients[$name])) { + return $this->clients[$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); + } + + if (!$throwAway && isset($this->clients[$name])) { + return $this->clients[$name]; + } + + $builder =& $this->builderConfig[$name]; + + // Convert references to the actual client + foreach ($builder['params'] as &$v) { + if (is_string($v) && substr($v, 0, 1) == '{' && substr($v, -1) == '}') { + $v = $this->get(trim($v, '{} ')); + } + } + + // Get the configured parameters and merge in any parameters provided for throw-away clients + $config = $builder['params']; + if (is_array($throwAway)) { + $config = $throwAway + $config; + } + + $client = $builder['class']::factory($config); + + if (!$throwAway) { + $this->clients[$name] = $client; + } + + if ($client instanceof ClientInterface) { + foreach ($this->plugins as $plugin) { + $client->addSubscriber($plugin); + } + // Dispatch an event letting listeners know a client was created + $this->dispatch('service_builder.create_client', array('client' => $client)); + } + + return $client; + } + + public function set($key, $service) + { + if (is_array($service) && isset($service['class']) && isset($service['params'])) { + $this->builderConfig[$key] = $service; + } else { + $this->clients[$key] = $service; + } + + return $this; + } + + public function offsetSet($offset, $value) + { + $this->set($offset, $value); + } + + public function offsetUnset($offset) + { + unset($this->builderConfig[$offset]); + unset($this->clients[$offset]); + } + + public function offsetExists($offset) + { + return isset($this->builderConfig[$offset]) || isset($this->clients[$offset]); + } + + public function offsetGet($offset) + { + return $this->get($offset); + } +} diff --git a/vendor/guzzle/guzzle/src/Guzzle/Service/Builder/ServiceBuilderInterface.php b/vendor/guzzle/guzzle/src/Guzzle/Service/Builder/ServiceBuilderInterface.php new file mode 100644 index 0000000..4fc310a --- /dev/null +++ b/vendor/guzzle/guzzle/src/Guzzle/Service/Builder/ServiceBuilderInterface.php @@ -0,0 +1,40 @@ +<?php + +namespace Guzzle\Service\Builder; + +use Guzzle\Service\Exception\ServiceNotFoundException; + +/** + * Service builder used to store and build clients or arbitrary data. Client configuration data can be supplied to tell + * the service builder how to create and cache {@see \Guzzle\Service\ClientInterface} objects. Arbitrary data can be + * supplied and accessed from a service builder. Arbitrary data and other clients can be referenced by name in client + * configuration arrays to make them input for building other clients (e.g. "{key}"). + */ +interface ServiceBuilderInterface +{ + /** + * Get a ClientInterface object or arbitrary data from the service builder + * + * @param string $name Name of the registered service or data to retrieve + * @param bool|array $throwAway Only pertains to retrieving client objects built using a configuration array. + * Set to TRUE to not store the client for later retrieval from the ServiceBuilder. + * If an array is specified, that data will overwrite the configured params of the + * client if the client implements {@see \Guzzle\Common\FromConfigInterface} and will + * not store the client for later retrieval. + * + * @return \Guzzle\Service\ClientInterface|mixed + * @throws ServiceNotFoundException when a client or data cannot be found by the given name + */ + public function get($name, $throwAway = false); + + /** + * Register a service or arbitrary data by name with the service builder + * + * @param string $key Name of the client or data to register + * @param mixed $service Client configuration array or arbitrary data to register. The client configuration array + * must include a 'class' (string) and 'params' (array) key. + * + * @return ServiceBuilderInterface + */ + public function set($key, $service); +} diff --git a/vendor/guzzle/guzzle/src/Guzzle/Service/Builder/ServiceBuilderLoader.php b/vendor/guzzle/guzzle/src/Guzzle/Service/Builder/ServiceBuilderLoader.php new file mode 100644 index 0000000..c561a3d --- /dev/null +++ b/vendor/guzzle/guzzle/src/Guzzle/Service/Builder/ServiceBuilderLoader.php @@ -0,0 +1,89 @@ +<?php + +namespace Guzzle\Service\Builder; + +use Guzzle\Service\AbstractConfigLoader; +use Guzzle\Service\Exception\ServiceNotFoundException; + +/** + * Service builder config loader + */ +class ServiceBuilderLoader extends AbstractConfigLoader +{ + protected function build($config, array $options) + { + // A service builder class can be specified in the class field + $class = !empty($config['class']) ? $config['class'] : __NAMESPACE__ . '\\ServiceBuilder'; + + // Account for old style configs that do not have a services array + $services = isset($config['services']) ? $config['services'] : $config; + + // Validate the configuration and handle extensions + foreach ($services as $name => &$service) { + + $service['params'] = isset($service['params']) ? $service['params'] : array(); + + // Check if this client builder extends another client + if (!empty($service['extends'])) { + + // Make sure that the service it's extending has been defined + if (!isset($services[$service['extends']])) { + throw new ServiceNotFoundException( + "{$name} is trying to extend a non-existent service: {$service['extends']}" + ); + } + + $extended = &$services[$service['extends']]; + + // Use the correct class attribute + if (empty($service['class'])) { + $service['class'] = isset($extended['class']) ? $extended['class'] : ''; + } + if ($extendsParams = isset($extended['params']) ? $extended['params'] : false) { + $service['params'] = $service['params'] + $extendsParams; + } + } + + // Overwrite default values with global parameter values + if (!empty($options)) { + $service['params'] = $options + $service['params']; + } + + $service['class'] = isset($service['class']) ? $service['class'] : ''; + } + + return new $class($services); + } + + protected function mergeData(array $a, array $b) + { + $result = $b + $a; + + // Merge services using a recursive union of arrays + if (isset($a['services']) && $b['services']) { + + // Get a union of the services of the two arrays + $result['services'] = $b['services'] + $a['services']; + + // Merge each service in using a union of the two arrays + foreach ($result['services'] as $name => &$service) { + + // By default, services completely override a previously defined service unless it extends itself + if (isset($a['services'][$name]['extends']) + && isset($b['services'][$name]['extends']) + && $b['services'][$name]['extends'] == $name + ) { + $service += $a['services'][$name]; + // Use the `extends` attribute of the parent + $service['extends'] = $a['services'][$name]['extends']; + // Merge parameters using a union if both have parameters + if (isset($a['services'][$name]['params'])) { + $service['params'] += $a['services'][$name]['params']; + } + } + } + } + + return $result; + } +} |