src/WebBundle/Command/CacheClearCommand.php line 20

Open in your IDE?
  1. <?php
  2. namespace WebBundle\Command;
  3. use FlexApp\Classes\Constants;
  4. use Symfony\Component\Console\Command\LockableTrait;
  5. use Symfony\Component\Console\Input\InputInterface;
  6. use Symfony\Component\Console\Output\OutputInterface;
  7. use WebBundle\Helper\App;
  8. /**
  9.  * class: CacheClearCommand
  10.  * php bin/console app:cache:clear
  11.  * php bin/console app:cache:clear -t trans
  12.  * -----------------------------------------------------
  13.  * Created by MihailShirnin on 09.04.2017.
  14.  * @package WebBundle\Command
  15.  */
  16. class CacheClearCommand extends ExtendedCommand
  17. {
  18.     use LockableTrait;
  19.     protected $nameCommand 'app:cache:clear';
  20.     protected function execute(InputInterface $inputOutputInterface $output)
  21.     {
  22.         parent::execute($input$output);
  23.         if (!$this->lock()) {
  24.             $this->io->error('Кеш уже кем то очищается. Может его уже не надо чистить?!');
  25.         } else {
  26.             $optType $input->getOption('type');
  27.             $optVal $input->getOption('par');
  28.             if ($optType == 'trans') {
  29.                 $this->runClearCacheTranslate();
  30.             } elseif ($optType == 'mem') {
  31.                 $this->runClearCacheMemcache($optVal);
  32.             } else {
  33.                 $this->runClearCacheAllFiles();
  34.             }
  35.         }
  36.         return Constants::COMMAND_SUCCESS_CODE;
  37.     }
  38.     /**
  39.      * Читка кеша
  40.      * php bin/console app:cache:clear
  41.      */
  42.     private function runClearCacheAllFiles()
  43.     {
  44.         $arrEnv = [/*'te', */'te_dev'];
  45.         $this->io->title("Чистим файловый кеш , ничего не тыкаем, ждем ...");
  46.         $oCacheService App::getContainer()->get('app.service.cache');
  47.         $this->loadRequiredOnConsoleTerminateEventServices(); //Загружаем необходимые сервисы до того, как будет удален весь кеш, иначе потом скрипт не сможет к ним обратиться, т.к. в sf 3.4 новый кеш генерируется в подпапке с другим именем.
  48.         foreach ($arrEnv as $env) {
  49.             if ($oCacheService->clearCacheFilesEnv($env)) {
  50.                 $this->io->success("Кеш для '{$env}' очищен");
  51.             }
  52.         }
  53.         $this->io->section('Загружаем сразу сайт из параметра <info>full_domain</info>, что бы сформировать новый кеш');
  54.         $ch curl_init();
  55.         curl_setopt($chCURLOPT_URLApp::getParameter('full_domain'));
  56.         //curl вернет нам ответ, а не выведет
  57.         curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
  58.         curl_exec($ch);
  59.         curl_close($ch);
  60.         $this->io->success("Готово, кеш очищен и перестроен.");
  61.     }
  62.     /**
  63.      * Читка кеша переводов
  64.      * php bin/console app:cache:clear -t trans
  65.      */
  66.     private function runClearCacheTranslate()
  67.     {
  68.         $this->io->title("Чистим кеш переводов");
  69.         $oCacheService App::getContainer()->get('app.service.cache');
  70.         if ($oCacheService->clearCacheTranslate(true)) {
  71.             $this->io->success("Готово.");
  72.         } else {
  73.             $this->io->error("Что то пошло не так. Необходимо разобраться.");
  74.         }
  75.     }
  76.     /**
  77.      * Читка Memcache
  78.      * php bin/console app:cache:clear -t mem -p $key
  79.      * @param $key
  80.      */
  81.     private function runClearCacheMemcache($key)
  82.     {
  83.         $this->io->title("Чистим выборочно значения Memcache");
  84.         $oCacheService App::getContainer()->get('app.service.cache');
  85.         if ($oCacheService->clearValMemcache($keytrue)) {
  86.             $this->io->success("Готово.");
  87.         } else {
  88.             $this->io->error("Что то пошло не так. Необходимо разобраться.");
  89.         }
  90.     }
  91.     private function loadRequiredOnConsoleTerminateEventServices(): void
  92.     {
  93.         $this->getContainer()->get('swiftmailer.email_sender.listener');
  94.         $this->getContainer()->get('console.error_listener');
  95.     }
  96. }