3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Cache\Pattern
;
13 use Zend\Cache\Exception
;
15 class ClassCache
extends CallbackCache
20 * @param PatternOptions $options
22 * @throws Exception\InvalidArgumentException if missing 'class' or 'storage' options
24 public function setOptions(PatternOptions
$options)
26 parent
::setOptions($options);
28 if (!$options->getClass()) {
29 throw new Exception\
InvalidArgumentException("Missing option 'class'");
30 } elseif (!$options->getStorage()) {
31 throw new Exception\
InvalidArgumentException("Missing option 'storage'");
37 * Call and cache a class method
39 * @param string $method Method name to call
40 * @param array $args Method arguments
42 * @throws Exception\RuntimeException
45 public function call($method, array $args = array())
47 $options = $this->getOptions();
48 $classname = $options->getClass();
49 $method = strtolower($method);
50 $callback = $classname . '::' . $method;
52 $cache = $options->getCacheByDefault();
54 $cache = !in_array($method, $options->getClassNonCacheMethods());
56 $cache = in_array($method, $options->getClassCacheMethods());
61 return call_user_func_array($callback, $args);
63 return $classname::$method();
67 return parent
::call($callback, $args);
71 * Generate a unique key in base of a key representing the callback part
72 * and a key representing the arguments part.
74 * @param string $method The method
75 * @param array $args Callback arguments
77 * @throws Exception\RuntimeException
79 public function generateKey($method, array $args = array())
81 return $this->generateCallbackKey(
82 $this->getOptions()->getClass() . '::' . $method,
88 * Generate a unique key in base of a key representing the callback part
89 * and a key representing the arguments part.
91 * @param callable $callback A valid callback
92 * @param array $args Callback arguments
94 * @throws Exception\RuntimeException
96 protected function generateCallbackKey($callback, array $args)
98 $callbackKey = md5(strtolower($callback));
99 $argumentKey = $this->generateArgumentsKey($args);
100 return $callbackKey . $argumentKey;
104 * Calling a method of the entity.
106 * @param string $method Method name to call
107 * @param array $args Method arguments
109 * @throws Exception\RuntimeException
112 public function __call($method, array $args)
114 return $this->call($method, $args);
118 * Set a static property
120 * @param string $name
121 * @param mixed $value
123 * @see http://php.net/manual/language.oop5.overloading.php#language.oop5.overloading.members
125 public function __set($name, $value)
127 $class = $this->getOptions()->getClass();
128 $class::$name = $value;
132 * Get a static property
134 * @param string $name
136 * @see http://php.net/manual/language.oop5.overloading.php#language.oop5.overloading.members
138 public function __get($name)
140 $class = $this->getOptions()->getClass();
141 return $class::$name;
145 * Is a static property exists.
147 * @param string $name
150 public function __isset($name)
152 $class = $this->getOptions()->getClass();
153 return isset($class::$name);
157 * Unset a static property
159 * @param string $name
162 public function __unset($name)
164 $class = $this->getOptions()->getClass();
165 unset($class::$name);