|
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
|
|
|
use Symfony\Component\Finder\Finder; |
|
20
|
|
|
|
|
21
|
|
|
class OpcacheCompileScriptsCommand extends AbstractCommand |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* {@inheritdoc} |
|
25
|
|
|
*/ |
|
26
|
19 |
|
protected function configure() |
|
27
|
|
|
{ |
|
28
|
|
|
$this |
|
29
|
19 |
|
->setName('opcache:compile:scripts') |
|
30
|
19 |
|
->setDescription('Compile scripts from path to the opcode cache') |
|
31
|
19 |
|
->addArgument('path', InputArgument::REQUIRED) |
|
32
|
19 |
|
->addOption( |
|
33
|
19 |
|
'exclude', |
|
34
|
19 |
|
null, |
|
35
|
19 |
|
InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL, |
|
36
|
19 |
|
'Exclude files from given path' |
|
37
|
|
|
) |
|
38
|
19 |
|
->addOption( |
|
39
|
19 |
|
'batch', |
|
40
|
19 |
|
null, |
|
41
|
19 |
|
InputOption::VALUE_NONE, |
|
42
|
19 |
|
'Compile all files at once, could be useful if pm.max_requests is too low' |
|
43
|
|
|
) |
|
44
|
19 |
|
->setHelp(''); |
|
45
|
19 |
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* {@inheritdoc} |
|
49
|
|
|
*/ |
|
50
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
51
|
|
|
{ |
|
52
|
|
|
$this->ensureExtensionLoaded('Zend OPcache'); |
|
53
|
|
|
$path = $input->getArgument('path'); |
|
54
|
|
|
|
|
55
|
|
|
$info = $this->getCacheTool()->opcache_get_status(true); |
|
56
|
|
|
|
|
57
|
|
|
if ($info === false) { |
|
58
|
|
|
throw new \RuntimeException('opcache_get_status(): No Opcache status info available. Perhaps Opcache is disabled via opcache.enable or opcache.enable_cli?'); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$exclude = []; |
|
62
|
|
|
if ($input->hasOption('exclude')) { |
|
63
|
|
|
$exclude = $input->getOption('exclude'); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$splFiles = $this->prepareFileList($path, $exclude); |
|
|
|
|
|
|
67
|
|
|
if ($input->getOption('batch')) { |
|
68
|
|
|
$this->compileBatch($splFiles, $output); |
|
69
|
|
|
} else { |
|
70
|
|
|
$this->compile($splFiles, $output); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param \Traversable|\SplFileInfo[] $splFiles |
|
76
|
|
|
* @param OutputInterface $output |
|
77
|
|
|
*/ |
|
78
|
|
|
protected function compile($splFiles, OutputInterface $output) |
|
79
|
|
|
{ |
|
80
|
|
|
$rows = []; |
|
81
|
|
|
foreach ($splFiles as $file) { |
|
82
|
|
|
$rows[] = [ |
|
83
|
|
|
$this->getCacheTool()->opcache_compile_file($file->getRealPath()), |
|
84
|
|
|
$file->getRealPath() |
|
85
|
|
|
]; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$table = new Table($output); |
|
89
|
|
|
$table |
|
90
|
|
|
->setHeaders(['Compiled', 'Filename']) |
|
91
|
|
|
->setRows($rows) |
|
92
|
|
|
; |
|
93
|
|
|
$table->render(); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param \Traversable|\SplFileInfo[] $splFiles |
|
98
|
|
|
* @param OutputInterface $output |
|
99
|
|
|
*/ |
|
100
|
|
|
protected function compileBatch($splFiles, OutputInterface $output) |
|
101
|
|
|
{ |
|
102
|
|
|
$paths = []; |
|
103
|
|
|
foreach ($splFiles as $file) { |
|
104
|
|
|
$paths []= $file->getRealPath(); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
$compiled = $this->getCacheTool()->opcache_compile_files($paths); |
|
108
|
|
|
|
|
109
|
|
|
$table = new Table($output); |
|
110
|
|
|
$table |
|
111
|
|
|
->setHeaders(['Compiled']) |
|
112
|
|
|
->setRows([[$compiled]]) |
|
113
|
|
|
; |
|
114
|
|
|
|
|
115
|
|
|
$table->render(); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @param string $path |
|
120
|
|
|
* @param array $exclude |
|
121
|
|
|
* |
|
122
|
|
|
* @return \Traversable|\SplFileInfo[] |
|
123
|
|
|
*/ |
|
124
|
|
|
private function prepareFileList($path, $exclude = []) |
|
125
|
|
|
{ |
|
126
|
|
|
return Finder::create() |
|
127
|
|
|
->files() |
|
128
|
|
|
->in($path) |
|
129
|
|
|
->name('*.php') |
|
130
|
|
|
->notPath('/Tests/') |
|
131
|
|
|
->notPath('/tests/') |
|
132
|
|
|
->notPath($exclude) |
|
133
|
|
|
->ignoreUnreadableDirs() |
|
134
|
|
|
->ignoreDotFiles(true) |
|
135
|
|
|
->ignoreVCS(true); |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.