-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[FrameworkBundle] Added cache:clear command #100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?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; | ||
|
||
/** | ||
* Clear and Warmup the cache. | ||
* | ||
* @author Francis Besset <francis.besset@gmail.com> | ||
*/ | ||
class CacheClearCommand extends CacheWarmupCommand | ||
{ | ||
/** | ||
* @see Command | ||
*/ | ||
protected function configure() | ||
{ | ||
$this | ||
->setName('cache:clear') | ||
->setDefinition(array( | ||
new InputOption('warmup', '', InputOption::VALUE_NONE, 'Warms up the cache') | ||
)) | ||
->setDescription('Clear the cache') | ||
->setHelp(<<<EOF | ||
The <info>cache:clear</info> command clear the cache. | ||
|
||
<info>./app/console cache:clear --warmup</info> | ||
|
||
Warmup option, warms up the cache. | ||
EOF | ||
) | ||
; | ||
} | ||
|
||
protected function initialize(InputInterface $input, OutputInterface $output) | ||
{ | ||
parent::initialize($input, $output); | ||
|
||
$this->cacheDir = $this->container->getParameter('kernel.environment').'_tmp'; | ||
} | ||
|
||
/** | ||
* {@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 %s directory', $this->realCacheDir)); | ||
} | ||
|
||
$this->clearDir($oldCacheDir); | ||
|
||
if (!$input->getOption('warmup')) { | ||
$output->writeln('Clear cache'); | ||
|
||
rename($realCacheDir, $oldCacheDir); | ||
$this->clearDir($oldCacheDir); | ||
} else { | ||
parent::execute($input, $output); | ||
|
||
$output->writeln('Move cache directories'); | ||
rename($realCacheDir, $oldCacheDir); | ||
rename($this->kernelTmp->getCacheDir(), $realCacheDir); | ||
|
||
$output->writeln('Clear the old cache'); | ||
$this->clearDir($oldCacheDir); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,15 +12,22 @@ | |
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\DependencyInjection\ContainerInterface; | ||
use Symfony\Component\Finder\Finder; | ||
|
||
/** | ||
* Warmup the cache. | ||
* | ||
* @author Fabien Potencier <fabien@symfony.com> | ||
* @author Francis Besset <francis.besset@gmail.com> | ||
*/ | ||
class CacheWarmupCommand extends Command | ||
{ | ||
protected $cacheDir; | ||
protected $kernelTmp; | ||
|
||
/** | ||
* @see Command | ||
*/ | ||
|
@@ -29,24 +36,91 @@ protected function configure() | |
$this | ||
->setName('cache:warmup') | ||
->setDescription('Warms up an empty cache') | ||
->setDefinition(array( | ||
new InputOption('warmup-dir', '', InputOption::VALUE_OPTIONAL, 'Warms up the cache in a specific directory') | ||
)) | ||
->setHelp(<<<EOF | ||
The <info>cache:warmup</info> command warms up the cache. | ||
The <info>cache:warmup --warmup-dir=new_cache</info> command warms up the cache. | ||
|
||
Before running this command, the cache must be empty. | ||
Before running this command, the cache must be empty if not use warmup-dir option. | ||
EOF | ||
) | ||
; | ||
} | ||
|
||
protected function initialize(InputInterface $input, OutputInterface $output) | ||
{ | ||
parent::initialize($input, $output); | ||
|
||
if ($input->hasOption('warmup-dir')) { | ||
$this->cacheDir = $input->getOption('warmup-dir'); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$output->writeln('Warming up the cache'); | ||
|
||
$warmer = $this->container->get('cache_warmer'); | ||
if (!$this->cacheDir) { | ||
$this->warmUp($this->container); | ||
} else { | ||
$this->kernelTmp = new \AppKernel( | ||
$this->container->getParameter('kernel.environment'), | ||
$this->container->getParameter('kernel.debug'), | ||
$this->cacheDir | ||
); | ||
|
||
$this->clearDir($this->kernelTmp->getCacheDir()); | ||
|
||
$this->kernelTmp->boot(); | ||
unlink($this->kernelTmp->getCacheDir().DIRECTORY_SEPARATOR.$this->kernelTmp->getContainerClass().'.php'); | ||
|
||
$this->warmUp($this->kernelTmp->getContainer()); | ||
} | ||
} | ||
|
||
protected function warmUp(ContainerInterface $container) | ||
{ | ||
$warmer = $container->get('cache_warmer'); | ||
$warmer->enableOptionalWarmers(); | ||
$warmer->warmUp($this->container->getParameter('kernel.cache_dir')); | ||
$warmer->warmUp($container->getParameter('kernel.cache_dir')); | ||
} | ||
|
||
protected function clearDir($dir) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can use this instead:
|
||
{ | ||
if (is_dir($dir)) { | ||
$finder = new Finder(); | ||
$files = $finder | ||
->in($dir) | ||
->getIterator() | ||
; | ||
|
||
$array = iterator_to_array($files); | ||
|
||
foreach (array_reverse($array) as $file) { | ||
if ($file->isFile()) { | ||
if (!is_writable($file->getPathname())) { | ||
throw new \RuntimeException(sprintf('Unable to delete %s file', $file->getPathname())); | ||
} | ||
|
||
unlink($file->getPathname()); | ||
} else { | ||
if (!is_writable($file->getPathname())) { | ||
throw new \RuntimeException(sprintf('Unable to delete %s directory', $file->getPathname())); | ||
} | ||
|
||
rmdir($file->getPathname()); | ||
} | ||
} | ||
|
||
if (!is_writable($dir)) { | ||
throw new \RuntimeException(sprintf('Unable to delete %s directory', $dir)); | ||
} | ||
|
||
rmdir($dir); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This won't work if an application uses a different class name for their kernel.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm there is already some black magic in the WebTestCase for this, but obviously we want people to easily change the position and name for their kernel. does this require some generic solution? then again i would guess from inside a running kernel instance it should be no problem to properly locate the actual kernel class name