<?php
namespace WebBundle\Command;
use FlexApp\Classes\Constants;
use Symfony\Component\Console\Command\LockableTrait;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use WebBundle\Helper\App;
/**
* class: CacheClearCommand
* php bin/console app:cache:clear
* php bin/console app:cache:clear -t trans
* -----------------------------------------------------
* Created by MihailShirnin on 09.04.2017.
* @package WebBundle\Command
*/
class CacheClearCommand extends ExtendedCommand
{
use LockableTrait;
protected $nameCommand = 'app:cache:clear';
protected function execute(InputInterface $input, OutputInterface $output)
{
parent::execute($input, $output);
if (!$this->lock()) {
$this->io->error('Кеш уже кем то очищается. Может его уже не надо чистить?!');
} else {
$optType = $input->getOption('type');
$optVal = $input->getOption('par');
if ($optType == 'trans') {
$this->runClearCacheTranslate();
} elseif ($optType == 'mem') {
$this->runClearCacheMemcache($optVal);
} else {
$this->runClearCacheAllFiles();
}
}
return Constants::COMMAND_SUCCESS_CODE;
}
/**
* Читка кеша
* php bin/console app:cache:clear
*/
private function runClearCacheAllFiles()
{
$arrEnv = [/*'te', */'te_dev'];
$this->io->title("Чистим файловый кеш , ничего не тыкаем, ждем ...");
$oCacheService = App::getContainer()->get('app.service.cache');
$this->loadRequiredOnConsoleTerminateEventServices(); //Загружаем необходимые сервисы до того, как будет удален весь кеш, иначе потом скрипт не сможет к ним обратиться, т.к. в sf 3.4 новый кеш генерируется в подпапке с другим именем.
foreach ($arrEnv as $env) {
if ($oCacheService->clearCacheFilesEnv($env)) {
$this->io->success("Кеш для '{$env}' очищен");
}
}
$this->io->section('Загружаем сразу сайт из параметра <info>full_domain</info>, что бы сформировать новый кеш');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, App::getParameter('full_domain'));
//curl вернет нам ответ, а не выведет
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
curl_close($ch);
$this->io->success("Готово, кеш очищен и перестроен.");
}
/**
* Читка кеша переводов
* php bin/console app:cache:clear -t trans
*/
private function runClearCacheTranslate()
{
$this->io->title("Чистим кеш переводов");
$oCacheService = App::getContainer()->get('app.service.cache');
if ($oCacheService->clearCacheTranslate(true)) {
$this->io->success("Готово.");
} else {
$this->io->error("Что то пошло не так. Необходимо разобраться.");
}
}
/**
* Читка Memcache
* php bin/console app:cache:clear -t mem -p $key
* @param $key
*/
private function runClearCacheMemcache($key)
{
$this->io->title("Чистим выборочно значения Memcache");
$oCacheService = App::getContainer()->get('app.service.cache');
if ($oCacheService->clearValMemcache($key, true)) {
$this->io->success("Готово.");
} else {
$this->io->error("Что то пошло не так. Необходимо разобраться.");
}
}
private function loadRequiredOnConsoleTerminateEventServices(): void
{
$this->getContainer()->get('swiftmailer.email_sender.listener');
$this->getContainer()->get('console.error_listener');
}
}