src/Eccube/Util/CacheUtil.php line 57

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of EC-CUBE
  4.  *
  5.  * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
  6.  *
  7.  * http://www.ec-cube.co.jp/
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Eccube\Util;
  13. use Symfony\Bundle\FrameworkBundle\Console\Application;
  14. use Symfony\Component\Console\Input\ArrayInput;
  15. use Symfony\Component\Console\Output\BufferedOutput;
  16. use Symfony\Component\Console\Output\OutputInterface;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. use Symfony\Component\Filesystem\Filesystem;
  19. use Symfony\Component\Finder\Finder;
  20. use Symfony\Component\HttpKernel\Event\PostResponseEvent;
  21. use Symfony\Component\HttpKernel\KernelEvents;
  22. use Symfony\Component\HttpKernel\KernelInterface;
  23. /**
  24.  * キャッシュ関連のユーティリティクラス.
  25.  */
  26. class CacheUtil implements EventSubscriberInterface
  27. {
  28.     private $clearCacheAfterResponse false;
  29.     /**
  30.      * @var KernelInterface
  31.      */
  32.     protected $kernel;
  33.     /**
  34.      * CacheUtil constructor.
  35.      *
  36.      * @param KernelInterface $kernel
  37.      */
  38.     public function __construct(KernelInterface $kernel)
  39.     {
  40.         $this->kernel $kernel;
  41.     }
  42.     /**
  43.      * @param string $env
  44.      */
  45.     public function clearCache($env null)
  46.     {
  47.         $this->clearCacheAfterResponse $env;
  48.     }
  49.     public function forceClearCache(PostResponseEvent $event)
  50.     {
  51.         if ($this->clearCacheAfterResponse === false) {
  52.             return;
  53.         }
  54.         $console = new Application($this->kernel);
  55.         $console->setAutoExit(false);
  56.         $command = [
  57.             'command' => 'cache:clear',
  58.             '--no-warmup' => true,
  59.             '--no-ansi' => true,
  60.         ];
  61.         if ($this->clearCacheAfterResponse !== null) {
  62.             $command['--env'] = $this->clearCacheAfterResponse;
  63.         }
  64.         $input = new ArrayInput($command);
  65.         $output = new BufferedOutput(
  66.             OutputInterface::VERBOSITY_DEBUG,
  67.             true
  68.         );
  69.         $console->run($input$output);
  70.         if (function_exists('opcache_reset')) {
  71.             opcache_reset();
  72.         }
  73.         if (function_exists('apc_clear_cache')) {
  74.             apc_clear_cache('user');
  75.             apc_clear_cache();
  76.         }
  77.         if (function_exists('wincache_ucache_clear')) {
  78.             wincache_ucache_clear();
  79.         }
  80.         return $output->fetch();
  81.     }
  82.     /**
  83.      * Doctrineのキャッシュを削除します.
  84.      * APP_ENV=prodの場合のみ実行されます.
  85.      *
  86.      * @param null $env
  87.      *
  88.      * @return string
  89.      *
  90.      * @throws \Exception
  91.      */
  92.     public function clearDoctrineCache()
  93.     {
  94.         if ($this->kernel->getEnvironment() !== 'prod') {
  95.             return;
  96.         }
  97.         $console = new Application($this->kernel);
  98.         $console->setAutoExit(false);
  99.         $command = [
  100.             'command' => 'cache:pool:clear',
  101.             'pools' => ['doctrine.app_cache_pool'],
  102.             '--no-ansi' => true,
  103.         ];
  104.         $input = new ArrayInput($command);
  105.         $output = new BufferedOutput(
  106.             OutputInterface::VERBOSITY_DEBUG,
  107.             true
  108.         );
  109.         $console->run($input$output);
  110.         return $output->fetch();
  111.     }
  112.     /**
  113.      * Twigキャッシュを削除します.
  114.      */
  115.     public function clearTwigCache()
  116.     {
  117.         $cacheDir $this->kernel->getCacheDir().'/twig';
  118.         $fs = new Filesystem();
  119.         $fs->remove($cacheDir);
  120.     }
  121.     /**
  122.      * キャッシュを削除する.
  123.      *
  124.      * doctrine, profiler, twig によって生成されたキャッシュディレクトリを削除する.
  125.      * キャッシュは $app['config']['root_dir'].'/app/cache' に生成されます.
  126.      *
  127.      * @param Application $app
  128.      * @param boolean $isAll .gitkeep を残してすべてのファイル・ディレクトリを削除する場合 true, 各ディレクトリのみを削除する場合 false
  129.      * @param boolean $isTwig Twigキャッシュファイルのみ削除する場合 true
  130.      *
  131.      * @return boolean 削除に成功した場合 true
  132.      *
  133.      * @deprecated CacheUtil::clearCacheを利用すること
  134.      */
  135.     public static function clear($app$isAll$isTwig false)
  136.     {
  137.         $cacheDir $app['config']['root_dir'].'/app/cache';
  138.         $filesystem = new Filesystem();
  139.         $finder Finder::create()->notName('.gitkeep')->files();
  140.         if ($isAll) {
  141.             $finder $finder->in($cacheDir);
  142.             $filesystem->remove($finder);
  143.         } elseif ($isTwig) {
  144.             if (is_dir($cacheDir.'/twig')) {
  145.                 $finder $finder->in($cacheDir.'/twig');
  146.                 $filesystem->remove($finder);
  147.             }
  148.         } else {
  149.             if (is_dir($cacheDir.'/doctrine')) {
  150.                 $finder $finder->in($cacheDir.'/doctrine');
  151.                 $filesystem->remove($finder);
  152.             }
  153.             if (is_dir($cacheDir.'/profiler')) {
  154.                 $finder $finder->in($cacheDir.'/profiler');
  155.                 $filesystem->remove($finder);
  156.             }
  157.             if (is_dir($cacheDir.'/twig')) {
  158.                 $finder $finder->in($cacheDir.'/twig');
  159.                 $filesystem->remove($finder);
  160.             }
  161.             if (is_dir($cacheDir.'/translator')) {
  162.                 $finder $finder->in($cacheDir.'/translator');
  163.                 $filesystem->remove($finder);
  164.             }
  165.         }
  166.         if (function_exists('opcache_reset')) {
  167.             opcache_reset();
  168.         }
  169.         if (function_exists('apc_clear_cache')) {
  170.             apc_clear_cache('user');
  171.             apc_clear_cache();
  172.         }
  173.         if (function_exists('wincache_ucache_clear')) {
  174.             wincache_ucache_clear();
  175.         }
  176.         return true;
  177.     }
  178.     /**
  179.      * {@inheritdoc}
  180.      */
  181.     public static function getSubscribedEvents()
  182.     {
  183.         return [KernelEvents::TERMINATE => 'forceClearCache'];
  184.     }
  185. }