From 4baa8cf42eb419fedea54c2797ea232ddf5520dd Mon Sep 17 00:00:00 2001 From: Moshe Weitzman Date: Thu, 7 Sep 2023 12:55:05 -0400 Subject: [PATCH] Move cache:rebuild to own command file (#5750) https://drupal.slack.com/archives/C62H9CWQM/p1694091163310429?thread_ts=1693915858.778809&cid=C62H9CWQM --- src/Commands/core/CacheCommands.php | 34 +------------- src/Commands/core/CacheRebuildCommands.php | 53 ++++++++++++++++++++++ src/Commands/core/DeployCommands.php | 2 +- tests/functional/SqlDumpTest.php | 3 +- 4 files changed, 57 insertions(+), 35 deletions(-) create mode 100644 src/Commands/core/CacheRebuildCommands.php diff --git a/src/Commands/core/CacheCommands.php b/src/Commands/core/CacheCommands.php index 165cb3b68b..2ae18bfb39 100644 --- a/src/Commands/core/CacheCommands.php +++ b/src/Commands/core/CacheCommands.php @@ -14,8 +14,6 @@ use Drush\Boot\BootstrapManager; use Drush\Boot\DrupalBootLevels; use Drush\Commands\DrushCommands; -use Drupal\Core\DrupalKernel; -use Drupal\Core\Site\Settings; use Drupal\Core\Cache\Cache; use Drush\Utils\StringUtils; use Consolidation\AnnotatedCommand\Input\StdinAwareInterface; @@ -36,6 +34,7 @@ final class CacheCommands extends DrushCommands implements CustomEventAwareInter const TAGS = 'cache:tags'; const CLEAR = 'cache:clear'; const SET = 'cache:set'; + // @deprecated. Use CacheRebuildCommands::REBUILD const REBUILD = 'cache:rebuild'; const EVENT_CLEAR = 'cache-clear'; @@ -212,37 +211,6 @@ protected function setPrepareData($data, $options) return $data; } - /** - * Rebuild all caches. - * - * This is a copy of core/rebuild.php. - */ - #[CLI\Command(name: self::REBUILD, aliases: ['cr', 'rebuild', 'cache-rebuild'])] - #[CLI\Option(name: 'cache-clear', description: 'Set to 0 to suppress normal cache clearing; the caller should then clear if needed.')] - #[CLI\Bootstrap(level: DrupalBootLevels::SITE)] - public function rebuild($options = ['cache-clear' => true]) - { - if (!$options['cache-clear']) { - $this->logger()->info(dt("Skipping cache-clear operation due to --no-cache-clear option.")); - return true; - } - - // We no longer clear APC and similar caches as they are useless on CLI. - // See https://github.com/drush-ops/drush/pull/2450 - $root = $this->bootstrapManager->getRoot(); - require_once DRUSH_DRUPAL_CORE . '/includes/utility.inc'; - - $request = $this->bootstrapManager->bootstrap()->getRequest(); - DrupalKernel::bootEnvironment(); - - $site_path = DrupalKernel::findSitePath($request); - Settings::initialize($root, $site_path, $this->autoloader); - - // drupal_rebuild() calls drupal_flush_all_caches() itself, so we don't do it manually. - drupal_rebuild($this->autoloader, $request); - $this->logger()->success(dt('Cache rebuild complete.')); - } - #[CLI\Hook(type: HookManager::ARGUMENT_VALIDATOR, target: self::CLEAR)] public function validate(CommandData $commandData): void { diff --git a/src/Commands/core/CacheRebuildCommands.php b/src/Commands/core/CacheRebuildCommands.php new file mode 100644 index 0000000000..6df838505f --- /dev/null +++ b/src/Commands/core/CacheRebuildCommands.php @@ -0,0 +1,53 @@ + true]) + { + if (!$options['cache-clear']) { + $this->logger()->info(dt("Skipping cache-clear operation due to --no-cache-clear option.")); + return true; + } + + // We no longer clear APC and similar caches as they are useless on CLI. + // See https://github.com/drush-ops/drush/pull/2450 + $bootstrapManager = Drush::bootstrapManager(); + $root = $bootstrapManager->getRoot(); + require_once DRUSH_DRUPAL_CORE . '/includes/utility.inc'; + + $request = $bootstrapManager->bootstrap()->getRequest(); + DrupalKernel::bootEnvironment(); + + $site_path = DrupalKernel::findSitePath($request); + $autoloader = Drush::getContainer()->get('loader'); + Settings::initialize($root, $site_path, $autoloader); + + // drupal_rebuild() calls drupal_flush_all_caches() itself, so we don't do it manually. + drupal_rebuild($autoloader, $request); + $this->logger()->success(dt('Cache rebuild complete.')); + } +} diff --git a/src/Commands/core/DeployCommands.php b/src/Commands/core/DeployCommands.php index c09410f377..ec6923fa90 100644 --- a/src/Commands/core/DeployCommands.php +++ b/src/Commands/core/DeployCommands.php @@ -59,7 +59,7 @@ public function cacheRebuild(ProcessManager $manager, SiteAlias $self, array $re { // It is possible that no updates were pending and thus no caches cleared yet. $this->logger()->success("Cache rebuild start."); - $process = $manager->drush($self, CacheCommands::REBUILD, [], $redispatchOptions); + $process = $manager->drush($self, CacheRebuildCommands::REBUILD, [], $redispatchOptions); $process->mustRun($process->showRealtime()); } } diff --git a/tests/functional/SqlDumpTest.php b/tests/functional/SqlDumpTest.php index 74721c54de..f701344b41 100644 --- a/tests/functional/SqlDumpTest.php +++ b/tests/functional/SqlDumpTest.php @@ -5,6 +5,7 @@ namespace Unish; use Drush\Commands\core\CacheCommands; +use Drush\Commands\core\CacheRebuildCommands; use Drush\Commands\sql\SqlCommands; use Symfony\Component\Filesystem\Path; @@ -38,7 +39,7 @@ public function testSqlDump() ]; // In Drupal 9.1+, cache_discovery et. al. do not exist until after a cache rebuild. - $this->drush(CacheCommands::REBUILD, []); + $this->drush(CacheRebuildCommands::REBUILD, []); $this->drush(SqlCommands::DUMP, [], $options + ['simulate' => null]); $expected = $this->dbDriver() == 'mysql' ? '--ignore-table=unish_dev.cache_discovery' : '--exclude-table=cache_discovery';