src/Eccube/Util/CacheUtil.php line 67

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\DependencyInjection\ContainerInterface;
  18. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  19. use Symfony\Component\Filesystem\Filesystem;
  20. use Symfony\Component\Finder\Finder;
  21. use Symfony\Component\HttpKernel\Event\PostResponseEvent;
  22. use Symfony\Component\HttpKernel\KernelEvents;
  23. use Symfony\Component\HttpKernel\KernelInterface;
  24. /**
  25.  * キャッシュ関連のユーティリティクラス.
  26.  */
  27. class CacheUtil implements EventSubscriberInterface
  28. {
  29.     const DOCTRINE_APP_CACHE_KEY 'doctrine.app_cache_pool';
  30.     private $clearCacheAfterResponse false;
  31.     /**
  32.      * @var KernelInterface
  33.      */
  34.     protected $kernel;
  35.     /**
  36.      * @var ContainerInterface
  37.      */
  38.     private $container;
  39.     /**
  40.      * CacheUtil constructor.
  41.      *
  42.      * @param KernelInterface $kernel
  43.      * @param ContainerInterface $container
  44.      */
  45.     public function __construct(KernelInterface $kernelContainerInterface $container)
  46.     {
  47.         $this->kernel $kernel;
  48.         $this->container $container;
  49.     }
  50.     /**
  51.      * @param string $env
  52.      */
  53.     public function clearCache($env null)
  54.     {
  55.         $this->clearCacheAfterResponse $env;
  56.     }
  57.     public function forceClearCache(PostResponseEvent $event)
  58.     {
  59.         if ($this->clearCacheAfterResponse === false) {
  60.             return;
  61.         }
  62.         $console = new Application($this->kernel);
  63.         $console->setAutoExit(false);
  64.         $command = [
  65.             'command' => 'cache:clear',
  66.             '--no-warmup' => true,
  67.             '--no-ansi' => true,
  68.         ];
  69.         if ($this->clearCacheAfterResponse !== null) {
  70.             $command['--env'] = $this->clearCacheAfterResponse;
  71.         }
  72.         $input = new ArrayInput($command);
  73.         $output = new BufferedOutput(
  74.             OutputInterface::VERBOSITY_DEBUG,
  75.             true
  76.         );
  77.         $console->run($input$output);
  78.         if (function_exists('opcache_reset')) {
  79.             opcache_reset();
  80.         }
  81.         if (function_exists('apc_clear_cache')) {
  82.             apc_clear_cache('user');
  83.             apc_clear_cache();
  84.         }
  85.         if (function_exists('wincache_ucache_clear')) {
  86.             wincache_ucache_clear();
  87.         }
  88.         return $output->fetch();
  89.     }
  90.     /**
  91.      * Doctrineのキャッシュを削除します.
  92.      *
  93.      * @param null $env
  94.      *
  95.      * @return string
  96.      *
  97.      * @throws \Exception
  98.      */
  99.     public function clearDoctrineCache()
  100.     {
  101.         if (!$this->container->has(self::DOCTRINE_APP_CACHE_KEY)) {
  102.             return;
  103.         }
  104.         $console = new Application($this->kernel);
  105.         $console->setAutoExit(false);
  106.         $command = [
  107.             'command' => 'cache:pool:clear',
  108.             'pools' => [self::DOCTRINE_APP_CACHE_KEY],
  109.             '--no-ansi' => true,
  110.         ];
  111.         $input = new ArrayInput($command);
  112.         $output = new BufferedOutput(
  113.             OutputInterface::VERBOSITY_DEBUG,
  114.             true
  115.         );
  116.         $console->run($input$output);
  117.         return $output->fetch();
  118.     }
  119.     /**
  120.      * Twigキャッシュを削除します.
  121.      */
  122.     public function clearTwigCache()
  123.     {
  124.         $cacheDir $this->kernel->getCacheDir().'/twig';
  125.         $fs = new Filesystem();
  126.         $fs->remove($cacheDir);
  127.     }
  128.     /**
  129.      * キャッシュを削除する.
  130.      *
  131.      * doctrine, profiler, twig によって生成されたキャッシュディレクトリを削除する.
  132.      * キャッシュは $app['config']['root_dir'].'/app/cache' に生成されます.
  133.      *
  134.      * @param Application $app
  135.      * @param boolean $isAll .gitkeep を残してすべてのファイル・ディレクトリを削除する場合 true, 各ディレクトリのみを削除する場合 false
  136.      * @param boolean $isTwig Twigキャッシュファイルのみ削除する場合 true
  137.      *
  138.      * @return boolean 削除に成功した場合 true
  139.      *
  140.      * @deprecated CacheUtil::clearCacheを利用すること
  141.      */
  142.     public static function clear($app$isAll$isTwig false)
  143.     {
  144.         $cacheDir $app['config']['root_dir'].'/app/cache';
  145.         $filesystem = new Filesystem();
  146.         $finder Finder::create()->notName('.gitkeep')->files();
  147.         if ($isAll) {
  148.             $finder $finder->in($cacheDir);
  149.             $filesystem->remove($finder);
  150.         } elseif ($isTwig) {
  151.             if (is_dir($cacheDir.'/twig')) {
  152.                 $finder $finder->in($cacheDir.'/twig');
  153.                 $filesystem->remove($finder);
  154.             }
  155.         } else {
  156.             if (is_dir($cacheDir.'/doctrine')) {
  157.                 $finder $finder->in($cacheDir.'/doctrine');
  158.                 $filesystem->remove($finder);
  159.             }
  160.             if (is_dir($cacheDir.'/profiler')) {
  161.                 $finder $finder->in($cacheDir.'/profiler');
  162.                 $filesystem->remove($finder);
  163.             }
  164.             if (is_dir($cacheDir.'/twig')) {
  165.                 $finder $finder->in($cacheDir.'/twig');
  166.                 $filesystem->remove($finder);
  167.             }
  168.             if (is_dir($cacheDir.'/translator')) {
  169.                 $finder $finder->in($cacheDir.'/translator');
  170.                 $filesystem->remove($finder);
  171.             }
  172.         }
  173.         if (function_exists('opcache_reset')) {
  174.             opcache_reset();
  175.         }
  176.         if (function_exists('apc_clear_cache')) {
  177.             apc_clear_cache('user');
  178.             apc_clear_cache();
  179.         }
  180.         if (function_exists('wincache_ucache_clear')) {
  181.             wincache_ucache_clear();
  182.         }
  183.         return true;
  184.     }
  185.     /**
  186.      * {@inheritdoc}
  187.      */
  188.     public static function getSubscribedEvents()
  189.     {
  190.         return [KernelEvents::TERMINATE => 'forceClearCache'];
  191.     }
  192. }