/vendor/zendframework/zendframework/library/Zend/Cache/Storage/Plugin/Serializer.php

https://bitbucket.org/pcelta/zf2 · PHP · 313 lines · 176 code · 39 blank · 98 comment · 11 complexity · 2627192c1fc24db42a90ef2cee919695 MD5 · raw file

  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\Plugin;
  11. use stdClass;
  12. use Zend\Cache\Exception;
  13. use Zend\Cache\Storage\Capabilities;
  14. use Zend\Cache\Storage\Event;
  15. use Zend\Cache\Storage\PostEvent;
  16. use Zend\EventManager\EventManagerInterface;
  17. /**
  18. * @category Zend
  19. * @package Zend_Cache
  20. * @subpackage Storage
  21. */
  22. class Serializer extends AbstractPlugin
  23. {
  24. /**
  25. * @var array
  26. */
  27. protected $capabilities = array();
  28. /**
  29. * Handles
  30. *
  31. * @var array
  32. */
  33. protected $handles = array();
  34. /**
  35. * Attach
  36. *
  37. * @param EventManagerInterface $events
  38. * @param int $priority
  39. * @return Serializer
  40. * @throws Exception\LogicException
  41. */
  42. public function attach(EventManagerInterface $events, $priority = 1)
  43. {
  44. $index = spl_object_hash($events);
  45. if (isset($this->handles[$index])) {
  46. throw new Exception\LogicException('Plugin already attached');
  47. }
  48. $handles = array();
  49. $this->handles[$index] = & $handles;
  50. // The higher the priority the sooner the plugin will be called on pre events
  51. // but the later it will be called on post events.
  52. $prePriority = $priority;
  53. $postPriority = -$priority;
  54. // read
  55. $handles[] = $events->attach('getItem.post', array($this, 'onReadItemPost'), $postPriority);
  56. $handles[] = $events->attach('getItems.post', array($this, 'onReadItemsPost'), $postPriority);
  57. // write
  58. $handles[] = $events->attach('setItem.pre', array($this, 'onWriteItemPre'), $prePriority);
  59. $handles[] = $events->attach('setItems.pre', array($this, 'onWriteItemsPre'), $prePriority);
  60. $handles[] = $events->attach('addItem.pre', array($this, 'onWriteItemPre'), $prePriority);
  61. $handles[] = $events->attach('addItems.pre', array($this, 'onWriteItemsPre'), $prePriority);
  62. $handles[] = $events->attach('replaceItem.pre', array($this, 'onWriteItemPre'), $prePriority);
  63. $handles[] = $events->attach('replaceItems.pre', array($this, 'onWriteItemsPre'), $prePriority);
  64. $handles[] = $events->attach('checkAndSetItem.pre', array($this, 'onWriteItemPre'), $prePriority);
  65. // increment / decrement item(s)
  66. $handles[] = $events->attach('incrementItem.pre', array($this, 'onIncrementItemPre'), $prePriority);
  67. $handles[] = $events->attach('incrementItems.pre', array($this, 'onIncrementItemsPre'), $prePriority);
  68. $handles[] = $events->attach('decrementItem.pre', array($this, 'onDecrementItemPre'), $prePriority);
  69. $handles[] = $events->attach('decrementItems.pre', array($this, 'onDecrementItemsPre'), $prePriority);
  70. // overwrite capabilities
  71. $handles[] = $events->attach('getCapabilities.post', array($this, 'onGetCapabilitiesPost'), $postPriority);
  72. return $this;
  73. }
  74. /**
  75. * Detach
  76. *
  77. * @param EventManagerInterface $events
  78. * @return Serializer
  79. * @throws Exception\LogicException
  80. */
  81. public function detach(EventManagerInterface $events)
  82. {
  83. $index = spl_object_hash($events);
  84. if (!isset($this->handles[$index])) {
  85. throw new Exception\LogicException('Plugin not attached');
  86. }
  87. // detach all handles of this index
  88. foreach ($this->handles[$index] as $handle) {
  89. $events->detach($handle);
  90. }
  91. // remove all detached handles
  92. unset($this->handles[$index]);
  93. return $this;
  94. }
  95. /**
  96. * On read item post
  97. *
  98. * @param PostEvent $event
  99. * @return void
  100. */
  101. public function onReadItemPost(PostEvent $event)
  102. {
  103. $serializer = $this->getOptions()->getSerializer();
  104. $result = $event->getResult();
  105. $result = $serializer->unserialize($result);
  106. $event->setResult($result);
  107. }
  108. /**
  109. * On read items post
  110. *
  111. * @param PostEvent $event
  112. * @return void
  113. */
  114. public function onReadItemsPost(PostEvent $event)
  115. {
  116. $serializer = $this->getOptions()->getSerializer();
  117. $result = $event->getResult();
  118. foreach ($result as &$value) {
  119. $value = $serializer->unserialize($value);
  120. }
  121. $event->setResult($result);
  122. }
  123. /**
  124. * On write item pre
  125. *
  126. * @param Event $event
  127. * @return void
  128. */
  129. public function onWriteItemPre(Event $event)
  130. {
  131. $serializer = $this->getOptions()->getSerializer();
  132. $params = $event->getParams();
  133. $params['value'] = $serializer->serialize($params['value']);
  134. }
  135. /**
  136. * On write items pre
  137. *
  138. * @param Event $event
  139. * @return void
  140. */
  141. public function onWriteItemsPre(Event $event)
  142. {
  143. $serializer = $this->getOptions()->getSerializer();
  144. $params = $event->getParams();
  145. foreach ($params['keyValuePairs'] as &$value) {
  146. $value = $serializer->serialize($value);
  147. }
  148. }
  149. /**
  150. * On increment item pre
  151. *
  152. * @param Event $event
  153. * @return mixed
  154. */
  155. public function onIncrementItemPre(Event $event)
  156. {
  157. $storage = $event->getTarget();
  158. $params = $event->getParams();
  159. $casToken = null;
  160. $success = null;
  161. $oldValue = $storage->getItem($params['key'], $success, $casToken);
  162. $newValue = $oldValue + $params['value'];
  163. if ($success) {
  164. $storage->checkAndSetItem($casToken, $params['key'], $oldValue + $params['value']);
  165. $result = $newValue;
  166. } else {
  167. $result = false;
  168. }
  169. $event->stopPropagation(true);
  170. return $result;
  171. }
  172. /**
  173. * On increment items pre
  174. *
  175. * @param Event $event
  176. * @return mixed
  177. */
  178. public function onIncrementItemsPre(Event $event)
  179. {
  180. $storage = $event->getTarget();
  181. $params = $event->getParams();
  182. $keyValuePairs = $storage->getItems(array_keys($params['keyValuePairs']));
  183. foreach ($params['keyValuePairs'] as $key => & $value) {
  184. if (isset($keyValuePairs[$key])) {
  185. $keyValuePairs[$key]+= $value;
  186. } else {
  187. $keyValuePairs[$key] = $value;
  188. }
  189. }
  190. $failedKeys = $storage->setItems($keyValuePairs);
  191. foreach ($failedKeys as $failedKey) {
  192. unset($keyValuePairs[$failedKey]);
  193. }
  194. $event->stopPropagation(true);
  195. return $keyValuePairs;
  196. }
  197. /**
  198. * On decrement item pre
  199. *
  200. * @param Event $event
  201. * @return mixed
  202. */
  203. public function onDecrementItemPre(Event $event)
  204. {
  205. $storage = $event->getTarget();
  206. $params = $event->getParams();
  207. $success = null;
  208. $casToken = null;
  209. $oldValue = $storage->getItem($params['key'], $success, $casToken);
  210. $newValue = $oldValue - $params['value'];
  211. if ($success) {
  212. $storage->checkAndSetItem($casToken, $params['key'], $oldValue + $params['value']);
  213. $result = $newValue;
  214. } else {
  215. $result = false;
  216. }
  217. $event->stopPropagation(true);
  218. return $result;
  219. }
  220. /**
  221. * On decrement items pre
  222. *
  223. * @param Event $event
  224. * @return mixed
  225. */
  226. public function onDecrementItemsPre(Event $event)
  227. {
  228. $storage = $event->getTarget();
  229. $params = $event->getParams();
  230. $keyValuePairs = $storage->getItems(array_keys($params['keyValuePairs']));
  231. foreach ($params['keyValuePairs'] as $key => &$value) {
  232. if (isset($keyValuePairs[$key])) {
  233. $keyValuePairs[$key]-= $value;
  234. } else {
  235. $keyValuePairs[$key] = -$value;
  236. }
  237. }
  238. $failedKeys = $storage->setItems($keyValuePairs);
  239. foreach ($failedKeys as $failedKey) {
  240. unset($keyValuePairs[$failedKey]);
  241. }
  242. $event->stopPropagation(true);
  243. return $keyValuePairs;
  244. }
  245. /**
  246. * On get capabilities
  247. *
  248. * @param PostEvent $event
  249. * @return void
  250. */
  251. public function onGetCapabilitiesPost(PostEvent $event)
  252. {
  253. $baseCapabilities = $event->getResult();
  254. $index = spl_object_hash($baseCapabilities);
  255. if (!isset($this->capabilities[$index])) {
  256. $this->capabilities[$index] = new Capabilities(
  257. $baseCapabilities->getAdapter(),
  258. new stdClass(), // marker
  259. array('supportedDatatypes' => array(
  260. 'NULL' => true,
  261. 'boolean' => true,
  262. 'integer' => true,
  263. 'double' => true,
  264. 'string' => true,
  265. 'array' => true,
  266. 'object' => 'object',
  267. 'resource' => false,
  268. )),
  269. $baseCapabilities
  270. );
  271. }
  272. $event->setResult($this->capabilities[$index]);
  273. }
  274. }