Skip to content

Commit

Permalink
Move cache:rebuild to own command file (#5750)
Browse files Browse the repository at this point in the history
  • Loading branch information
weitzman authored Sep 7, 2023
1 parent 9dd5d67 commit 4baa8cf
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 35 deletions.
34 changes: 1 addition & 33 deletions src/Commands/core/CacheCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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';

Expand Down Expand Up @@ -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
{
Expand Down
53 changes: 53 additions & 0 deletions src/Commands/core/CacheRebuildCommands.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace Drush\Commands\core;

use Drush\Attributes as CLI;
use Drush\Boot\DrupalBootLevels;
use Drush\Commands\DrushCommands;
use Drupal\Core\DrupalKernel;
use Drupal\Core\Site\Settings;
use Drush\Drush;

/**
* cache:rebuild must not use a create() method or else it will not stop at SITE bootstrap.
*/
final class CacheRebuildCommands extends DrushCommands
{
const REBUILD = 'cache:rebuild';

/**
* 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
$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.'));
}
}
2 changes: 1 addition & 1 deletion src/Commands/core/DeployCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
3 changes: 2 additions & 1 deletion tests/functional/SqlDumpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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';
Expand Down

0 comments on commit 4baa8cf

Please sign in to comment.