diff --git a/core/lib/Thelia/Action/Cache.php b/core/lib/Thelia/Action/Cache.php new file mode 100644 index 000000000..6d2cf1a16 --- /dev/null +++ b/core/lib/Thelia/Action/Cache.php @@ -0,0 +1,77 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Action; +use Symfony\Component\EventDispatcher\EventSubscriberInterface; +use Symfony\Component\Filesystem\Filesystem; +use Thelia\Core\Event\Cache\CacheEvent; +use Thelia\Core\Event\TheliaEvents; + + +/** + * Class Cache + * @package Thelia\Action + * @author Manuel Raynaud + */ +class Cache extends BaseAction implements EventSubscriberInterface +{ + + public function cacheClear(CacheEvent $event) + { + $dir = $event->getDir(); + + $directoryBrowser = new \DirectoryIterator($dir); + + + $fs = new Filesystem(); + $fs->remove($dir); + + } + + /** + * Returns an array of event names this subscriber wants to listen to. + * + * The array keys are event names and the value can be: + * + * * The method name to call (priority defaults to 0) + * * An array composed of the method name to call and the priority + * * An array of arrays composed of the method names to call and respective + * priorities, or 0 if unset + * + * For instance: + * + * * array('eventName' => 'methodName') + * * array('eventName' => array('methodName', $priority)) + * * array('eventName' => array(array('methodName1', $priority), array('methodName2')) + * + * @return array The event names to listen to + * + * @api + */ + public static function getSubscribedEvents() + { + return array( + TheliaEvents::CACHE_CLEAR => array('cacheClear', 128) + ); + } +} \ No newline at end of file diff --git a/core/lib/Thelia/Command/CacheClear.php b/core/lib/Thelia/Command/CacheClear.php index e85e29741..1d4e7919c 100755 --- a/core/lib/Thelia/Command/CacheClear.php +++ b/core/lib/Thelia/Command/CacheClear.php @@ -30,6 +30,8 @@ use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\Filesystem\Exception\IOException; use Thelia\Command\ContainerAwareCommand; +use Thelia\Core\Event\Cache\CacheEvent; +use Thelia\Core\Event\TheliaEvents; /** * clear the cache @@ -72,7 +74,11 @@ class CacheClear extends ContainerAwareCommand $output->writeln(sprintf("Clearing cache in %s directory", $dir)); try { - $directoryBrowser = new \DirectoryIterator($dir); + $cacheEvent = new CacheEvent($dir); + $this-> + getContainer() + ->get('event_dispatcher') + ->dispatch(TheliaEvents::CACHE_CLEAR, $cacheEvent); } catch (\UnexpectedValueException $e) { // throws same exception code for does not exist and permission denied ... if (!file_exists($dir)) { @@ -82,15 +88,11 @@ class CacheClear extends ContainerAwareCommand } throw $e; - } - - $fs = new Filesystem(); - try { - $fs->remove($dir); - - $output->writeln(sprintf("%s cache dir cleared successfully", $dir)); } catch (IOException $e) { $output->writeln(sprintf("Error during clearing cache : %s", $e->getMessage())); } + + $output->writeln(sprintf("%s cache dir cleared successfully", $dir)); + } } diff --git a/core/lib/Thelia/Config/Resources/action.xml b/core/lib/Thelia/Config/Resources/action.xml index 8b4cfe783..9088d1a50 100755 --- a/core/lib/Thelia/Config/Resources/action.xml +++ b/core/lib/Thelia/Config/Resources/action.xml @@ -141,6 +141,11 @@ + + + + + diff --git a/core/lib/Thelia/Core/Event/Cache/CacheEvent.php b/core/lib/Thelia/Core/Event/Cache/CacheEvent.php new file mode 100644 index 000000000..3a882d5dd --- /dev/null +++ b/core/lib/Thelia/Core/Event/Cache/CacheEvent.php @@ -0,0 +1,67 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Core\Event\Cache; + +use Thelia\Core\Event\ActionEvent; + + +/** + * Class CacheEvent + * @package Thelia\Core\Event\Cache + * @author Manuel Raynaud + */ +class CacheEvent extends ActionEvent +{ + /** + * @var string cache directory + */ + protected $dir; + + public function __construct($dir) + { + $this->dir = $dir; + } + + /** + * @param mixed $dir + * + * @return $this + */ + public function setDir($dir) + { + $this->dir = $dir; + + return $this; + } + + /** + * @return mixed + */ + public function getDir() + { + return $this->dir; + } + + +} \ No newline at end of file diff --git a/core/lib/Thelia/Core/Event/TheliaEvents.php b/core/lib/Thelia/Core/Event/TheliaEvents.php index 78e96bf97..f70eca2a1 100755 --- a/core/lib/Thelia/Core/Event/TheliaEvents.php +++ b/core/lib/Thelia/Core/Event/TheliaEvents.php @@ -657,6 +657,13 @@ final class TheliaEvents const GENERATE_PDF = 'thelia.generatePdf'; + /** + * sent when a module is activated or deactivated + */ const MODULE_TOGGLE_ACTIVATION = 'thelia.module.toggleActivation'; + /** + * sent for clearing cache + */ + const CACHE_CLEAR = 'thelia.cache.clear'; }