Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bb7986e

Browse files
Francis Bessetstof
authored andcommitted
[FrameworkBundle] Added cache:clear command with warmup option
1 parent e0b46fe commit bb7986e

File tree

4 files changed

+197
-7
lines changed

4 files changed

+197
-7
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[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 Symfony\Bundle\FrameworkBundle\Command;
13+
14+
use Symfony\Component\Console\Input\InputInterface;
15+
use Symfony\Component\Console\Input\InputOption;
16+
use Symfony\Component\Console\Output\OutputInterface;
17+
18+
/**
19+
* Clear and Warmup the cache.
20+
*
21+
* @author Francis Besset <[email protected]>
22+
*/
23+
class CacheClearCommand extends CacheWarmupCommand
24+
{
25+
/**
26+
* @see Command
27+
*/
28+
protected function configure()
29+
{
30+
$this
31+
->setName('cache:clear')
32+
->setDefinition(array(
33+
new InputOption('warmup', '', InputOption::VALUE_NONE, 'Warms up the cache')
34+
))
35+
->setDescription('Clear the cache')
36+
->setHelp(<<<EOF
37+
The <info>cache:clear</info> command clear the cache.
38+
39+
<info>./app/console cache:clear --warmup</info>
40+
41+
Warmup option, warms up the cache.
42+
EOF
43+
)
44+
;
45+
}
46+
47+
protected function initialize(InputInterface $input, OutputInterface $output)
48+
{
49+
parent::initialize($input, $output);
50+
51+
$this->cacheDir = $this->container->getParameter('kernel.environment').'_tmp';
52+
}
53+
54+
/**
55+
* {@inheritdoc}
56+
*/
57+
protected function execute(InputInterface $input, OutputInterface $output)
58+
{
59+
$realCacheDir = $this->container->getParameter('kernel.cache_dir');
60+
$oldCacheDir = $realCacheDir.'_old';
61+
62+
if (!is_writable($realCacheDir)) {
63+
throw new \RuntimeException(sprintf('Unable to write %s directory', $this->realCacheDir));
64+
}
65+
66+
$this->clearDir($oldCacheDir);
67+
68+
if (!$input->getOption('warmup')) {
69+
$output->writeln('Clear cache');
70+
71+
rename($realCacheDir, $oldCacheDir);
72+
$this->clearDir($oldCacheDir);
73+
} else {
74+
parent::execute($input, $output);
75+
76+
$output->writeln('Move cache directories');
77+
rename($realCacheDir, $oldCacheDir);
78+
rename($this->kernelTmp->getCacheDir(), $realCacheDir);
79+
80+
$output->writeln('Clear the old cache');
81+
$this->clearDir($oldCacheDir);
82+
}
83+
}
84+
}

src/Symfony/Bundle/FrameworkBundle/Command/CacheWarmupCommand.php

Lines changed: 78 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,22 @@
1212
namespace Symfony\Bundle\FrameworkBundle\Command;
1313

1414
use Symfony\Component\Console\Input\InputInterface;
15+
use Symfony\Component\Console\Input\InputOption;
1516
use Symfony\Component\Console\Output\OutputInterface;
17+
use Symfony\Component\DependencyInjection\ContainerInterface;
18+
use Symfony\Component\Finder\Finder;
1619

1720
/**
1821
* Warmup the cache.
1922
*
2023
* @author Fabien Potencier <[email protected]>
24+
* @author Francis Besset <[email protected]>
2125
*/
2226
class CacheWarmupCommand extends Command
2327
{
28+
protected $cacheDir;
29+
protected $kernelTmp;
30+
2431
/**
2532
* @see Command
2633
*/
@@ -29,24 +36,91 @@ protected function configure()
2936
$this
3037
->setName('cache:warmup')
3138
->setDescription('Warms up an empty cache')
39+
->setDefinition(array(
40+
new InputOption('warmup-dir', '', InputOption::VALUE_OPTIONAL, 'Warms up the cache in a specific directory')
41+
))
3242
->setHelp(<<<EOF
33-
The <info>cache:warmup</info> command warms up the cache.
43+
The <info>cache:warmup --warmup-dir=new_cache</info> command warms up the cache.
3444
35-
Before running this command, the cache must be empty.
45+
Before running this command, the cache must be empty if not use warmup-dir option.
3646
EOF
3747
)
3848
;
3949
}
4050

51+
protected function initialize(InputInterface $input, OutputInterface $output)
52+
{
53+
parent::initialize($input, $output);
54+
55+
if ($input->hasOption('warmup-dir')) {
56+
$this->cacheDir = $input->getOption('warmup-dir');
57+
}
58+
}
59+
4160
/**
4261
* {@inheritdoc}
4362
*/
4463
protected function execute(InputInterface $input, OutputInterface $output)
4564
{
4665
$output->writeln('Warming up the cache');
4766

48-
$warmer = $this->container->get('cache_warmer');
67+
if (!$this->cacheDir) {
68+
$this->warmUp($this->container);
69+
} else {
70+
$this->kernelTmp = new \AppKernel(
71+
$this->container->getParameter('kernel.environment'),
72+
$this->container->getParameter('kernel.debug'),
73+
$this->cacheDir
74+
);
75+
76+
$this->clearDir($this->kernelTmp->getCacheDir());
77+
78+
$this->kernelTmp->boot();
79+
unlink($this->kernelTmp->getCacheDir().DIRECTORY_SEPARATOR.$this->kernelTmp->getContainerClass().'.php');
80+
81+
$this->warmUp($this->kernelTmp->getContainer());
82+
}
83+
}
84+
85+
protected function warmUp(ContainerInterface $container)
86+
{
87+
$warmer = $container->get('cache_warmer');
4988
$warmer->enableOptionalWarmers();
50-
$warmer->warmUp($this->container->getParameter('kernel.cache_dir'));
89+
$warmer->warmUp($container->getParameter('kernel.cache_dir'));
90+
}
91+
92+
protected function clearDir($dir)
93+
{
94+
if (is_dir($dir)) {
95+
$finder = new Finder();
96+
$files = $finder
97+
->in($dir)
98+
->getIterator()
99+
;
100+
101+
$array = iterator_to_array($files);
102+
103+
foreach (array_reverse($array) as $file) {
104+
if ($file->isFile()) {
105+
if (!is_writable($file->getPathname())) {
106+
throw new \RuntimeException(sprintf('Unable to delete %s file', $file->getPathname()));
107+
}
108+
109+
unlink($file->getPathname());
110+
} else {
111+
if (!is_writable($file->getPathname())) {
112+
throw new \RuntimeException(sprintf('Unable to delete %s directory', $file->getPathname()));
113+
}
114+
115+
rmdir($file->getPathname());
116+
}
117+
}
118+
119+
if (!is_writable($dir)) {
120+
throw new \RuntimeException(sprintf('Unable to delete %s directory', $dir));
121+
}
122+
123+
rmdir($dir);
124+
}
51125
}
52126
}

src/Symfony/Component/HttpKernel/CacheWarmer/CacheWarmer.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ abstract class CacheWarmer implements CacheWarmerInterface
1919
{
2020
protected function writeCacheFile($file, $content)
2121
{
22+
$dir = dirname($file);
23+
if (!is_dir($dir)) {
24+
if (false === @mkdir($dir, 0777, true)) {
25+
throw new \RuntimeException(sprintf('Unable to create the %s directory', $dir));
26+
}
27+
} elseif (!is_writable($dir)) {
28+
throw new \RuntimeException(sprintf('Unable to write in the %s directory', $dir));
29+
}
30+
2231
$tmpFile = tempnam(dirname($file), basename($file));
2332
if (false !== @file_put_contents($tmpFile, $content) && @rename($tmpFile, $file)) {
2433
chmod($file, 0644);

src/Symfony/Component/HttpKernel/Kernel.php

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ abstract class Kernel implements KernelInterface
4444
protected $rootDir;
4545
protected $environment;
4646
protected $debug;
47+
protected $cacheDir;
4748
protected $booted;
4849
protected $name;
4950
protected $startTime;
@@ -56,10 +57,11 @@ abstract class Kernel implements KernelInterface
5657
* @param string $environment The environment
5758
* @param Boolean $debug Whether to enable debugging or not
5859
*/
59-
public function __construct($environment, $debug)
60+
public function __construct($environment, $debug, $cacheDir = null)
6061
{
6162
$this->environment = $environment;
6263
$this->debug = (Boolean) $debug;
64+
$this->cacheDir = $cacheDir;
6365
$this->booted = false;
6466
$this->rootDir = realpath($this->registerRootDir());
6567
$this->name = preg_replace('/[^a-zA-Z0-9_]+/', '', basename($this->rootDir));
@@ -308,6 +310,14 @@ public function getContainer()
308310
return $this->container;
309311
}
310312

313+
/**
314+
* @return string The container classname
315+
*/
316+
public function getContainerClass()
317+
{
318+
return $this->name.ucfirst($this->environment).($this->debug ? 'Debug' : '').'ProjectContainer'.($this->cacheDir ? 'Tmp' : '');
319+
}
320+
311321
/**
312322
* Gets the request start time (not available if debug is disabled).
313323
*
@@ -325,7 +335,7 @@ public function getStartTime()
325335
*/
326336
public function getCacheDir()
327337
{
328-
return $this->rootDir.'/cache/'.$this->environment;
338+
return $this->rootDir.'/cache/'.($this->cacheDir ?: $this->environment);
329339
}
330340

331341
/**
@@ -399,7 +409,7 @@ protected function initializeBundles()
399409

400410
protected function initializeContainer()
401411
{
402-
$class = $this->name.ucfirst($this->environment).($this->debug ? 'Debug' : '').'ProjectContainer';
412+
$class = $this->getContainerClass();
403413
$cache = new ConfigCache($this->getCacheDir(), $class, $this->debug);
404414
$fresh = false;
405415
if (!$cache->isFresh()) {
@@ -417,6 +427,19 @@ protected function initializeContainer()
417427
if ($fresh && 'cli' !== php_sapi_name()) {
418428
$this->container->get('cache_warmer')->warmUp($this->container->getParameter('kernel.cache_dir'));
419429
}
430+
431+
if ($cacheDir = $this->cacheDir) {
432+
$realCacheDir = $this->getCacheDir();
433+
$this->cacheDir = null;
434+
435+
$class = $this->getContainerClass();
436+
$cache = new ConfigCache($realCacheDir, $class, $this->debug);
437+
438+
$container = $this->buildContainer();
439+
$this->dumpContainer($cache, $container, $class);
440+
441+
$this->cacheDir = $cacheDir;
442+
}
420443
}
421444

422445
protected function getKernelParameters()

0 commit comments

Comments
 (0)