2016-09-09 09:04:40 +01: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 Psr\Cache\CacheItemPoolInterface ;
use Symfony\Component\Console\Input\InputInterface ;
use Symfony\Component\Console\Input\InputArgument ;
use Symfony\Component\Console\Output\OutputInterface ;
use Symfony\Component\Console\Style\SymfonyStyle ;
use Symfony\Component\HttpKernel\CacheClearer\Psr6CacheClearer ;
/**
* Clear cache pools .
*
* @ author Nicolas Grekas < p @ tchwork . com >
*/
final class CachePoolClearCommand extends ContainerAwareCommand
{
2017-08-21 09:40:46 +01:00
protected static $defaultName = 'cache:pool:clear' ;
2017-07-22 10:58:19 +01:00
private $poolClearer ;
/**
* @ param Psr6CacheClearer $poolClearer
*/
public function __construct ( $poolClearer = null )
{
if ( ! $poolClearer instanceof Psr6CacheClearer ) {
@ trigger_error ( sprintf ( 'Passing a command name as the first argument of "%s" is deprecated since version 3.4 and will be removed in 4.0. If the command was registered by convention, make it a service instead.' , __METHOD__ ), E_USER_DEPRECATED );
2017-08-06 11:59:30 +01:00
parent :: __construct ( $poolClearer );
2017-07-22 10:58:19 +01:00
return ;
}
2017-08-06 11:59:30 +01:00
parent :: __construct ();
2017-07-22 10:58:19 +01:00
$this -> poolClearer = $poolClearer ;
}
2016-09-09 09:04:40 +01:00
/**
* { @ inheritdoc }
*/
protected function configure ()
{
$this
-> setDefinition ( array (
2017-03-25 14:42:26 +00:00
new InputArgument ( 'pools' , InputArgument :: IS_ARRAY | InputArgument :: REQUIRED , 'A list of cache pools or cache pool clearers' ),
2016-09-09 09:04:40 +01:00
))
-> setDescription ( 'Clears cache pools' )
-> setHelp ( <<< 'EOF'
The < info >% command . name %</ info > command clears the given cache pools or cache pool clearers .
% command . full_name % < cache pool or clearer 1 > [ ...< cache pool or clearer N > ]
EOF
)
;
}
/**
* { @ inheritdoc }
*/
protected function execute ( InputInterface $input , OutputInterface $output )
{
2017-07-22 10:58:19 +01:00
// BC to be removed in 4.0
if ( null === $this -> poolClearer ) {
$this -> poolClearer = $this -> getContainer () -> get ( 'cache.global_clearer' );
$cacheDir = $this -> getContainer () -> getParameter ( 'kernel.cache_dir' );
}
2016-09-09 09:04:40 +01:00
$io = new SymfonyStyle ( $input , $output );
2017-07-22 10:58:19 +01:00
$kernel = $this -> getApplication () -> getKernel ();
2016-09-09 09:04:40 +01:00
$pools = array ();
$clearers = array ();
foreach ( $input -> getArgument ( 'pools' ) as $id ) {
2017-07-22 10:58:19 +01:00
if ( $this -> poolClearer -> hasPool ( $id )) {
2016-12-07 19:11:50 +00:00
$pools [ $id ] = $id ;
2016-09-09 09:04:40 +01:00
} else {
2017-07-22 10:58:19 +01:00
$pool = $kernel -> getContainer () -> get ( $id );
2016-12-07 19:11:50 +00:00
if ( $pool instanceof CacheItemPoolInterface ) {
$pools [ $id ] = $pool ;
} elseif ( $pool instanceof Psr6CacheClearer ) {
$clearers [ $id ] = $pool ;
} else {
throw new \InvalidArgumentException ( sprintf ( '"%s" is not a cache pool nor a cache clearer.' , $id ));
}
2016-09-09 09:04:40 +01:00
}
}
foreach ( $clearers as $id => $clearer ) {
$io -> comment ( sprintf ( 'Calling cache clearer: <info>%s</info>' , $id ));
2017-07-22 10:58:19 +01:00
$clearer -> clear ( isset ( $cacheDir ) ? $cacheDir : $kernel -> getContainer () -> getParameter ( 'kernel.cache_dir' ));
2016-09-09 09:04:40 +01:00
}
foreach ( $pools as $id => $pool ) {
$io -> comment ( sprintf ( 'Clearing cache pool: <info>%s</info>' , $id ));
2016-12-07 19:11:50 +00:00
if ( $pool instanceof CacheItemPoolInterface ) {
$pool -> clear ();
} else {
2017-07-22 10:58:19 +01:00
$this -> poolClearer -> clearPool ( $id );
2016-12-07 19:11:50 +00:00
}
2016-09-09 09:04:40 +01:00
}
$io -> success ( 'Cache was successfully cleared.' );
}
}