Skip to content

Commit 9b2510b

Browse files
committed
Add ability to exclude files from opcode cache listing
1 parent f7b47d4 commit 9b2510b

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

src/Command/OpcacheStatusScriptsCommand.php

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use CacheTool\Util\Formatter;
1515
use Symfony\Component\Console\Helper\Table;
1616
use Symfony\Component\Console\Input\InputInterface;
17+
use Symfony\Component\Console\Input\InputOption;
1718
use Symfony\Component\Console\Output\OutputInterface;
1819

1920
class OpcacheStatusScriptsCommand extends AbstractOpcacheCommand
@@ -26,7 +27,13 @@ protected function configure()
2627
$this
2728
->setName('opcache:status:scripts')
2829
->setDescription('Show scripts in the opcode cache')
29-
->setHelp('');
30+
->setHelp('')
31+
->addOption(
32+
'exclude',
33+
'e',
34+
InputOption::VALUE_OPTIONAL,
35+
'Exclude scripts that match this regex. Example: `.*vendor.*`. Delimiters are not needed.'
36+
);
3037
}
3138

3239
/**
@@ -39,26 +46,31 @@ protected function execute(InputInterface $input, OutputInterface $output): int
3946
$info = $this->getCacheTool()->opcache_get_status(true);
4047
$this->ensureSuccessfulOpcacheCall($info);
4148

49+
$exclude = $input->getOption('exclude') ?? null;
50+
4251
$table = new Table($output);
4352
$table
4453
->setHeaders([
4554
'Hits',
4655
'Memory',
4756
'Filename'
4857
])
49-
->setRows($this->processFilelist($info['scripts']))
58+
->setRows($this->processFilelist($info['scripts'], $exclude))
5059
;
5160

5261
$table->render();
5362

5463
return 0;
5564
}
5665

57-
protected function processFileList(array $cacheList)
58-
{
66+
protected function processFileList(
67+
array $cacheList,
68+
string $exclude = null
69+
) {
5970
$list = [];
6071

61-
foreach ($cacheList as $item) {
72+
$filteredList = $exclude ? $this->excludeFiles($cacheList, $exclude) : $cacheList;
73+
foreach ($filteredList as $item) {
6274
$list[] = [
6375
number_format($item['hits']),
6476
Formatter::bytes($item['memory_consumption']),
@@ -68,4 +80,9 @@ protected function processFileList(array $cacheList)
6880

6981
return $list;
7082
}
83+
84+
protected function excludeFiles(array $cacheList, string $exclude = null): array
85+
{
86+
return array_intersect_key($cacheList, array_flip(preg_grep("({$exclude})", array_keys($cacheList), \PREG_GREP_INVERT)));
87+
}
7188
}

0 commit comments

Comments
 (0)