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' ),
2012-02-09 00:44:27 +00:00
new InputOption ( 'no-optional-warmers' , '' , InputOption :: VALUE_NONE , 'Skip optional cache warmers (faster)' ),
2011-03-11 22:35:48 +00:00
))
2012-01-09 09:01:34 +00:00
-> setDescription ( 'Clears the cache' )
2011-03-11 22:35:48 +00:00
-> setHelp ( <<< EOF
2012-02-12 15:37:55 +00:00
The < info >% command . name %</ info > command clears the application cache for a given environment
2011-03-21 10:29:20 +00:00
and debug mode :
2011-03-11 22:35:48 +00:00
2012-02-12 15:37:55 +00:00
< info > php % command . full_name % -- env = dev </ info >
< info > php % command . full_name % -- 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
}
2013-03-04 16:52:53 +00:00
$filesystem = $this -> getContainer () -> get ( 'filesystem' );
2011-08-07 20:00:38 +01:00
$kernel = $this -> getContainer () -> get ( 'kernel' );
2012-07-05 06:56:11 +01:00
$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-08-07 20:00:38 +01:00
2011-12-12 22:39:17 +00:00
$this -> getContainer () -> get ( 'cache_clearer' ) -> clear ( $realCacheDir );
2013-03-04 16:52:53 +00:00
if ( $filesystem -> exists ( $oldCacheDir )) {
$filesystem -> remove ( $oldCacheDir );
}
2011-03-21 08:39:28 +00:00
if ( $input -> getOption ( 'no-warmup' )) {
2013-03-04 16:52:53 +00:00
$filesystem -> rename ( $realCacheDir , $oldCacheDir );
2011-03-11 22:35:48 +00:00
} else {
2011-03-21 09:12:01 +00:00
$warmupDir = $realCacheDir . '_new' ;
2011-03-21 08:52:39 +00:00
2013-03-04 16:52:53 +00:00
if ( $filesystem -> exists ( $warmupDir )) {
$filesystem -> remove ( $warmupDir );
}
2012-02-09 00:44:27 +00:00
$this -> warmup ( $warmupDir , ! $input -> getOption ( 'no-optional-warmers' ));
2011-03-11 22:35:48 +00:00
2013-03-04 16:52:53 +00:00
$filesystem -> rename ( $realCacheDir , $oldCacheDir );
$filesystem -> rename ( $warmupDir , $realCacheDir );
2011-03-11 22:35:48 +00:00
}
2011-03-21 08:31:04 +00:00
2013-03-04 16:52:53 +00:00
$filesystem -> remove ( $oldCacheDir );
2011-03-11 22:35:48 +00:00
}
2011-03-21 09:12:01 +00:00
2012-02-09 00:44:27 +00:00
protected function warmup ( $warmupDir , $enableOptionalWarmers = true )
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' );
2012-02-09 00:44:27 +00:00
if ( $enableOptionalWarmers ) {
$warmer -> enableOptionalWarmers ();
}
2011-03-21 09:12:01 +00:00
$warmer -> warmUp ( $warmupDir );
2013-03-01 16:33:44 +00:00
foreach ( Finder :: create () -> files () -> name ( '*.meta' ) -> in ( $warmupDir ) as $file ) {
// fix meta references to the Kernel
$content = preg_replace (
'/C\:\d+\:"' . preg_quote ( $class . $this -> getTempKernelSuffix (), '"/' ) . '"/' ,
sprintf ( 'C:%s:"%s"' , strlen ( $class ), $class ),
file_get_contents ( $file )
);
// fix meta references to cache files
$realWarmupDir = substr ( $warmupDir , 0 , - 4 );
$content = preg_replace_callback (
'/s\:\d+\:"' . preg_quote ( $warmupDir , '/' ) . '([^"]+)"/' ,
function ( array $matches ) use ( $realWarmupDir ) {
$path = $realWarmupDir . $matches [ 1 ];
return sprintf ( 's:%s:"%s"' , strlen ( $path ), $path );
},
$content
);
file_put_contents ( $file , $content );
}
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 (), '/' ) . '/' ;
2013-03-01 16:33:44 +00:00
foreach ( Finder :: create () -> files () -> name ( get_class ( $kernel -> getContainer ()) . '*' ) -> in ( $warmupDir ) as $file ) {
2011-03-21 09:12:01 +00:00
$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
}
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
}