|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of CacheTool. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Samuel Gordalina <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace CacheTool\Command; |
|
13
|
|
|
|
|
14
|
|
|
use Symfony\Component\Console\Helper\Table; |
|
15
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
16
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
17
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
18
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
19
|
|
|
|
|
20
|
|
|
class OpcacheInvalidateScriptsCommand extends AbstractCommand |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* {@inheritdoc} |
|
24
|
|
|
*/ |
|
25
|
5 |
|
protected function configure() |
|
26
|
|
|
{ |
|
27
|
5 |
|
$this |
|
28
|
5 |
|
->setName('opcache:invalidate:scripts') |
|
29
|
5 |
|
->setDescription('Remove scripts from the opcode cache') |
|
30
|
5 |
|
->addArgument('path', InputArgument::REQUIRED) |
|
31
|
5 |
|
->addOption('force', 'f', InputOption::VALUE_NONE, 'Force scripts invalidation') |
|
32
|
5 |
|
->setHelp(''); |
|
33
|
5 |
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* {@inheritdoc} |
|
37
|
|
|
*/ |
|
38
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->ensureExtensionLoaded('Zend OPcache'); |
|
41
|
|
|
$path = $input->getArgument('path'); |
|
42
|
|
|
$force = $input->getOption('force'); |
|
43
|
|
|
|
|
44
|
|
|
$info = $this->getCacheTool()->opcache_get_status(true); |
|
45
|
|
|
|
|
46
|
|
|
if ($info === false) { |
|
47
|
|
|
throw new \RuntimeException('opcache_get_status(): No Opcache status info available. Perhaps Opcache is disabled via opcache.enable or opcache.enable_cli?'); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
$table = new Table($output); |
|
51
|
|
|
$table |
|
52
|
|
|
->setHeaders(array( |
|
53
|
|
|
'Cleaned', |
|
54
|
|
|
'Filename' |
|
55
|
|
|
)) |
|
56
|
|
|
->setRows($this->processFilelist($info['scripts'], $path, $force)) |
|
57
|
|
|
; |
|
58
|
|
|
|
|
59
|
|
|
$table->render(); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
protected function processFileList(array $cacheList, $path, $force) |
|
63
|
|
|
{ |
|
64
|
|
|
$list = array(); |
|
65
|
|
|
|
|
66
|
|
|
sort($cacheList); |
|
67
|
|
|
|
|
68
|
|
|
foreach ($cacheList as $item) { |
|
69
|
|
|
$filename = $this->processFilename($item['full_path']); |
|
70
|
|
|
if (preg_match('|' . $path . '|', $filename)) { |
|
71
|
|
|
$list[] = array( |
|
72
|
|
|
$this->getCacheTool()->opcache_invalidate($filename, $force), |
|
73
|
|
|
$filename |
|
74
|
|
|
); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return $list; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
protected function processFilename($filename) |
|
82
|
|
|
{ |
|
83
|
|
|
$dir = getcwd(); |
|
84
|
|
|
|
|
85
|
|
|
if (0 === strpos($filename, $dir)) { |
|
86
|
|
|
return "." . substr($filename, strlen($dir)); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return $filename; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|