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 $kernel, ContainerInterface $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. * Twigキャッシュを削除します.
  122. */
  123. public function clearTwigCache()
  124. {
  125. $cacheDir = $this->kernel->getCacheDir().'/twig';
  126. $fs = new Filesystem();
  127. $fs->remove($cacheDir);
  128. }
  129. /**
  130. * キャッシュを削除する.
  131. *
  132. * doctrine, profiler, twig によって生成されたキャッシュディレクトリを削除する.
  133. * キャッシュは $app['config']['root_dir'].'/app/cache' に生成されます.
  134. *
  135. * @param Application $app
  136. * @param boolean $isAll .gitkeep を残してすべてのファイル・ディレクトリを削除する場合 true, 各ディレクトリのみを削除する場合 false
  137. * @param boolean $isTwig Twigキャッシュファイルのみ削除する場合 true
  138. *
  139. * @return boolean 削除に成功した場合 true
  140. *
  141. * @deprecated CacheUtil::clearCacheを利用すること
  142. */
  143. public static function clear($app, $isAll, $isTwig = false)
  144. {
  145. $cacheDir = $app['config']['root_dir'].'/app/cache';
  146. $filesystem = new Filesystem();
  147. $finder = Finder::create()->notName('.gitkeep')->files();
  148. if ($isAll) {
  149. $finder = $finder->in($cacheDir);
  150. $filesystem->remove($finder);
  151. } elseif ($isTwig) {
  152. if (is_dir($cacheDir.'/twig')) {
  153. $finder = $finder->in($cacheDir.'/twig');
  154. $filesystem->remove($finder);
  155. }
  156. } else {
  157. if (is_dir($cacheDir.'/doctrine')) {
  158. $finder = $finder->in($cacheDir.'/doctrine');
  159. $filesystem->remove($finder);
  160. }
  161. if (is_dir($cacheDir.'/profiler')) {
  162. $finder = $finder->in($cacheDir.'/profiler');
  163. $filesystem->remove($finder);
  164. }
  165. if (is_dir($cacheDir.'/twig')) {
  166. $finder = $finder->in($cacheDir.'/twig');
  167. $filesystem->remove($finder);
  168. }
  169. if (is_dir($cacheDir.'/translator')) {
  170. $finder = $finder->in($cacheDir.'/translator');
  171. $filesystem->remove($finder);
  172. }
  173. }
  174. if (function_exists('opcache_reset')) {
  175. opcache_reset();
  176. }
  177. if (function_exists('apc_clear_cache')) {
  178. apc_clear_cache('user');
  179. apc_clear_cache();
  180. }
  181. if (function_exists('wincache_ucache_clear')) {
  182. wincache_ucache_clear();
  183. }
  184. return true;
  185. }
  186. /**
  187. * {@inheritdoc}
  188. */
  189. public static function getSubscribedEvents()
  190. {
  191. return [KernelEvents::TERMINATE => 'forceClearCache'];
  192. }
  193. }