dispatching event for clearing cache
This commit is contained in:
77
core/lib/Thelia/Action/Cache.php
Normal file
77
core/lib/Thelia/Action/Cache.php
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
<?php
|
||||||
|
/*************************************************************************************/
|
||||||
|
/* */
|
||||||
|
/* Thelia */
|
||||||
|
/* */
|
||||||
|
/* Copyright (c) OpenStudio */
|
||||||
|
/* email : info@thelia.net */
|
||||||
|
/* web : http://www.thelia.net */
|
||||||
|
/* */
|
||||||
|
/* This program is free software; you can redistribute it and/or modify */
|
||||||
|
/* it under the terms of the GNU General Public License as published by */
|
||||||
|
/* the Free Software Foundation; either version 3 of the License */
|
||||||
|
/* */
|
||||||
|
/* This program is distributed in the hope that it will be useful, */
|
||||||
|
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
||||||
|
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
||||||
|
/* GNU General Public License for more details. */
|
||||||
|
/* */
|
||||||
|
/* You should have received a copy of the GNU General Public License */
|
||||||
|
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||||
|
/* */
|
||||||
|
/*************************************************************************************/
|
||||||
|
|
||||||
|
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 <mraynaud@openstudio.fr>
|
||||||
|
*/
|
||||||
|
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)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -30,6 +30,8 @@ use Symfony\Component\Filesystem\Filesystem;
|
|||||||
use Symfony\Component\Filesystem\Exception\IOException;
|
use Symfony\Component\Filesystem\Exception\IOException;
|
||||||
|
|
||||||
use Thelia\Command\ContainerAwareCommand;
|
use Thelia\Command\ContainerAwareCommand;
|
||||||
|
use Thelia\Core\Event\Cache\CacheEvent;
|
||||||
|
use Thelia\Core\Event\TheliaEvents;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* clear the cache
|
* clear the cache
|
||||||
@@ -72,7 +74,11 @@ class CacheClear extends ContainerAwareCommand
|
|||||||
$output->writeln(sprintf("Clearing cache in <info>%s</info> directory", $dir));
|
$output->writeln(sprintf("Clearing cache in <info>%s</info> directory", $dir));
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$directoryBrowser = new \DirectoryIterator($dir);
|
$cacheEvent = new CacheEvent($dir);
|
||||||
|
$this->
|
||||||
|
getContainer()
|
||||||
|
->get('event_dispatcher')
|
||||||
|
->dispatch(TheliaEvents::CACHE_CLEAR, $cacheEvent);
|
||||||
} catch (\UnexpectedValueException $e) {
|
} catch (\UnexpectedValueException $e) {
|
||||||
// throws same exception code for does not exist and permission denied ...
|
// throws same exception code for does not exist and permission denied ...
|
||||||
if (!file_exists($dir)) {
|
if (!file_exists($dir)) {
|
||||||
@@ -82,15 +88,11 @@ class CacheClear extends ContainerAwareCommand
|
|||||||
}
|
}
|
||||||
|
|
||||||
throw $e;
|
throw $e;
|
||||||
}
|
|
||||||
|
|
||||||
$fs = new Filesystem();
|
|
||||||
try {
|
|
||||||
$fs->remove($dir);
|
|
||||||
|
|
||||||
$output->writeln(sprintf("<info>%s cache dir cleared successfully</info>", $dir));
|
|
||||||
} catch (IOException $e) {
|
} catch (IOException $e) {
|
||||||
$output->writeln(sprintf("Error during clearing cache : %s", $e->getMessage()));
|
$output->writeln(sprintf("Error during clearing cache : %s", $e->getMessage()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$output->writeln(sprintf("<info>%s cache dir cleared successfully</info>", $dir));
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -141,6 +141,11 @@
|
|||||||
<tag name="kernel.event_subscriber"/>
|
<tag name="kernel.event_subscriber"/>
|
||||||
</service>
|
</service>
|
||||||
|
|
||||||
|
<service id="thelia.action.cache" class="Thelia\Action\Cache">
|
||||||
|
<argument type="service" id="service_container"/>
|
||||||
|
<tag name="kernel.event_subscriber"/>
|
||||||
|
</service>
|
||||||
|
|
||||||
</services>
|
</services>
|
||||||
|
|
||||||
</config>
|
</config>
|
||||||
|
|||||||
67
core/lib/Thelia/Core/Event/Cache/CacheEvent.php
Normal file
67
core/lib/Thelia/Core/Event/Cache/CacheEvent.php
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
<?php
|
||||||
|
/*************************************************************************************/
|
||||||
|
/* */
|
||||||
|
/* Thelia */
|
||||||
|
/* */
|
||||||
|
/* Copyright (c) OpenStudio */
|
||||||
|
/* email : info@thelia.net */
|
||||||
|
/* web : http://www.thelia.net */
|
||||||
|
/* */
|
||||||
|
/* This program is free software; you can redistribute it and/or modify */
|
||||||
|
/* it under the terms of the GNU General Public License as published by */
|
||||||
|
/* the Free Software Foundation; either version 3 of the License */
|
||||||
|
/* */
|
||||||
|
/* This program is distributed in the hope that it will be useful, */
|
||||||
|
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
||||||
|
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
||||||
|
/* GNU General Public License for more details. */
|
||||||
|
/* */
|
||||||
|
/* You should have received a copy of the GNU General Public License */
|
||||||
|
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||||
|
/* */
|
||||||
|
/*************************************************************************************/
|
||||||
|
|
||||||
|
namespace Thelia\Core\Event\Cache;
|
||||||
|
|
||||||
|
use Thelia\Core\Event\ActionEvent;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class CacheEvent
|
||||||
|
* @package Thelia\Core\Event\Cache
|
||||||
|
* @author Manuel Raynaud <mraynaud@openstudio.fr>
|
||||||
|
*/
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -657,6 +657,13 @@ final class TheliaEvents
|
|||||||
|
|
||||||
const GENERATE_PDF = 'thelia.generatePdf';
|
const GENERATE_PDF = 'thelia.generatePdf';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* sent when a module is activated or deactivated
|
||||||
|
*/
|
||||||
const MODULE_TOGGLE_ACTIVATION = 'thelia.module.toggleActivation';
|
const MODULE_TOGGLE_ACTIVATION = 'thelia.module.toggleActivation';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* sent for clearing cache
|
||||||
|
*/
|
||||||
|
const CACHE_CLEAR = 'thelia.cache.clear';
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user