Open Journal Systems  3.3.0
CacheAdapterFactory.php
1 <?php
2 
3 namespace Guzzle\Cache;
4 
5 use Doctrine\Common\Cache\Cache;
10 use Zend\Cache\Storage\StorageInterface;
11 
16 {
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 
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 
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 }
Guzzle\Cache\CacheAdapterInterface
Definition: CacheAdapterInterface.php:12
Guzzle\Cache\Zf2CacheAdapter
Definition: Zf2CacheAdapter.php:12
Guzzle\Cache\CacheAdapterFactory\factory
static factory($config=array())
Definition: CacheAdapterFactory.php:52
Guzzle\Cache\DoctrineCacheAdapter
Definition: DoctrineCacheAdapter.php:12
Guzzle\Common\Version\warn
static warn($message)
Definition: Version.php:23
Guzzle\Cache\CacheAdapterFactory\fromCache
static fromCache($cache)
Definition: CacheAdapterFactory.php:25
Guzzle\Common\Exception\InvalidArgumentException
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Common/Exception/InvalidArgumentException.php:5
Guzzle\Common\Version
Definition: Version.php:8
Guzzle\Cache\CacheAdapterFactory
Definition: CacheAdapterFactory.php:15
Guzzle\Cache
Definition: AbstractCacheAdapter.php:3
Guzzle\Common\FromConfigInterface
Definition: FromConfigInterface.php:8
Guzzle\Common\Exception\RuntimeException
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Common/Exception/RuntimeException.php:5