CacheAdapterFactory.php 4.1 KB

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