1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* this file is part of magerun |
4
|
|
|
* |
5
|
|
|
* @author Tom Klingenberg <https://github.com/ktomk> |
6
|
|
|
*/ |
7
|
|
|
namespace N98\Magento\Command\Cache\Dir; |
8
|
|
|
|
9
|
|
|
use FilesystemIterator; |
10
|
|
|
use N98\Magento\Command\AbstractMagentoCommand; |
11
|
|
|
use N98\Util\Filesystem; |
12
|
|
|
use RuntimeException; |
13
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
14
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class FlushCommand |
18
|
|
|
* |
19
|
|
|
* @package N98\Magento\Command\Cache |
20
|
|
|
*/ |
21
|
|
|
class FlushCommand extends AbstractMagentoCommand |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var OutputInterface |
25
|
|
|
*/ |
26
|
|
|
private $output; |
27
|
|
|
|
28
|
|
|
const NAME = 'cache:dir:flush'; |
29
|
|
|
|
30
|
|
|
protected function configure() |
31
|
|
|
{ |
32
|
|
|
$this |
33
|
|
|
->setName(FlushCommand::NAME) |
34
|
|
|
->setDescription('Flush (empty) Magento cache directory'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function getHelp() |
38
|
|
|
{ |
39
|
|
|
return <<<HELP |
40
|
|
|
The default cache backend is the files cache in Magento. The default |
41
|
|
|
directory of that default cache backend is the directory "var/cache" |
42
|
|
|
within the Magento web-root directory (should be blocked from external |
43
|
|
|
access). |
44
|
|
|
|
45
|
|
|
The cache:dir:flish Magerun command will remove all files within that |
46
|
|
|
directory. This is currently the most purist form to reset default |
47
|
|
|
caching configuration in Magento. |
48
|
|
|
|
49
|
|
|
Flushing the cache directory can help to re-initialize the whole Magento |
50
|
|
|
application after it got stuck in cached configuration like a half-done |
51
|
|
|
cache initialization, old config data within the files cache and similar. |
52
|
|
|
HELP; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param InputInterface $input |
57
|
|
|
* @param OutputInterface $output |
58
|
|
|
* |
59
|
|
|
* @return int|void |
60
|
|
|
*/ |
61
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
62
|
|
|
{ |
63
|
|
|
$this->output = $output; |
64
|
|
|
$this->detectMagento($output, true); |
65
|
|
|
|
66
|
|
|
if (!$this->initMagento()) { |
67
|
|
|
return; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$workingDirectory = getcwd(); |
71
|
|
|
$magentoRootFolder = $this->getApplication()->getMagentoRootFolder(); |
72
|
|
|
$cacheDir = $magentoRootFolder . '/var/cache'; |
73
|
|
|
|
74
|
|
|
$output->writeln(sprintf('<info>Flushing cache directory <comment>%s</comment></info>', $cacheDir)); |
75
|
|
|
|
76
|
|
|
$this->verbose(sprintf('<debug>root-dir: <comment>%s</comment>', $magentoRootFolder)); |
77
|
|
|
$this->verbose(sprintf('<debug>cwd: <comment>%s</comment>', $workingDirectory)); |
78
|
|
|
|
79
|
|
|
$this->emptyDirectory($cacheDir); |
80
|
|
|
|
81
|
|
|
$output->writeln('Cache directory flushed'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param string $path |
86
|
|
|
* |
87
|
|
|
* @return bool |
88
|
|
|
*/ |
89
|
|
|
private function emptyDirectory($path) |
90
|
|
|
{ |
91
|
|
|
$errors = array(); |
92
|
|
|
|
93
|
|
|
$dir = new FilesystemIterator($path); |
94
|
|
|
foreach ($dir as $file => $info) { |
95
|
|
|
if ($info->isDir()) { |
96
|
|
|
$this->verbose( |
97
|
|
|
'<debug>Filesystem::recursiveRemoveDirectory() <comment>' . $file . '</comment></debug>' |
98
|
|
|
); |
99
|
|
|
if (!isset($fs)) { |
100
|
|
|
$fs = new Filesystem(); |
101
|
|
|
} |
102
|
|
|
if (!$fs->recursiveRemoveDirectory($file)) { |
103
|
|
|
$errors[] = $file; |
104
|
|
|
}; |
105
|
|
|
} else { |
106
|
|
|
$this->verbose('<debug>unlink() <comment>' . $file . '</comment></debug>'); |
107
|
|
|
if (!unlink($file)) { |
108
|
|
|
$errors[] = $file; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
if (!$errors) { |
114
|
|
|
return true; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$message = sprintf("Failed to empty directory %s, unable to remove:\n", var_export($path, true)); |
118
|
|
|
foreach ($errors as $error) { |
119
|
|
|
$message .= sprintf(" - %s\n", var_export($error, true)); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
throw new RuntimeException($message); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param string $message |
127
|
|
|
*/ |
128
|
|
|
private function verbose($message) |
129
|
|
|
{ |
130
|
|
|
$output = $this->output; |
131
|
|
|
|
132
|
|
|
if (!$output) { |
133
|
|
|
return; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
if (OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) { |
137
|
|
|
$output->writeln($message); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|