Overview

Namespaces

  • Contrib
    • Bundle
      • CoverallsBundle
        • Console
        • Entity
      • CoverallsV1Bundle
        • Api
        • Collector
        • Command
        • Config
        • Entity
          • Git
    • Component
      • File
      • Log
      • System
        • Git
  • Guzzle
    • Batch
      • Exception
    • Cache
    • Common
      • Exception
    • Http
      • Curl
      • Exception
      • Message
      • QueryAggregator
    • Inflection
    • Iterator
    • Log
    • Parser
      • Cookie
      • Message
      • UriTemplate
      • Url
    • Plugin
      • Async
      • Backoff
      • Cache
      • Cookie
        • CookieJar
        • Exception
      • CurlAuth
      • ErrorResponse
        • Exception
      • History
      • Log
      • Md5
      • Mock
      • Oauth
    • Service
      • Builder
      • Command
        • Factory
        • LocationVisitor
          • Request
          • Response
      • Description
      • Exception
      • Resource
    • Stream
  • PHP
  • Psr
    • Log
  • Symfony
    • Component
      • Config
        • Definition
          • Builder
          • Exception
        • Exception
        • Loader
        • Resource
        • Util
      • Console
        • Command
        • Formatter
        • Helper
        • Input
        • Output
        • Tester
      • EventDispatcher
        • Debug
      • Finder
        • Adapter
        • Comparator
        • Exception
        • Expression
        • Iterator
        • Shell
      • Stopwatch
      • Yaml
        • Exception

Classes

  • AbstractCacheAdapter
  • CacheAdapterFactory
  • ClosureCacheAdapter
  • DoctrineCacheAdapter
  • NullCacheAdapter
  • Zf1CacheAdapter
  • Zf2CacheAdapter

Interfaces

  • CacheAdapterInterface
  • Overview
  • Namespace
  • Class
  • Tree
  • Todo
 1: <?php
 2: 
 3: namespace Guzzle\Cache;
 4: 
 5: use Guzzle\Common\FromConfigInterface;
 6: use Guzzle\Common\Exception\InvalidArgumentException;
 7: use Guzzle\Common\Exception\RuntimeException;
 8: 
 9: /**
10:  * Generates cache adapters and cache providers objects using an array of configuration data.  This can be useful for
11:  * creating cache adapters in client builder configuration files.
12:  */
13: class CacheAdapterFactory implements FromConfigInterface
14: {
15:     /**
16:      * Create a Guzzle cache adapter based on an array of options
17:      *
18:      * @param array $config Array of configuration options
19:      *
20:      * @return CacheAdapterInterface
21:      * @throws InvalidArgumentException
22:      */
23:     public static function factory($config = array())
24:     {
25:         if (!is_array($config)) {
26:             throw new InvalidArgumentException('$config must be an array');
27:         }
28: 
29:         if (!isset($config['cache.adapter']) && !isset($config['cache.provider'])) {
30:             $config['cache.adapter'] = 'Guzzle\Cache\NullCacheAdapter';
31:             $config['cache.provider'] = null;
32:         } else {
33:             // Validate that the options are valid
34:             foreach (array('cache.adapter', 'cache.provider') as $required) {
35:                 if (!isset($config[$required])) {
36:                     throw new InvalidArgumentException("{$required} is a required CacheAdapterFactory option");
37:                 }
38:                 if (is_string($config[$required])) {
39:                     // Convert dot notation to namespaces
40:                     $config[$required] = str_replace('.', '\\', $config[$required]);
41:                     if (!class_exists($config[$required])) {
42:                         throw new InvalidArgumentException("{$config[$required]} is not a valid class for {$required}");
43:                     }
44:                 }
45:             }
46:             // Instantiate the cache provider
47:             if (is_string($config['cache.provider'])) {
48:                 $args = isset($config['cache.provider.args']) ? $config['cache.provider.args'] : null;
49:                 $config['cache.provider'] = self::createObject($config['cache.provider'], $args);
50:             }
51:         }
52: 
53:         // Instantiate the cache adapter using the provider and options
54:         if (is_string($config['cache.adapter'])) {
55:             $args = isset($config['cache.adapter.args']) ? $config['cache.adapter.args'] : array();
56:             array_unshift($args, $config['cache.provider']);
57:             $config['cache.adapter'] = self::createObject($config['cache.adapter'], $args);
58:         }
59: 
60:         return $config['cache.adapter'];
61:     }
62: 
63:     /**
64:      * Create a class using an array of constructor arguments
65:      *
66:      * @param string $className Class name
67:      * @param array  $args      Arguments for the class constructor
68:      *
69:      * @return mixed
70:      * @throws RuntimeException
71:      */
72:     protected static function createObject($className, array $args = null)
73:     {
74:         try {
75:             if (!$args) {
76:                 return new $className;
77:             } else {
78:                 $c = new \ReflectionClass($className);
79:                 return $c->newInstanceArgs($args);
80:             }
81:         } catch (\Exception $e) {
82:             throw new RuntimeException($e->getMessage(), $e->getCode(), $e);
83:         }
84:     }
85: }
86: 
php-coveralls API documentation generated by ApiGen 2.8.0