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
{
/**
2013-03-07 13:36:36 +00:00
* { @ inheritdoc }
2011-03-11 22:35:48 +00:00
*/
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' ;
2013-03-14 10:46:34 +00:00
$filesystem = $this -> getContainer () -> get ( 'filesystem' );
2011-03-11 22:35:48 +00:00
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
if ( $filesystem -> exists ( $oldCacheDir )) {
$filesystem -> remove ( $oldCacheDir );
}
2013-03-14 10:46:34 +00: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 )));
$this -> getContainer () -> get ( 'cache_clearer' ) -> clear ( $realCacheDir );
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 {
2013-03-14 10:46:34 +00:00
// the warmup cache dir name must have the same length than the real one
2013-03-13 11:02:56 +00:00
// to avoid the many problems in serialized resources files
$warmupDir = substr ( $realCacheDir , 0 , - 1 ) . '_' ;
2011-03-21 08:52:39 +00:00
2013-03-04 16:52:53 +00:00
if ( $filesystem -> exists ( $warmupDir )) {
$filesystem -> remove ( $warmupDir );
}
2013-03-13 11:02:56 +00:00
$this -> warmup ( $warmupDir , $realCacheDir , ! $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 );
2013-08-15 14:21:20 +01:00
if ( defined ( 'PHP_WINDOWS_VERSION_BUILD' )) {
sleep ( 1 ); // workaround for windows php rename bug
}
2013-03-04 16:52:53 +00:00
$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
2013-03-14 10:46:34 +00:00
/**
* @ param string $warmupDir
* @ param string $realCacheDir
* @ param bool $enableOptionalWarmers
*/
2013-03-13 11:02:56 +00:00
protected function warmup ( $warmupDir , $realCacheDir , $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
2013-03-14 10:46:34 +00:00
// create a temporary kernel
$realKernel = $this -> getContainer () -> get ( 'kernel' );
$realKernelClass = get_class ( $realKernel );
2011-06-26 17:34:40 +01:00
$namespace = '' ;
2013-03-14 10:46:34 +00:00
if ( false !== $pos = strrpos ( $realKernelClass , '\\' )) {
$namespace = substr ( $realKernelClass , 0 , $pos );
$realKernelClass = substr ( $realKernelClass , $pos + 1 );
2011-06-26 17:34:40 +01:00
}
2013-03-14 10:46:34 +00:00
$tempKernel = $this -> getTempKernel ( $realKernel , $namespace , $realKernelClass , $warmupDir );
$tempKernel -> boot ();
2011-06-26 17:34:40 +01:00
2013-03-14 10:46:34 +00:00
// warmup temporary dir
$warmer = $tempKernel -> 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-13 11:02:56 +00:00
// fix references to the Kernel in .meta files
2013-03-01 16:33:44 +00:00
foreach ( Finder :: create () -> files () -> name ( '*.meta' ) -> in ( $warmupDir ) as $file ) {
2013-03-13 11:02:56 +00:00
file_put_contents ( $file , preg_replace (
2013-03-14 10:46:34 +00:00
'/(C\:\d+\:)"' . get_class ( $tempKernel ) . '"/' ,
sprintf ( '$1"%s"' , $realKernelClass ),
2013-03-01 16:33:44 +00:00
file_get_contents ( $file )
2013-03-13 11:02:56 +00:00
));
2013-03-01 16:33:44 +00:00
}
2013-03-13 11:02:56 +00:00
// fix references to cached files with the real cache directory name
foreach ( Finder :: create () -> files () -> in ( $warmupDir ) as $file ) {
$content = str_replace ( $warmupDir , $realCacheDir , file_get_contents ( $file ));
2013-03-01 16:33:44 +00:00
file_put_contents ( $file , $content );
}
2013-03-14 10:46:34 +00:00
// fix references to kernel/container related classes
$search = $tempKernel -> getName () . ucfirst ( $tempKernel -> getEnvironment ());
$replace = $realKernel -> getName () . ucfirst ( $realKernel -> getEnvironment ());
foreach ( Finder :: create () -> files () -> name ( $search . '*' ) -> in ( $warmupDir ) as $file ) {
$content = str_replace ( $search , $replace , file_get_contents ( $file ));
file_put_contents ( str_replace ( $search , $replace , $file ), $content );
2011-03-21 09:12:01 +00:00
unlink ( $file );
}
2011-06-26 13:36:25 +01:00
}
2013-03-14 10:46:34 +00:00
/**
* @ param KernelInterface $parent
* @ param string $namespace
* @ param string $parentClass
* @ param string $warmupDir
*
* @ return KernelInterface
*/
protected function getTempKernel ( KernelInterface $parent , $namespace , $parentClass , $warmupDir )
2011-03-21 09:12:01 +00:00
{
$rootDir = $parent -> getRootDir ();
2013-03-14 10:46:34 +00:00
// the temp kernel class name must have the same length than the real one
// to avoid the many problems in serialized resources files
$class = substr ( $parentClass , 0 , - 1 ) . '_' ;
// the temp kernel name must be changed too
$name = substr ( $parent -> getName (), 0 , - 1 ) . '_' ;
2011-03-21 09:12:01 +00:00
$code = <<< EOF
< ? php
2011-03-22 18:46:08 +00:00
namespace $namespace
2011-03-21 09:12:01 +00:00
{
2013-03-14 10:46:34 +00:00
class $ class extends $parentClass
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
2013-03-14 10:46:34 +00:00
public function getName ()
2011-03-22 18:46:08 +00:00
{
2013-03-14 10:46:34 +00:00
return '$name' ;
2011-03-22 18:46:08 +00:00
}
2011-03-21 09:12:01 +00:00
2013-03-14 10:46:34 +00:00
public function getRootDir ()
2011-03-22 18:46:08 +00:00
{
2013-03-14 10:46:34 +00:00
return '$rootDir' ;
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 );
2013-03-14 10:46:34 +00:00
$class = " $namespace\\ $class " ;
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
}