2011-03-11 22:35:48 +00:00
< ? 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 ;
2011-03-21 09:12:01 +00:00
use Symfony\Component\HttpKernel\KernelInterface ;
use Symfony\Component\Finder\Finder ;
2011-03-11 22:35:48 +00:00
/**
* Clear and Warmup the cache .
*
* @ author Francis Besset < francis . besset @ gmail . com >
2011-03-21 08:39:28 +00:00
* @ author Fabien Potencier < fabien @ symfony . com >
2011-03-11 22:35:48 +00:00
*/
2011-06-20 20:04:55 +01:00
class CacheClearCommand extends ContainerAwareCommand
2011-03-11 22:35:48 +00:00
{
2011-06-26 13:36:25 +01:00
protected $name ;
2011-03-11 22:35:48 +00:00
/**
* @ see Command
*/
protected function configure ()
{
$this
-> setName ( 'cache:clear' )
-> setDefinition ( array (
2011-03-21 10:29:20 +00:00
new InputOption ( 'no-warmup' , '' , InputOption :: VALUE_NONE , 'Do not warm up the cache' ),
2011-03-11 22:35:48 +00:00
))
-> setDescription ( 'Clear the cache' )
-> setHelp ( <<< EOF
2011-03-21 10:29:20 +00:00
The < info > cache : clear </ info > command clears the application cache for a given environment
and debug mode :
2011-03-11 22:35:48 +00:00
2011-07-28 09:01:00 +01:00
< info > php app / console cache : clear -- env = dev </ info >
< info > php app / console cache : clear -- env = prod -- no - debug </ info >
2011-03-11 22:35:48 +00:00
EOF
)
;
}
/**
* { @ inheritdoc }
*/
protected function execute ( InputInterface $input , OutputInterface $output )
{
2011-06-20 20:04:55 +01:00
$realCacheDir = $this -> getContainer () -> getParameter ( 'kernel.cache_dir' );
2011-03-11 22:35:48 +00:00
$oldCacheDir = $realCacheDir . '_old' ;
if ( ! is_writable ( $realCacheDir )) {
2011-04-14 12:11:03 +01:00
throw new \RuntimeException ( sprintf ( 'Unable to write in the "%s" directory' , $realCacheDir ));
2011-03-11 22:35:48 +00:00
}
2011-08-07 20:00:38 +01:00
$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 )));
2011-03-21 08:39:28 +00:00
if ( $input -> getOption ( 'no-warmup' )) {
2011-03-11 22:35:48 +00:00
rename ( $realCacheDir , $oldCacheDir );
} else {
2011-03-21 09:12:01 +00:00
$warmupDir = $realCacheDir . '_new' ;
2011-03-21 08:52:39 +00:00
2011-06-08 09:16:10 +01:00
$this -> warmup ( $warmupDir );
2011-03-11 22:35:48 +00:00
rename ( $realCacheDir , $oldCacheDir );
2011-03-21 09:12:01 +00:00
rename ( $warmupDir , $realCacheDir );
2011-03-11 22:35:48 +00:00
}
2011-03-21 08:31:04 +00:00
2011-06-20 20:04:55 +01:00
$this -> getContainer () -> get ( 'filesystem' ) -> remove ( $oldCacheDir );
2011-03-11 22:35:48 +00:00
}
2011-03-21 09:12:01 +00:00
2011-06-08 09:16:10 +01:00
protected function warmup ( $warmupDir )
2011-03-21 09:12:01 +00:00
{
2011-06-20 20:04:55 +01:00
$this -> getContainer () -> get ( 'filesystem' ) -> remove ( $warmupDir );
2011-03-21 09:12:01 +00:00
2011-06-26 17:34:40 +01:00
$parent = $this -> getContainer () -> get ( 'kernel' );
$class = get_class ( $parent );
$namespace = '' ;
if ( false !== $pos = strrpos ( $class , '\\' )) {
$namespace = substr ( $class , 0 , $pos );
$class = substr ( $class , $pos + 1 );
}
$kernel = $this -> getTempKernel ( $parent , $namespace , $class , $warmupDir );
2011-03-21 09:12:01 +00:00
$kernel -> boot ();
$warmer = $kernel -> getContainer () -> get ( 'cache_warmer' );
$warmer -> enableOptionalWarmers ();
$warmer -> warmUp ( $warmupDir );
2011-03-21 10:29:20 +00:00
// fix container files and classes
2011-06-26 13:36:25 +01:00
$regex = '/' . preg_quote ( $this -> getTempKernelSuffix (), '/' ) . '/' ;
2011-03-21 09:12:01 +00:00
$finder = new Finder ();
foreach ( $finder -> files () -> name ( get_class ( $kernel -> getContainer ()) . '*' ) -> in ( $warmupDir ) as $file ) {
$content = file_get_contents ( $file );
2011-06-26 13:36:25 +01:00
$content = preg_replace ( $regex , '' , $content );
2011-07-18 16:38:41 +01:00
// fix absolute paths to the cache directory
2011-12-13 07:50:54 +00:00
$content = preg_replace ( '/' . preg_quote ( $warmupDir , '/' ) . '/' , preg_replace ( '/_new$/' , '' , $warmupDir ), $content );
2011-07-18 16:38:41 +01:00
2011-06-26 13:36:25 +01:00
file_put_contents ( preg_replace ( $regex , '' , $file ), $content );
2011-03-21 09:12:01 +00:00
unlink ( $file );
}
2011-06-26 13:36:25 +01:00
// fix meta references to the Kernel
foreach ( $finder -> files () -> name ( '*.meta' ) -> in ( $warmupDir ) as $file ) {
$content = preg_replace (
2011-06-26 17:34:40 +01:00
'/C\:\d+\:"' . preg_quote ( $class . $this -> getTempKernelSuffix (), '"/' ) . '"/' ,
sprintf ( 'C:%s:"%s"' , strlen ( $class ), $class ),
2011-06-26 13:36:25 +01:00
file_get_contents ( $file )
);
file_put_contents ( $file , $content );
}
}
protected function getTempKernelSuffix ()
{
if ( null === $this -> name ) {
$this -> name = '__' . uniqid () . '__' ;
}
return $this -> name ;
2011-03-21 09:12:01 +00:00
}
2011-06-26 17:34:40 +01:00
protected function getTempKernel ( KernelInterface $parent , $namespace , $class , $warmupDir )
2011-03-21 09:12:01 +00:00
{
2011-06-26 13:36:25 +01:00
$suffix = $this -> getTempKernelSuffix ();
2011-03-21 09:12:01 +00:00
$rootDir = $parent -> getRootDir ();
$code = <<< EOF
< ? php
2011-03-22 18:46:08 +00:00
namespace $namespace
2011-03-21 09:12:01 +00:00
{
2011-06-26 17:34:40 +01:00
class $ class $suffix extends $class
2011-03-21 09:12:01 +00:00
{
2011-03-22 18:46:08 +00:00
public function getCacheDir ()
{
return '$warmupDir' ;
}
2011-03-21 09:12:01 +00:00
2011-03-22 18:46:08 +00:00
public function getRootDir ()
{
return '$rootDir' ;
}
2011-03-21 09:12:01 +00:00
2011-03-22 18:46:08 +00:00
protected function getContainerClass ()
{
2011-06-26 17:34:40 +01:00
return parent :: getContainerClass () . '$suffix' ;
2011-03-22 18:46:08 +00:00
}
2011-03-21 09:12:01 +00:00
}
}
EOF ;
2011-06-20 20:04:55 +01:00
$this -> getContainer () -> get ( 'filesystem' ) -> mkdir ( $warmupDir );
2011-03-21 09:12:01 +00:00
file_put_contents ( $file = $warmupDir . '/kernel.tmp' , $code );
require_once $file ;
@ unlink ( $file );
2011-06-26 17:34:40 +01:00
$class = " $namespace\\ $class $suffix " ;
2011-03-22 18:46:08 +00:00
2011-06-08 14:20:37 +01:00
return new $class ( $parent -> getEnvironment (), $parent -> isDebug ());
2011-03-21 09:12:01 +00:00
}
2011-03-11 22:35:48 +00:00
}