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