Skip to content

Instantly share code, notes, and snippets.

@althaus
Created April 5, 2013 15:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save althaus/5320231 to your computer and use it in GitHub Desktop.
Save althaus/5320231 to your computer and use it in GitHub Desktop.
Extend the base cache clear command adding a little sleep to prevent issus on Windows with "Access denied" on the cache directories.
<?php
namespace Althaus\ExtrasBundle\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Bundle\FrameworkBundle\Command\CacheClearCommand as BaseCommand;
class CacheClearCommand extends BaseCommand
{
/**
* @see Command
*/
protected function configure()
{
parent::configure();
$this->setDescription('Clears the cache (incl. Windows workaround)');
//$this->setName('cache:clr');
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$realCacheDir = $this->getContainer()->getParameter('kernel.cache_dir');
$oldCacheDir = $realCacheDir.'_old';
if (!is_writable($realCacheDir)) {
throw new \RuntimeException(sprintf('Unable to write in the "%s" directory', $realCacheDir));
}
$kernel = $this->getContainer()->get('kernel');
$output->writeln(sprintf('Clearing the cache for the <info>%s</info> environment with debug <info>%s</info>', $kernel->getEnvironment(), var_export($kernel->isDebug(), true)));
if ($input->getOption('no-warmup')) {
rename($realCacheDir, $oldCacheDir);
} else {
$warmupDir = $realCacheDir.'_new';
$this->warmup($warmupDir);
rename($realCacheDir, $oldCacheDir);
sleep(1);
rename($warmupDir, $realCacheDir);
}
$this->getContainer()->get('filesystem')->remove($oldCacheDir);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment