8000 [FrameworkBundle] refactored the cache:clear command · dpb587/symfony@e4a636a · GitHub
[go: up one dir, main page]

Skip to content

Commit e4a636a

Browse files
committed
[FrameworkBundle] refactored the cache:clear command
* removed the hack on the Kernel * removed inheritance from the warmup command * major cleanup
1 parent 9cf9f67 commit e4a636a

File tree

4 files changed

+68
-74
lines changed

4 files changed

+68
-74
lines changed

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

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,16 @@
1414
use Symfony\Component\Console\Input\InputInterface;
1515
use Symfony\Component\Console\Input\InputOption;
1616
use Symfony\Component\Console\Output\OutputInterface;
17+
use Symfony\Component\HttpKernel\KernelInterface;
18+
use Symfony\Component\Finder\Finder;
1719

1820
/**
1921
* Clear and Warmup the cache.
2022
*
2123
* @author Francis Besset <francis.besset@gmail.com>
2224
* @author Fabien Potencier <fabien@symfony.com>
2325
*/
24-
class CacheClearCommand extends CacheWarmupCommand
26+
class CacheClearCommand extends Command
2527
{
2628
/**
2729
* @see Command
@@ -58,14 +60,70 @@ protected function execute(InputInterface $input, OutputInterface $output)
5860
if ($input->getOption('no-warmup')) {
5961
rename($realCacheDir, $oldCacheDir);
6062
} else {
61-
$this->setWarmupDir($this->container->getParameter('kernel.environment').'_tmp');
63+
$warmupDir = $realCacheDir.'_new';
6264

63-
parent::execute($input, $output);
65+
$this->warmup($warmupDir);
6466

6567
rename($realCacheDir, $oldCacheDir);
66-
rename($this->kernelTmp->getCacheDir(), $realCacheDir);
68+
rename($warmupDir, $realCacheDir);
6769
}
6870

6971
$this->container->get('filesystem')->remove($oldCacheDir);
7072
}
73+
74+
protected function warmup($warmupDir)
75+
{
76+
$this->container->get('filesystem')->remove($warmupDir);
77+
78+
$kernel = $this->getTempKernel($this->container->get('kernel'), $warmupDir);
79+
$kernel->boot();
80+
81+
$warmer = $kernel->getContainer()->get('cache_warmer');
82+
$warmer->enableOptionalWarmers();
83+
$warmer->warmUp($warmupDir);
84+
85+
// rename container files
86+
$finder = new Finder();
87+
foreach ($finder->files()->name(get_class($kernel->getContainer()).'*')->in($warmupDir) as $file) {
88+
$content = file_get_contents($file);
89+
$content = preg_replace('/__.*__/', '', $content);
90+
file_put_contents(preg_replace('/__.*__/', '', $file), $content);
91+
unlink($file);
92+
}
93+
}
94+
95+
protected function getTempKernel(KernelInterface $parent, $warmupDir)
96+
{
97+
$parentClass = get_class($parent);
98+
$rand = uniqid();
99+
$class = $parentClass.$rand;
100+
$rootDir = $parent->getRootDir();
101+
$code = <<<EOF
102+
<?php
103+
104+
class $class extends $parentClass
105+
{
106+
public function getCacheDir()
107+
{
108+
return '$warmupDir';
109+
}
110+
111+
public function getRootDir()
112+
{
113+
return '$rootDir';
114+
}
115+
116+
protected function getContainerClass()
117+
{
118+
return parent::getContainerClass().'__{$rand}__';
119+
}
120+
}
121+
EOF;
122+
$this->container->get('filesystem')->mkdirs($warmupDir);
123+
file_put_contents($file = $warmupDir.'/kernel.tmp', $code);
124+
require_once $file;
125+
@unlink($file);
126+
127+
return new $class($parent->getEnvironment(), $parent->isDebug());
128+
}
71129
}

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

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

1414
use Symfony\Component\Console\Input\InputInterface;
15-
use Symfony\Component\Console\Input\InputOption;
1615
use Symfony\Component\Console\Output\OutputInterface;
17-
use Symfony\Component\DependencyInjection\ContainerInterface;
18-
use Symfony\Component\Finder\Finder;
1916

2017
/**
2118
* Warmup the cache.
2219
*
2320
* @author Fabien Potencier <fabien@symfony.com>
24-
* @author Francis Besset <francis.besset@gmail.com>
2521
*/
2622
class CacheWarmupCommand extends Command
2723
{
28-
protected $warmupDir;
29-
protected $kernelTmp;
30-
3124
/**
3225
* @see Command
3326
*/
@@ -36,9 +29,6 @@ protected function configure()
3629
$this
3730
->setName('cache:warmup')
3831
->setDescription('Warms up an empty cache')
39-
->setDefinition(array(
40-
new InputOption('warmup-dir', '', InputOption::VALUE_REQUIRED, 'Warms up the cache in the specified directory')
41-
))
4232
->setHelp(<<<EOF
4333
The <info>cache:warmup</info> command warms up the cache.
4434
@@ -55,38 +45,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
5545
{
5646
$output->writeln('Warming up the cache');
5747

58-
if ($input->getOption('warmup-dir')) {
59-
$this->setWarmupDir($input->getOption('warmup-dir'));
60-
}
61-
62-
if (!$this->warmupDir) {
63-
$this->warmUp($this->container);
64-
} else {
65-
$class = get_class($this->container->get('kernel'));
66-
$this->kernelTmp = new $class(
67-
$this->container->getParameter('kernel.environment'),
68-
$this->container->getParameter('kernel.debug'),
69-
$this->warmupDir
70-
);
71-
72-
$this->container->get('filesystem')->remove($this->kernelTmp->getCacheDir());
73-
74-
$this->kernelTmp->boot();
75-
unlink($this->kernelTmp->getCacheDir().DIRECTORY_SEPARATOR.$this->kernelTmp->getContainerClass().'.php');
76-
77-
$this->warmUp($this->kernelTmp->getContainer());
78-
}
79-
}
80-
81-
protected function warmUp(ContainerInterface $container)
82-
{
83-
$warmer = $container->get('cache_warmer');
48+
$warmer = $this->container->get('cache_warmer');
8449
$warmer->enableOptionalWarmers();
85-
$warmer->warmUp($container->getParameter('kernel.cache_dir'));
86-
}
87-
88-
protected function setWarmupDir($dir)
89-
{
90-
$this->warmupDir = $dir;
50+
$warmer->warmUp($this->container->getParameter('kernel.cache_dir'));
9151
}
9252
}

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

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,6 @@ 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-
3122
$tmpFile = tempnam(dirname($file), basename($file));
3223
if (false !== @file_put_contents($tmpFile, $content) && @rename($tmpFile, $file)) {
3324
chmod($file, 0644);

src/Symfony/Component/HttpKernel/Kernel.php

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ abstract class Kernel implements KernelInterface
4444
protected $rootDir;
4545
protected $environment;
4646
protected $debug;
47-
protected $cacheDir;
4847
protected $booted;
4948
protected $name;
5049
protected $startTime;
@@ -57,11 +56,10 @@ abstract class Kernel implements KernelInterface
5756
* @param string $environment The environment
5857
* @param Boolean $debug Whether to enable debugging or not
5958
*/
60-
public function __construct($environment, $debug, $cacheDir = null)
59+
public function __construct($environment, $debug)
6160
{
6261
$this->environment = $environment;
6362
$this->debug = (Boolean) $debug;
64-
$this->cacheDir = $cacheDir;
6563
$this->booted = false;
6664
$this->rootDir = $this->getRootDir();
6765
$this->name = preg_replace('/[^a-zA-Z0-9_]+/', '', basename($this->rootDir));
@@ -337,7 +335,7 @@ public function getStartTime()
337335
*/
338336
public function getCacheDir()
339337
{
340-
return $this->rootDir.'/cache/'.($this->cacheDir ?: $this->environment);
338+
< 741A span class="pl-k">return $this->rootDir.'/cache/'.$this->environment;
341339
}
342340

343341
/**
@@ -414,9 +412,9 @@ protected function initializeBundles()
414412
*
415413
* @return string The container class
416414
*/
417-
public function getContainerClass()
415+
protected function getContainerClass()
418416
{
419-
return $this->name.ucfirst($this->environment).($this->debug ? 'Debug' : '').'ProjectContainer'.($this->cacheDir ? 'Tmp' : '');
417+
return $this->name.ucfirst($this->environment).($this->debug ? 'Debug' : '').'ProjectContainer';
420418
}
421419

422420
/**
@@ -445,19 +443,6 @@ protected function initializeContainer()
445443
if (!$fresh && 'cli' !== php_sapi_name()) {
446444
$this->container->get('cache_warmer')->warmUp($this->container->getParameter('kernel.cache_dir'));
447445
}
448-
449-
if ($cacheDir = $this->cacheDir) {
450-
$realCacheDir = $this->getCacheDir();
451-
$this->cacheDir = null;
452-
453-
$class = $this->getContainerClass();
454-
$cache = new ConfigCache($realCacheDir, $class, $this->debug);
455-
456-
$container = $this->buildContainer();
457-
$this->dumpContainer($cache, $container, $class);
458-
459-
$this->cacheDir = $cacheDir;
460-
}
461446
}
462447

463448
/**

0 commit comments

Comments
 (0)
0