forked from symfony/symfony
/
CacheClearCommand.php
132 lines (112 loc) · 3.83 KB
/
CacheClearCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\FrameworkBundle\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Finder\Finder;
/**
* Clear and Warmup the cache.
*
* @author Francis Besset <francis.besset@gmail.com>
* @author Fabien Potencier <fabien@symfony.com>
*/
class CacheClearCommand extends Command
{
/**
* @see Command
*/
protected function configure()
{
$this
->setName('cache:clear')
->setDefinition(array(
new InputOption('no-warmup', '', InputOption::VALUE_NONE, 'Do not warm up the cache'),
new InputOption('without-debug', '', InputOption::VALUE_NONE, 'If the cache is warmed up, whether to disable debugging or not'),
))
->setDescription('Clear the cache')
->setHelp(<<<EOF
The <info>cache:clear</info> command clears the application cache for a given environment
and debug mode:
<info>./app/console cache:clear dev</info>
<info>./app/console cache:clear prod --without-debug</info>
EOF
)
;
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$realCacheDir = $this->container->getParameter('kernel.cache_dir');
$oldCacheDir = $realCacheDir.'_old';
if (!is_writable($realCacheDir)) {
throw new \RuntimeException(sprintf('Unable to write in the "%s" directory', $this->realCacheDir));
}
if ($input->getOption('no-warmup')) {
rename($realCacheDir, $oldCacheDir);
} else {
$warmupDir = $realCacheDir.'_new';
$this->warmup(!$input->getOption('without-debug'), $warmupDir);
rename($realCacheDir, $oldCacheDir);
rename($warmupDir, $realCacheDir);
}
$this->container->get('filesystem')->remove($oldCacheDir);
}
protected function warmup($debug, $warmupDir)
{
$this->container->get('filesystem')->remove($warmupDir);
$kernel = $this->getTempKernel($this->container->get('kernel'), $debug, $warmupDir);
$kernel->boot();
$warmer = $kernel->getContainer()->get('cache_warmer');
$warmer->enableOptionalWarmers();
$warmer->warmUp($warmupDir);
// fix container files and classes
$finder = new Finder();
foreach ($finder->files()->name(get_class($kernel->getContainer()).'*')->in($warmupDir) as $file) {
$content = file_get_contents($file);
$content = preg_replace('/__.*__/', '', $content);
file_put_contents(preg_replace('/__.*__/', '', $file), $content);
unlink($file);
}
}
protected function getTempKernel(KernelInterface $parent, $debug, $warmupDir)
{
$parentClass = get_class($parent);
$rand = uniqid();
$class = $parentClass.$rand;
$rootDir = $parent->getRootDir();
$code = <<<EOF
<?php
class $class extends $parentClass
{
public function getCacheDir()
{
return '$warmupDir';
}
public function getRootDir()
{
return '$rootDir';
}
protected function getContainerClass()
{
return parent::getContainerClass().'__{$rand}__';
}
}
EOF;
$this->container->get('filesystem')->mkdirs($warmupDir);
file_put_contents($file = $warmupDir.'/kernel.tmp', $code);
require_once $file;
@unlink($file);
return new $class($parent->getEnvironment(), $debug);
}
}