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\CacheClearer\Psr6CacheClearer;
  22. use Symfony\Component\HttpKernel\Event\TerminateEvent;
  23. use Symfony\Component\HttpKernel\KernelEvents;
  24. use Symfony\Component\HttpKernel\KernelInterface;
  25. /**
  26.  * キャッシュ関連のユーティリティクラス.
  27.  */
  28. class CacheUtil implements EventSubscriberInterface
  29. {
  30.     public const DOCTRINE_APP_CACHE_KEY 'doctrine.app_cache_pool';
  31.     private $clearCacheAfterResponse false;
  32.     /**
  33.      * @var KernelInterface
  34.      */
  35.     protected $kernel;
  36.     /**
  37.      * @var ContainerInterface
  38.      */
  39.     private $container;
  40.     /**
  41.      * CacheUtil constructor.
  42.      *
  43.      * @param KernelInterface $kernel
  44.      * @param ContainerInterface $container
  45.      */
  46.     public function __construct(KernelInterface $kernelContainerInterface $container)
  47.     {
  48.         $this->kernel $kernel;
  49.         $this->container $container;
  50.     }
  51.     /**
  52.      * @param string $env
  53.      */
  54.     public function clearCache($env null)
  55.     {
  56.         $this->clearCacheAfterResponse $env;
  57.     }
  58.     public function forceClearCache(TerminateEvent $event)
  59.     {
  60.         if ($this->clearCacheAfterResponse === false) {
  61.             return;
  62.         }
  63.         $console = new Application($this->kernel);
  64.         $console->setAutoExit(false);
  65.         $command = [
  66.             'command' => 'cache:clear',
  67.             '--no-warmup' => true,
  68.             '--no-ansi' => true,
  69.         ];
  70.         if ($this->clearCacheAfterResponse !== null) {
  71.             $command['--env'] = $this->clearCacheAfterResponse;
  72.         }
  73.         $input = new ArrayInput($command);
  74.         $output = new BufferedOutput(
  75.             OutputInterface::VERBOSITY_DEBUG,
  76.             true
  77.         );
  78.         $console->run($input$output);
  79.         if (function_exists('opcache_reset')) {
  80.             opcache_reset();
  81.         }
  82.         if (function_exists('apc_clear_cache')) {
  83.             apc_clear_cache('user');
  84.             apc_clear_cache();
  85.         }
  86.         if (function_exists('wincache_ucache_clear')) {
  87.             wincache_ucache_clear();
  88.         }
  89.         return $output->fetch();
  90.     }
  91.     /**
  92.      * Doctrineのキャッシュを削除します.
  93.      *
  94.      * @return string
  95.      *
  96.      * @throws \Exception
  97.      */
  98.     public function clearDoctrineCache()
  99.     {
  100.         /** @var Psr6CacheClearer $poolClearer */
  101.         $poolClearer $this->container->get('cache.global_clearer');
  102.         if (!$poolClearer->hasPool(self::DOCTRINE_APP_CACHE_KEY)) {
  103.             return;
  104.         }
  105.         $console = new Application($this->kernel);
  106.         $console->setAutoExit(false);
  107.         $command = [
  108.             'command' => 'cache:pool:clear',
  109.             'pools' => [self::DOCTRINE_APP_CACHE_KEY],
  110.             '--no-ansi' => true,
  111.         ];
  112.         $input = new ArrayInput($command);
  113.         $output = new BufferedOutput(
  114.             OutputInterface::VERBOSITY_DEBUG,
  115.             true
  116.         );
  117.         $console->run($input$output);
  118.         return $output->fetch();
  119.     }
  120.     /**
  121.      * エンティティプロキシ(EntityExtensionアノテーションで拡張されたエンティティのapp/proxy/entity配下のファイル)を再生成します.
  122.      *
  123.      * コンソールコマンド `eccube:generate:proxies` と同じ処理をWebリクエスト経由で実行する.
  124.      * (このプロジェクトはbin/consoleコマンドを使わない運用のため、管理画面から実行できるようにしたもの)
  125.      *
  126.      * @return string
  127.      */
  128.     public function generateEntityProxies()
  129.     {
  130.         $console = new Application($this->kernel);
  131.         $console->setAutoExit(false);
  132.         $command = [
  133.             'command' => 'eccube:generate:proxies',
  134.             '--no-ansi' => true,
  135.         ];
  136.         $input = new ArrayInput($command);
  137.         $output = new BufferedOutput(
  138.             OutputInterface::VERBOSITY_DEBUG,
  139.             true
  140.         );
  141.         $console->run($input$output);
  142.         if (function_exists('opcache_reset')) {
  143.             opcache_reset();
  144.         }
  145.         return $output->fetch();
  146.     }
  147.     /**
  148.      * Twigキャッシュを削除します.
  149.      */
  150.     public function clearTwigCache()
  151.     {
  152.         $cacheDir $this->kernel->getCacheDir().'/twig';
  153.         $fs = new Filesystem();
  154.         $fs->remove($cacheDir);
  155.     }
  156.     /**
  157.      * キャッシュを削除する.
  158.      *
  159.      * doctrine, profiler, twig によって生成されたキャッシュディレクトリを削除する.
  160.      * キャッシュは $app['config']['root_dir'].'/app/cache' に生成されます.
  161.      *
  162.      * @param Application $app
  163.      * @param boolean $isAll .gitkeep を残してすべてのファイル・ディレクトリを削除する場合 true, 各ディレクトリのみを削除する場合 false
  164.      * @param boolean $isTwig Twigキャッシュファイルのみ削除する場合 true
  165.      *
  166.      * @return boolean 削除に成功した場合 true
  167.      *
  168.      * @deprecated CacheUtil::clearCacheを利用すること
  169.      */
  170.     public static function clear($app$isAll$isTwig false)
  171.     {
  172.         $cacheDir $app['config']['root_dir'].'/app/cache';
  173.         $filesystem = new Filesystem();
  174.         $finder Finder::create()->notName('.gitkeep')->files();
  175.         if ($isAll) {
  176.             $finder $finder->in($cacheDir);
  177.             $filesystem->remove($finder);
  178.         } elseif ($isTwig) {
  179.             if (is_dir($cacheDir.'/twig')) {
  180.                 $finder $finder->in($cacheDir.'/twig');
  181.                 $filesystem->remove($finder);
  182.             }
  183.         } else {
  184.             if (is_dir($cacheDir.'/doctrine')) {
  185.                 $finder $finder->in($cacheDir.'/doctrine');
  186.                 $filesystem->remove($finder);
  187.             }
  188.             if (is_dir($cacheDir.'/profiler')) {
  189.                 $finder $finder->in($cacheDir.'/profiler');
  190.                 $filesystem->remove($finder);
  191.             }
  192.             if (is_dir($cacheDir.'/twig')) {
  193.                 $finder $finder->in($cacheDir.'/twig');
  194.                 $filesystem->remove($finder);
  195.             }
  196.             if (is_dir($cacheDir.'/translator')) {
  197.                 $finder $finder->in($cacheDir.'/translator');
  198.                 $filesystem->remove($finder);
  199.             }
  200.         }
  201.         if (function_exists('opcache_reset')) {
  202.             opcache_reset();
  203.         }
  204.         if (function_exists('apc_clear_cache')) {
  205.             apc_clear_cache('user');
  206.             apc_clear_cache();
  207.         }
  208.         if (function_exists('wincache_ucache_clear')) {
  209.             wincache_ucache_clear();
  210.         }
  211.         return true;
  212.     }
  213.     /**
  214.      * {@inheritdoc}
  215.      */
  216.     public static function getSubscribedEvents()
  217.     {
  218.         return [KernelEvents::TERMINATE => 'forceClearCache'];
  219.     }
  220. }