Skip to content

Instantly share code, notes, and snippets.

@jmolivas
Last active August 29, 2015 14:12
Show Gist options
  • Save jmolivas/f8073e4e5fbdb6c39b62 to your computer and use it in GitHub Desktop.
Save jmolivas/f8073e4e5fbdb6c39b62 to your computer and use it in GitHub Desktop.
<?php
/**
* @file
* Contains \Drupal\AppConsole\Command\CacheRebuildCommand.
*/
namespace Drupal\AppConsole\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Drupal\Core\Cache\Cache;
class CacheRebuildCommand extends ContainerAwareCommand
{
protected $caches = [];
protected function configure()
{
$this
->setName('cache:rebuild')
->setDescription($this->trans('command.cache.rebuild.description'))
->setAliases(['cr'])
->addOption('cache', null, InputOption::VALUE_NONE, $this->trans('command.cache.rebuild.options.cache'))
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
require_once DRUPAL_ROOT . '/core/includes/utility.inc';
$output->writeln('[+] <comment>'.$this->trans('command.cache.rebuild.messages.rebuild').'</comment>');
$kernelHelper = $this->getHelper('kernel');
$classLoader = $kernelHelper->getClassLoader();
$request = $kernelHelper->getRequest();
$cache = $input->getOption('cache');
if ($cache == 'all') {
\drupal_rebuild($classLoader, $request);
}
else {
$caches = $this->getCaches();
$caches[$cache]->deleteAll();
}
$output->writeln('[+] <info>'.$this->trans('command.cache.rebuild.messages.done').'</info>');
}
protected function interact(InputInterface $input, OutputInterface $output)
{
$dialog = $this->getDialogHelper();
$dialog->writeSection($output, $this->trans('command.cache.rebuild.messages.welcome'));
$caches = $this->getCaches();
$cache_keys = array_keys($caches);
$cache_keys[] = 'all';
// --cache option
$cache = $input->getOption('cache');
if (!$cache) {
$cache = $dialog->askAndValidate(
$output,
$dialog->getQuestion($this->trans('command.cache.rebuild.questions.cache'),'all'),
function ($cache) use($cache_keys) {
if (!in_array($cache, array_values($cache_keys))) {
throw new \InvalidArgumentException(
sprintf(
$this->trans('command.cache.rebuild.messages.invalid_cache'),
$cache
)
);
}
return $cache;
},
false,
'all',
$cache_keys
);
}
$input->setOption('cache', $cache);
}
protected function getCaches()
{
if (empty($this->caches)) {
foreach (Cache::getBins() as $name => $bin) {
$this->caches[$name] = $bin;
}
}
return $this->caches;
}
}
command:
cache:
rebuild:
description: Rebuild and clear all site caches.
options:
cache: Only clean a specific cache
messages:
welcome: Welcome to the Drupal clear cache command.
rebuild: Rebuilding cache(s), wait a moment please.
done: Done cleaning cache(s).
invalid_cache: Cache "%s" is invalid.
questions:
cache: Select cache.
container:
debug:
description: Displays current services for an application.
command:
cache:
rebuild:
description: Reconstruye y limpia todas las cachés del sitio.
options:
cache: Sólo limpiar un cache específico.
messages:
welcome: Bienvenido al comando cache:rebuild.
rebuild: Reconstruyendo caché(s), espere un momento.
done: Hecho limpiar Caché(s).
invalid_cache: Caché "%s" en inválido.
questions:
cache: Seleccione un caché.
container:
debug:
description: Muestra los servicios actuales para una aplicación.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment