<?php
namespace AppBundle\EventListener;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\DependencyInjection\ContainerInterface as Container;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
/**
* Class KernelEventListener
* @package AppBundle\EventListener
*/
class KernelEventListener
{
protected $container;
public function __construct(Container $container)
{
$this->container = $container;
}
/**
* Controller に before() メソッドがある場合は事前に実行する。
*
* @param FilterControllerEvent $event
*/
public function onKernelController(FilterControllerEvent $event)
{
$controller = $event->getController();
$request = $event->getRequest();
if (!is_array($controller)) {
return;
}
$this->initConfig();
if(method_exists($controller[0], 'before') ){
$controller[0]->before($event);
}
if(method_exists($controller[0], 'beforeFilter') ){
$controller[0]->beforeFilter($event);
}
}
public function onKernelFinishRequest(FinishRequestEvent $event)
{
$configDir = CMS__ADMIN_DIR . 'cms/app/config/';
// 置き換え予定のファイルが有れば置き換える
$cacheClearRequired = false;
if(file_exists($configDir . '_bundles.xml')){
rename($configDir . '_bundles.xml', $configDir . 'bundles.xml');
$cacheClearRequired = true;
}
if(file_exists($configDir . '_mod_routing.yml')){
rename($configDir . '_mod_routing.yml', $configDir . 'mod_routing.yml');
$cacheClearRequired = true;
}
if($cacheClearRequired){
$app = new Application($this->container->get('kernel'));
$app->setAutoExit(false);
$input = new ArrayInput([
'command' => 'cache:clear'
]);
$output = new BufferedOutput();
$app->run($input, $output);
}
}
public function onKernelResponse(FilterResponseEvent $event)
{
$response = $event->getResponse();
}
protected function initConfig()
{
$config = $this->container->get('app.config');
if($this->container->hasParameter('app.config')){
$params = $this->container->getParameter('app.config');
} else {
$params = [];
}
if(is_array($params)) {
foreach ($params as $key => $value) {
$config->set($key, $value);
}
}
}
}