PageRenderTime 52ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Cache/Storage/Adapter/AdapterOptions.php

https://bitbucket.org/aboozar/zf2
PHP | 281 lines | 125 code | 28 blank | 128 comment | 11 complexity | 94cfcd3592dab8293045e6b5e4d5e612 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. * @package Zend_Cache
  9. */
  10. namespace Zend\Cache\Storage\Adapter;
  11. use ArrayObject;
  12. use Zend\Cache\Exception;
  13. use Zend\Cache\Storage\Event;
  14. use Zend\Cache\Storage\StorageInterface;
  15. use Zend\EventManager\EventsCapableInterface;
  16. use Zend\Stdlib\AbstractOptions;
  17. /**
  18. * Unless otherwise marked, all options in this class affect all adapters.
  19. *
  20. * @category Zend
  21. * @package Zend_Cache
  22. * @subpackage Storage
  23. */
  24. class AdapterOptions extends AbstractOptions
  25. {
  26. /**
  27. * The adapter using these options
  28. *
  29. * @var null|Filesystem
  30. */
  31. protected $adapter;
  32. /**
  33. * Validate key against pattern
  34. *
  35. * @var string
  36. */
  37. protected $keyPattern = '';
  38. /**
  39. * Namespace option
  40. *
  41. * @var string
  42. */
  43. protected $namespace = 'zfcache';
  44. /**
  45. * Readable option
  46. *
  47. * @var boolean
  48. */
  49. protected $readable = true;
  50. /**
  51. * TTL option
  52. *
  53. * @var int|float 0 means infinite or maximum of adapter
  54. */
  55. protected $ttl = 0;
  56. /**
  57. * Writable option
  58. *
  59. * @var boolean
  60. */
  61. protected $writable = true;
  62. /**
  63. * Cast to array
  64. *
  65. * @return array
  66. */
  67. public function toArray()
  68. {
  69. $array = array();
  70. $transform = function($letters) {
  71. $letter = array_shift($letters);
  72. return '_' . strtolower($letter);
  73. };
  74. foreach ($this as $key => $value) {
  75. $normalizedKey = preg_replace_callback('/([A-Z])/', $transform, $key);
  76. $array[$normalizedKey] = $value;
  77. }
  78. return $array;
  79. }
  80. /**
  81. * Adapter using this instance
  82. *
  83. * @param StorageInterface|null $adapter
  84. * @return AdapterOptions
  85. */
  86. public function setAdapter(StorageInterface $adapter = null)
  87. {
  88. $this->adapter = $adapter;
  89. return $this;
  90. }
  91. /**
  92. * Set key pattern
  93. *
  94. * @param null|string $keyPattern
  95. * @return AdapterOptions
  96. */
  97. public function setKeyPattern($keyPattern)
  98. {
  99. $keyPattern = (string) $keyPattern;
  100. if ($this->keyPattern !== $keyPattern) {
  101. // validate pattern
  102. if ($keyPattern !== '') {
  103. if (@preg_match($keyPattern, '') === false) {
  104. $err = error_get_last();
  105. throw new Exception\InvalidArgumentException("Invalid pattern '{$keyPattern}': {$err['message']}");
  106. }
  107. }
  108. $this->triggerOptionEvent('key_pattern', $keyPattern);
  109. $this->keyPattern = $keyPattern;
  110. }
  111. return $this;
  112. }
  113. /**
  114. * Get key pattern
  115. *
  116. * @return string
  117. */
  118. public function getKeyPattern()
  119. {
  120. return $this->keyPattern;
  121. }
  122. /**
  123. * Set namespace.
  124. *
  125. * @param string $namespace
  126. * @return AdapterOptions
  127. */
  128. public function setNamespace($namespace)
  129. {
  130. $namespace = (string) $namespace;
  131. if ($this->namespace !== $namespace) {
  132. $this->triggerOptionEvent('namespace', $namespace);
  133. $this->namespace = $namespace;
  134. }
  135. return $this;
  136. }
  137. /**
  138. * Get namespace
  139. *
  140. * @return string
  141. */
  142. public function getNamespace()
  143. {
  144. return $this->namespace;
  145. }
  146. /**
  147. * Enable/Disable reading data from cache.
  148. *
  149. * @param boolean $readable
  150. * @return AbstractAdapter
  151. */
  152. public function setReadable($readable)
  153. {
  154. $readable = (bool) $readable;
  155. if ($this->readable !== $readable) {
  156. $this->triggerOptionEvent('readable', $readable);
  157. $this->readable = $readable;
  158. }
  159. return $this;
  160. }
  161. /**
  162. * If reading data from cache enabled.
  163. *
  164. * @return boolean
  165. */
  166. public function getReadable()
  167. {
  168. return $this->readable;
  169. }
  170. /**
  171. * Set time to live.
  172. *
  173. * @param int|float $ttl
  174. * @return AdapterOptions
  175. */
  176. public function setTtl($ttl)
  177. {
  178. $this->normalizeTtl($ttl);
  179. if ($this->ttl !== $ttl) {
  180. $this->triggerOptionEvent('ttl', $ttl);
  181. $this->ttl = $ttl;
  182. }
  183. return $this;
  184. }
  185. /**
  186. * Get time to live.
  187. *
  188. * @return float
  189. */
  190. public function getTtl()
  191. {
  192. return $this->ttl;
  193. }
  194. /**
  195. * Enable/Disable writing data to cache.
  196. *
  197. * @param boolean $writable
  198. * @return AdapterOptions
  199. */
  200. public function setWritable($writable)
  201. {
  202. $writable = (bool) $writable;
  203. if ($this->writable !== $writable) {
  204. $this->triggerOptionEvent('writable', $writable);
  205. $this->writable = $writable;
  206. }
  207. return $this;
  208. }
  209. /**
  210. * If writing data to cache enabled.
  211. *
  212. * @return boolean
  213. */
  214. public function getWritable()
  215. {
  216. return $this->writable;
  217. }
  218. /**
  219. * Triggers an option event if this options instance has a connection to
  220. * an adapter implements EventsCapableInterface.
  221. *
  222. * @param string $optionName
  223. * @param mixed $optionValue
  224. * @return void
  225. */
  226. protected function triggerOptionEvent($optionName, $optionValue)
  227. {
  228. if ($this->adapter instanceof EventsCapableInterface) {
  229. $event = new Event('option', $this->adapter, new ArrayObject(array($optionName => $optionValue)));
  230. $this->adapter->getEventManager()->trigger($event);
  231. }
  232. }
  233. /**
  234. * Validates and normalize a TTL.
  235. *
  236. * @param int|float $ttl
  237. * @throws Exception\InvalidArgumentException
  238. * @return void
  239. */
  240. protected function normalizeTtl(&$ttl)
  241. {
  242. if (!is_int($ttl)) {
  243. $ttl = (float) $ttl;
  244. // convert to int if possible
  245. if ($ttl === (float) (int) $ttl) {
  246. $ttl = (int) $ttl;
  247. }
  248. }
  249. if ($ttl < 0) {
  250. throw new Exception\InvalidArgumentException("TTL can't be negative");
  251. }
  252. }
  253. }