diff --git a/src/Storage/Adapter/Filesystem.php b/src/Storage/Adapter/Filesystem.php index cbe2899ae..487d98d3d 100644 --- a/src/Storage/Adapter/Filesystem.php +++ b/src/Storage/Adapter/Filesystem.php @@ -25,6 +25,7 @@ use Zend\Cache\Storage\TaggableInterface; use Zend\Cache\Storage\TotalSpaceCapableInterface; use Zend\Stdlib\ErrorHandler; +use ArrayObject; class Filesystem extends AbstractAdapter implements AvailableSpaceCapableInterface, @@ -131,6 +132,8 @@ public function flush() * Remove expired items * * @return bool + * + * @triggers clearExpired.exception(ExceptionEvent) */ public function clearExpired() { @@ -161,7 +164,13 @@ public function clearExpired() } $error = ErrorHandler::stop(); if ($error) { - throw new Exception\RuntimeException("Failed to clear expired items", 0, $error); + $result = false; + return $this->triggerException( + __FUNCTION__, + new ArrayObject(), + $result, + new Exception\RuntimeException('Failed to clear expired items', 0, $error) + ); } return true; diff --git a/src/Storage/Plugin/ExceptionHandler.php b/src/Storage/Plugin/ExceptionHandler.php index 5c99c3cbe..e6acace0c 100644 --- a/src/Storage/Plugin/ExceptionHandler.php +++ b/src/Storage/Plugin/ExceptionHandler.php @@ -55,6 +55,9 @@ public function attach(EventManagerInterface $events, $priority = 1) $this->listeners[] = $events->attach('decrementItem.exception', $callback, $priority); $this->listeners[] = $events->attach('decrementItems.exception', $callback, $priority); + + // utility + $this->listeners[] = $events->attach('clearExpired.exception', $callback, $priority); } /** diff --git a/test/Storage/Adapter/FilesystemTest.php b/test/Storage/Adapter/FilesystemTest.php index 168e6a3d9..d0c6ca50a 100644 --- a/test/Storage/Adapter/FilesystemTest.php +++ b/test/Storage/Adapter/FilesystemTest.php @@ -10,6 +10,8 @@ namespace ZendTest\Cache\Storage\Adapter; use Zend\Cache; +use Zend\Cache\Storage\Plugin\ExceptionHandler; +use Zend\Cache\Storage\Plugin\PluginOptions; /** * @group Zend_Cache @@ -281,4 +283,22 @@ public function testGetMetadataWithAtime() $expectedAtime = fileatime($meta['filespec'] . '.dat'); $this->assertEquals($expectedAtime, $meta['atime']); } + + public function testClearExpiredExceptionTriggersEvent() + { + $this->_options->setTtl(0.1); + $this->_storage->setItem('k', 'v'); + $dirs = glob($this->_tmpCacheDir . '/*'); + if (count($dirs) === 0) { + $this->fail('Could not find cache dir'); + } + chmod($dirs[0], 0500); //make directory rx, unlink should fail + sleep(1); //wait for the entry to expire + $plugin = new ExceptionHandler(); + $options = new PluginOptions(array('throw_exceptions' => false)); + $plugin->setOptions($options); + $this->_storage->addPlugin($plugin); + $this->_storage->clearExpired(); + chmod($dirs[0], 0700); //set dir back to writable for tearDown + } } diff --git a/test/Storage/Plugin/ExceptionHandlerTest.php b/test/Storage/Plugin/ExceptionHandlerTest.php index edf017d61..7f1d878b6 100644 --- a/test/Storage/Plugin/ExceptionHandlerTest.php +++ b/test/Storage/Plugin/ExceptionHandlerTest.php @@ -71,6 +71,8 @@ public function testAddPlugin() 'decrementItem.exception' => 'onException', 'decrementItems.exception' => 'onException', + + 'clearExpired.exception' => 'onException', ); foreach ($expectedListeners as $eventName => $expectedCallbackMethod) { $listeners = $this->_adapter->getEventManager()->getListeners($eventName);