fix calendar css, take 2. (#213)
[openemr.git] / interface / modules / zend_modules / library / Zend / Cache / Storage / Plugin / Serializer.php
blobd79f221a06b046226d14d30a82bafdecb44c5fa6
1 <?php
2 /**
3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
8 */
10 namespace Zend\Cache\Storage\Plugin;
12 use stdClass;
13 use Zend\Cache\Storage\Capabilities;
14 use Zend\Cache\Storage\Event;
15 use Zend\Cache\Storage\PostEvent;
16 use Zend\EventManager\EventManagerInterface;
18 class Serializer extends AbstractPlugin
20 /**
21 * @var array
23 protected $capabilities = array();
25 /**
26 * {@inheritDoc}
28 public function attach(EventManagerInterface $events, $priority = 1)
30 // The higher the priority the sooner the plugin will be called on pre events
31 // but the later it will be called on post events.
32 $prePriority = $priority;
33 $postPriority = -$priority;
35 // read
36 $this->listeners[] = $events->attach('getItem.post', array($this, 'onReadItemPost'), $postPriority);
37 $this->listeners[] = $events->attach('getItems.post', array($this, 'onReadItemsPost'), $postPriority);
39 // write
40 $this->listeners[] = $events->attach('setItem.pre', array($this, 'onWriteItemPre'), $prePriority);
41 $this->listeners[] = $events->attach('setItems.pre', array($this, 'onWriteItemsPre'), $prePriority);
43 $this->listeners[] = $events->attach('addItem.pre', array($this, 'onWriteItemPre'), $prePriority);
44 $this->listeners[] = $events->attach('addItems.pre', array($this, 'onWriteItemsPre'), $prePriority);
46 $this->listeners[] = $events->attach('replaceItem.pre', array($this, 'onWriteItemPre'), $prePriority);
47 $this->listeners[] = $events->attach('replaceItems.pre', array($this, 'onWriteItemsPre'), $prePriority);
49 $this->listeners[] = $events->attach('checkAndSetItem.pre', array($this, 'onWriteItemPre'), $prePriority);
51 // increment / decrement item(s)
52 $this->listeners[] = $events->attach('incrementItem.pre', array($this, 'onIncrementItemPre'), $prePriority);
53 $this->listeners[] = $events->attach('incrementItems.pre', array($this, 'onIncrementItemsPre'), $prePriority);
55 $this->listeners[] = $events->attach('decrementItem.pre', array($this, 'onDecrementItemPre'), $prePriority);
56 $this->listeners[] = $events->attach('decrementItems.pre', array($this, 'onDecrementItemsPre'), $prePriority);
58 // overwrite capabilities
59 $this->listeners[] = $events->attach('getCapabilities.post', array($this, 'onGetCapabilitiesPost'), $postPriority);
62 /**
63 * On read item post
65 * @param PostEvent $event
66 * @return void
68 public function onReadItemPost(PostEvent $event)
70 $serializer = $this->getOptions()->getSerializer();
71 $result = $event->getResult();
72 $result = $serializer->unserialize($result);
73 $event->setResult($result);
76 /**
77 * On read items post
79 * @param PostEvent $event
80 * @return void
82 public function onReadItemsPost(PostEvent $event)
84 $serializer = $this->getOptions()->getSerializer();
85 $result = $event->getResult();
86 foreach ($result as &$value) {
87 $value = $serializer->unserialize($value);
89 $event->setResult($result);
92 /**
93 * On write item pre
95 * @param Event $event
96 * @return void
98 public function onWriteItemPre(Event $event)
100 $serializer = $this->getOptions()->getSerializer();
101 $params = $event->getParams();
102 $params['value'] = $serializer->serialize($params['value']);
106 * On write items pre
108 * @param Event $event
109 * @return void
111 public function onWriteItemsPre(Event $event)
113 $serializer = $this->getOptions()->getSerializer();
114 $params = $event->getParams();
115 foreach ($params['keyValuePairs'] as &$value) {
116 $value = $serializer->serialize($value);
121 * On increment item pre
123 * @param Event $event
124 * @return mixed
126 public function onIncrementItemPre(Event $event)
128 $storage = $event->getTarget();
129 $params = $event->getParams();
130 $casToken = null;
131 $success = null;
132 $oldValue = $storage->getItem($params['key'], $success, $casToken);
133 $newValue = $oldValue + $params['value'];
135 if ($success) {
136 $storage->checkAndSetItem($casToken, $params['key'], $oldValue + $params['value']);
137 $result = $newValue;
138 } else {
139 $result = false;
142 $event->stopPropagation(true);
143 return $result;
147 * On increment items pre
149 * @param Event $event
150 * @return mixed
152 public function onIncrementItemsPre(Event $event)
154 $storage = $event->getTarget();
155 $params = $event->getParams();
156 $keyValuePairs = $storage->getItems(array_keys($params['keyValuePairs']));
157 foreach ($params['keyValuePairs'] as $key => & $value) {
158 if (isset($keyValuePairs[$key])) {
159 $keyValuePairs[$key]+= $value;
160 } else {
161 $keyValuePairs[$key] = $value;
165 $failedKeys = $storage->setItems($keyValuePairs);
166 foreach ($failedKeys as $failedKey) {
167 unset($keyValuePairs[$failedKey]);
170 $event->stopPropagation(true);
171 return $keyValuePairs;
175 * On decrement item pre
177 * @param Event $event
178 * @return mixed
180 public function onDecrementItemPre(Event $event)
182 $storage = $event->getTarget();
183 $params = $event->getParams();
184 $success = null;
185 $casToken = null;
186 $oldValue = $storage->getItem($params['key'], $success, $casToken);
187 $newValue = $oldValue - $params['value'];
189 if ($success) {
190 $storage->checkAndSetItem($casToken, $params['key'], $oldValue + $params['value']);
191 $result = $newValue;
192 } else {
193 $result = false;
196 $event->stopPropagation(true);
197 return $result;
201 * On decrement items pre
203 * @param Event $event
204 * @return mixed
206 public function onDecrementItemsPre(Event $event)
208 $storage = $event->getTarget();
209 $params = $event->getParams();
210 $keyValuePairs = $storage->getItems(array_keys($params['keyValuePairs']));
211 foreach ($params['keyValuePairs'] as $key => &$value) {
212 if (isset($keyValuePairs[$key])) {
213 $keyValuePairs[$key]-= $value;
214 } else {
215 $keyValuePairs[$key] = -$value;
219 $failedKeys = $storage->setItems($keyValuePairs);
220 foreach ($failedKeys as $failedKey) {
221 unset($keyValuePairs[$failedKey]);
224 $event->stopPropagation(true);
225 return $keyValuePairs;
229 * On get capabilities
231 * @param PostEvent $event
232 * @return void
234 public function onGetCapabilitiesPost(PostEvent $event)
236 $baseCapabilities = $event->getResult();
237 $index = spl_object_hash($baseCapabilities);
239 if (!isset($this->capabilities[$index])) {
240 $this->capabilities[$index] = new Capabilities(
241 $baseCapabilities->getAdapter(),
242 new stdClass(), // marker
243 array('supportedDatatypes' => array(
244 'NULL' => true,
245 'boolean' => true,
246 'integer' => true,
247 'double' => true,
248 'string' => true,
249 'array' => true,
250 'object' => 'object',
251 'resource' => false,
253 $baseCapabilities
257 $event->setResult($this->capabilities[$index]);