Ampache PHP Cross Reference Groupware Applications

Source: /modules/Guzzle/Cache/CacheAdapterFactory.php - 117 lines - 4237 bytes - Summary - Text - Print

   1  <?php
   2  
   3  namespace Guzzle\Cache;
   4  
   5  use Doctrine\Common\Cache\Cache;
   6  use Guzzle\Common\Version;
   7  use Guzzle\Common\Exception\InvalidArgumentException;
   8  use Guzzle\Common\Exception\RuntimeException;
   9  use Guzzle\Common\FromConfigInterface;
  10  use Zend\Cache\Storage\StorageInterface;
  11  
  12  /**
  13   * Generates cache adapters from any number of known cache implementations
  14   */
  15  class CacheAdapterFactory implements FromConfigInterface
  16  {
  17      /**
  18       * Create a Guzzle cache adapter based on an array of options
  19       *
  20       * @param mixed $cache Cache value
  21       *
  22       * @return CacheAdapterInterface
  23       * @throws InvalidArgumentException
  24       */
  25      public static function fromCache($cache)
  26      {
  27          if (!is_object($cache)) {
  28              throw new InvalidArgumentException('Cache must be one of the known cache objects');
  29          }
  30  
  31          if ($cache instanceof CacheAdapterInterface) {
  32              return $cache;
  33          } elseif ($cache instanceof Cache) {
  34              return new DoctrineCacheAdapter($cache);
  35          } elseif ($cache instanceof StorageInterface) {
  36              return new Zf2CacheAdapter($cache);
  37          } else {
  38              throw new InvalidArgumentException('Unknown cache type: ' . get_class($cache));
  39          }
  40      }
  41  
  42      /**
  43       * Create a Guzzle cache adapter based on an array of options
  44       *
  45       * @param array $config Array of configuration options
  46       *
  47       * @return CacheAdapterInterface
  48       * @throws InvalidArgumentException
  49       * @deprecated This will be removed in a future version
  50       * @codeCoverageIgnore
  51       */
  52      public static function factory($config = array())
  53      {
  54          Version::warn(__METHOD__ . ' is deprecated');
  55          if (!is_array($config)) {
  56              throw new InvalidArgumentException('$config must be an array');
  57          }
  58  
  59          if (!isset($config['cache.adapter']) && !isset($config['cache.provider'])) {
  60              $config['cache.adapter'] = 'Guzzle\Cache\NullCacheAdapter';
  61              $config['cache.provider'] = null;
  62          } else {
  63              // Validate that the options are valid
  64              foreach (array('cache.adapter', 'cache.provider') as $required) {
  65                  if (!isset($config[$required])) {
  66                      throw new InvalidArgumentException("{$required} is a required CacheAdapterFactory option");
  67                  }
  68                  if (is_string($config[$required])) {
  69                      // Convert dot notation to namespaces
  70                      $config[$required] = str_replace('.', '\\', $config[$required]);
  71                      if (!class_exists($config[$required])) {
  72                          throw new InvalidArgumentException("{$config[$required]} is not a valid class for {$required}");
  73                      }
  74                  }
  75              }
  76              // Instantiate the cache provider
  77              if (is_string($config['cache.provider'])) {
  78                  $args = isset($config['cache.provider.args']) ? $config['cache.provider.args'] : null;
  79                  $config['cache.provider'] = self::createObject($config['cache.provider'], $args);
  80              }
  81          }
  82  
  83          // Instantiate the cache adapter using the provider and options
  84          if (is_string($config['cache.adapter'])) {
  85              $args = isset($config['cache.adapter.args']) ? $config['cache.adapter.args'] : array();
  86              array_unshift($args, $config['cache.provider']);
  87              $config['cache.adapter'] = self::createObject($config['cache.adapter'], $args);
  88          }
  89  
  90          return $config['cache.adapter'];
  91      }
  92  
  93      /**
  94       * Create a class using an array of constructor arguments
  95       *
  96       * @param string $className Class name
  97       * @param array  $args      Arguments for the class constructor
  98       *
  99       * @return mixed
 100       * @throws RuntimeException
 101       * @deprecated
 102       * @codeCoverageIgnore
 103       */
 104      private static function createObject($className, array $args = null)
 105      {
 106          try {
 107              if (!$args) {
 108                  return new $className;
 109              } else {
 110                  $c = new \ReflectionClass($className);
 111                  return $c->newInstanceArgs($args);
 112              }
 113          } catch (\Exception $e) {
 114              throw new RuntimeException($e->getMessage(), $e->getCode(), $e);
 115          }
 116      }
 117  }

title

Description

title

Description

title

Description

title

title

Body