|
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\Input\InputInterface; |
|
15
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
16
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
17
|
|
|
use Symfony\Component\Console\Question\ConfirmationQuestion; |
|
18
|
|
|
|
|
19
|
|
|
class OpcacheResetFileCacheCommand extends AbstractOpcacheCommand |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* {@inheritdoc} |
|
23
|
|
|
*/ |
|
24
|
19 |
|
protected function configure() |
|
25
|
|
|
{ |
|
26
|
|
|
$this |
|
27
|
19 |
|
->setName('opcache:reset:file-cache') |
|
28
|
19 |
|
->setDescription('Deletes all contents of the file cache directory') |
|
29
|
19 |
|
->addOption('force', 'f', InputOption::VALUE_NONE, 'Delete files without questioning') |
|
30
|
19 |
|
->setHelp(''); |
|
31
|
19 |
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* {@inheritdoc} |
|
35
|
|
|
*/ |
|
36
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
|
37
|
|
|
{ |
|
38
|
|
|
$this->ensureExtensionLoaded('Zend OPcache'); |
|
39
|
|
|
|
|
40
|
|
|
$force = $input->getOption('force'); |
|
|
|
|
|
|
41
|
|
|
$info = $this->getCacheTool()->opcache_get_status(); |
|
42
|
|
|
$fileCache = $info['file_cache'] ?? false; |
|
43
|
|
|
|
|
44
|
|
|
if (!$fileCache) { |
|
45
|
|
|
throw new \RuntimeException('opcache.file_cache is not set.'); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
if (!$input->getOption('force')) { |
|
49
|
|
|
$question = new ConfirmationQuestion( |
|
50
|
|
|
"Are you sure you want to delete the contents of <comment>${fileCache}</comment>? [no] ", |
|
51
|
|
|
false, |
|
52
|
|
|
'/^y/i' |
|
53
|
|
|
); |
|
54
|
|
|
|
|
55
|
|
|
$helper = $this->getHelper('question'); |
|
56
|
|
|
$result = $helper->ask($input, $output, $question); |
|
57
|
|
|
|
|
58
|
|
|
if (!$result) { |
|
59
|
|
|
$output->writeln('<info>Aborted file deletion</info>'); |
|
60
|
|
|
return 0; |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
$deleted = $this->performDelete($fileCache); |
|
66
|
|
|
$output->writeln("<info>Deleted <comment>{$deleted}</comment> files.</info>"); |
|
67
|
|
|
|
|
68
|
|
|
return 0; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param string $directory |
|
73
|
|
|
*/ |
|
74
|
|
|
protected function performDelete($directory) { |
|
75
|
|
|
$count = 0; |
|
76
|
|
|
$it = new \RecursiveDirectoryIterator($directory, \RecursiveDirectoryIterator::SKIP_DOTS); |
|
77
|
|
|
$files = new \RecursiveIteratorIterator($it, \RecursiveIteratorIterator::CHILD_FIRST); |
|
78
|
|
|
|
|
79
|
|
|
foreach($files as $file) { |
|
80
|
|
|
if ($file->isDir()){ |
|
81
|
|
|
rmdir($file->getRealPath()); |
|
82
|
|
|
} else { |
|
83
|
|
|
unlink($file->getRealPath()); |
|
84
|
|
|
} |
|
85
|
|
|
$count += 1; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return $count; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.