Ampache PHP Cross Reference Groupware Applications

Source: /modules/Guzzle/Service/Builder/ServiceBuilder.php - 189 lines - 5913 bytes - Summary - Text - Print

   1  <?php
   2  
   3  namespace Guzzle\Service\Builder;
   4  
   5  use Guzzle\Common\AbstractHasDispatcher;
   6  use Guzzle\Service\ClientInterface;
   7  use Guzzle\Service\Exception\ServiceBuilderException;
   8  use Guzzle\Service\Exception\ServiceNotFoundException;
   9  use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10  
  11  /**
  12   * {@inheritdoc}
  13   *
  14   * Clients and data can be set, retrieved, and removed by accessing the service builder like an associative array.
  15   */
  16  class ServiceBuilder extends AbstractHasDispatcher implements ServiceBuilderInterface, \ArrayAccess, \Serializable
  17  {
  18      /** @var array Service builder configuration data */
  19      protected $builderConfig = array();
  20  
  21      /** @var array Instantiated client objects */
  22      protected $clients = array();
  23  
  24      /** @var ServiceBuilderLoader Cached instance of the service builder loader */
  25      protected static $cachedFactory;
  26  
  27      /** @var array Plugins to attach to each client created by the service builder */
  28      protected $plugins = array();
  29  
  30      /**
  31       * Create a new ServiceBuilder using configuration data sourced from an
  32       * array, .js|.json or .php file.
  33       *
  34       * @param array|string $config           The full path to an .json|.js or .php file, or an associative array
  35       * @param array        $globalParameters Array of global parameters to pass to every service as it is instantiated.
  36       *
  37       * @return ServiceBuilderInterface
  38       * @throws ServiceBuilderException if a file cannot be opened
  39       * @throws ServiceNotFoundException when trying to extend a missing client
  40       */
  41      public static function factory($config = null, array $globalParameters = array())
  42      {
  43          // @codeCoverageIgnoreStart
  44          if (!static::$cachedFactory) {
  45              static::$cachedFactory = new ServiceBuilderLoader();
  46          }
  47          // @codeCoverageIgnoreEnd
  48  
  49          return self::$cachedFactory->load($config, $globalParameters);
  50      }
  51  
  52      /**
  53       * @param array $serviceBuilderConfig Service configuration settings:
  54       *     - name: Name of the service
  55       *     - class: Client class to instantiate using a factory method
  56       *     - params: array of key value pair configuration settings for the builder
  57       */
  58      public function __construct(array $serviceBuilderConfig = array())
  59      {
  60          $this->builderConfig = $serviceBuilderConfig;
  61      }
  62  
  63      public static function getAllEvents()
  64      {
  65          return array('service_builder.create_client');
  66      }
  67  
  68      public function unserialize($serialized)
  69      {
  70          $this->builderConfig = json_decode($serialized, true);
  71      }
  72  
  73      public function serialize()
  74      {
  75          return json_encode($this->builderConfig);
  76      }
  77  
  78      /**
  79       * Attach a plugin to every client created by the builder
  80       *
  81       * @param EventSubscriberInterface $plugin Plugin to attach to each client
  82       *
  83       * @return self
  84       */
  85      public function addGlobalPlugin(EventSubscriberInterface $plugin)
  86      {
  87          $this->plugins[] = $plugin;
  88  
  89          return $this;
  90      }
  91  
  92      /**
  93       * Get data from the service builder without triggering the building of a service
  94       *
  95       * @param string $name Name of the service to retrieve
  96       *
  97       * @return array|null
  98       */
  99      public function getData($name)
 100      {
 101          return isset($this->builderConfig[$name]) ? $this->builderConfig[$name] : null;
 102      }
 103  
 104      public function get($name, $throwAway = false)
 105      {
 106          if (!isset($this->builderConfig[$name])) {
 107  
 108              // Check to see if arbitrary data is being referenced
 109              if (isset($this->clients[$name])) {
 110                  return $this->clients[$name];
 111              }
 112  
 113              // Check aliases and return a match if found
 114              foreach ($this->builderConfig as $actualName => $config) {
 115                  if (isset($config['alias']) && $config['alias'] == $name) {
 116                      return $this->get($actualName, $throwAway);
 117                  }
 118              }
 119              throw new ServiceNotFoundException('No service is registered as ' . $name);
 120          }
 121  
 122          if (!$throwAway && isset($this->clients[$name])) {
 123              return $this->clients[$name];
 124          }
 125  
 126          $builder =& $this->builderConfig[$name];
 127  
 128          // Convert references to the actual client
 129          foreach ($builder['params'] as &$v) {
 130              if (is_string($v) && substr($v, 0, 1) == '{' && substr($v, -1) == '}') {
 131                  $v = $this->get(trim($v, '{} '));
 132              }
 133          }
 134  
 135          // Get the configured parameters and merge in any parameters provided for throw-away clients
 136          $config = $builder['params'];
 137          if (is_array($throwAway)) {
 138              $config = $throwAway + $config;
 139          }
 140  
 141          $client = $builder['class']::factory($config);
 142  
 143          if (!$throwAway) {
 144              $this->clients[$name] = $client;
 145          }
 146  
 147          if ($client instanceof ClientInterface) {
 148              foreach ($this->plugins as $plugin) {
 149                  $client->addSubscriber($plugin);
 150              }
 151              // Dispatch an event letting listeners know a client was created
 152              $this->dispatch('service_builder.create_client', array('client' => $client));
 153          }
 154  
 155          return $client;
 156      }
 157  
 158      public function set($key, $service)
 159      {
 160          if (is_array($service) && isset($service['class']) && isset($service['params'])) {
 161              $this->builderConfig[$key] = $service;
 162          } else {
 163              $this->clients[$key] = $service;
 164          }
 165  
 166          return $this;
 167      }
 168  
 169      public function offsetSet($offset, $value)
 170      {
 171          $this->set($offset, $value);
 172      }
 173  
 174      public function offsetUnset($offset)
 175      {
 176          unset($this->builderConfig[$offset]);
 177          unset($this->clients[$offset]);
 178      }
 179  
 180      public function offsetExists($offset)
 181      {
 182          return isset($this->builderConfig[$offset]) || isset($this->clients[$offset]);
 183      }
 184  
 185      public function offsetGet($offset)
 186      {
 187          return $this->get($offset);
 188      }
 189  }

title

Description

title

Description

title

Description

title

title

Body