/library/Zend/Cache/Storage/Plugin/ExceptionHandler.php

https://github.com/jtai/zf2 · PHP · 155 lines · 70 code · 27 blank · 58 comment · 3 complexity · 701d2647bc688049c599e086bbeb3b67 MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Cache
  17. * @subpackage Storage
  18. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. namespace Zend\Cache\Storage\Plugin;
  22. use Traversable,
  23. Zend\Cache\Exception,
  24. Zend\Cache\Storage\ExceptionEvent,
  25. Zend\EventManager\EventCollection;
  26. /**
  27. * @category Zend
  28. * @package Zend_Cache
  29. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  30. * @license http://framework.zend.com/license/new-bsd New BSD License
  31. */
  32. class ExceptionHandler extends AbstractPlugin
  33. {
  34. /**
  35. * Handles
  36. *
  37. * @var array
  38. */
  39. protected $handles = array();
  40. /**
  41. * Attach
  42. *
  43. * @param EventCollection $eventCollection
  44. * @return ExceptionHandler
  45. * @throws Exception\LogicException
  46. */
  47. public function attach(EventCollection $eventCollection)
  48. {
  49. $index = spl_object_hash($eventCollection);
  50. if (isset($this->handles[$index])) {
  51. throw new Exception\LogicException('Plugin already attached');
  52. }
  53. $callback = array($this, 'onException');
  54. $handles = array();
  55. $this->handles[$index] = & $handles;
  56. // read
  57. $handles[] = $eventCollection->attach('getItem.exception', $callback);
  58. $handles[] = $eventCollection->attach('getItems.exception', $callback);
  59. $handles[] = $eventCollection->attach('hasItem.exception', $callback);
  60. $handles[] = $eventCollection->attach('hasItems.exception', $callback);
  61. $handles[] = $eventCollection->attach('getMetadata.exception', $callback);
  62. $handles[] = $eventCollection->attach('getMetadatas.exception', $callback);
  63. // non-blocking
  64. $handles[] = $eventCollection->attach('getDelayed.exception', $callback);
  65. $handles[] = $eventCollection->attach('find.exception', $callback);
  66. $handles[] = $eventCollection->attach('fetch.exception', $callback);
  67. $handles[] = $eventCollection->attach('fetchAll.exception', $callback);
  68. // write
  69. $handles[] = $eventCollection->attach('setItem.exception', $callback);
  70. $handles[] = $eventCollection->attach('setItems.exception', $callback);
  71. $handles[] = $eventCollection->attach('addItem.exception', $callback);
  72. $handles[] = $eventCollection->attach('addItems.exception', $callback);
  73. $handles[] = $eventCollection->attach('replaceItem.exception', $callback);
  74. $handles[] = $eventCollection->attach('replaceItems.exception', $callback);
  75. $handles[] = $eventCollection->attach('touchItem.exception', $callback);
  76. $handles[] = $eventCollection->attach('touchItems.exception', $callback);
  77. $handles[] = $eventCollection->attach('removeItem.exception', $callback);
  78. $handles[] = $eventCollection->attach('removeItems.exception', $callback);
  79. $handles[] = $eventCollection->attach('checkAndSetItem.exception', $callback);
  80. // increment / decrement item(s)
  81. $handles[] = $eventCollection->attach('incrementItem.exception', $callback);
  82. $handles[] = $eventCollection->attach('incrementItems.exception', $callback);
  83. $handles[] = $eventCollection->attach('decrementItem.exception', $callback);
  84. $handles[] = $eventCollection->attach('decrementItems.exception', $callback);
  85. // clear
  86. $handles[] = $eventCollection->attach('clear.exception', $callback);
  87. $handles[] = $eventCollection->attach('clearByNamespace.exception', $callback);
  88. // additional
  89. $handles[] = $eventCollection->attach('optimize.exception', $callback);
  90. $handles[] = $eventCollection->attach('getCapacity.exception', $callback);
  91. return $this;
  92. }
  93. /**
  94. * Detach
  95. *
  96. * @param EventCollection $eventCollection
  97. * @return ExceptionHandler
  98. * @throws Exception\LogicException
  99. */
  100. public function detach(EventCollection $eventCollection)
  101. {
  102. $index = spl_object_hash($eventCollection);
  103. if (!isset($this->handles[$index])) {
  104. throw new Exception\LogicException('Plugin not attached');
  105. }
  106. // detach all handles of this index
  107. foreach ($this->handles[$index] as $handle) {
  108. $eventCollection->detach($handle);
  109. }
  110. // remove all detached handles
  111. unset($this->handles[$index]);
  112. return $this;
  113. }
  114. /**
  115. * On exception
  116. *
  117. * @param ExceptionEvent $event
  118. * @return void
  119. */
  120. public function onException(ExceptionEvent $event)
  121. {
  122. $options = $this->getOptions();
  123. if (($callback = $options->getExceptionCallback())) {
  124. call_user_func($callback, $event->getException());
  125. }
  126. $event->setThrowException($options->getThrowExceptions());
  127. }
  128. }