Skip to content

Commit

Permalink
Use createEarly() for DI in CacheRebuildCommands (#5752)
Browse files Browse the repository at this point in the history
  • Loading branch information
weitzman authored Sep 8, 2023
1 parent 4baa8cf commit 7b024fa
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
4 changes: 2 additions & 2 deletions docs/dependency-injection.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ createEarly() method
Drush commands that need to be instantiated prior to bootstrap may do so by
utilizing the `createEarly()` static factory. This method looks and functions
exacty like the `create()` static factory, except it is only passed the Drush
container. The Drupal container is not avalable to command handlers that use
container. The Drupal container is not available to command handlers that use
`createEarly()`.

Note also that Drush commands packaged with Drupal modules are not discovered
until after Dupal bootstraps, and therefore cannot use `createEarly()`. This
until after Drupal bootstraps, and therefore cannot use `createEarly()`. This
mechanism is only usable by PSR-4 discovered commands packaged with Composer
projects that are *not* Drupal modules.

Expand Down
34 changes: 25 additions & 9 deletions src/Commands/core/CacheRebuildCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,38 @@

namespace Drush\Commands\core;

use Composer\Autoload\ClassLoader;
use Consolidation\SiteAlias\SiteAliasManager;
use Drush\Attributes as CLI;
use Drush\Boot\BootstrapManager;
use Drush\Boot\DrupalBootLevels;
use Drush\Commands\DrushCommands;
use Drupal\Core\DrupalKernel;
use Drupal\Core\Site\Settings;
use Drush\Drush;
use Psr\Container\ContainerInterface as DrushContainer;

/**
* 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';

public function __construct(
private BootstrapManager $bootstrapManager,
private ClassLoader $autoloader
) {
parent::__construct();
}

public static function createEarly(DrushContainer $drush_container): self
{
$commandHandler = new static(
$drush_container->get('bootstrap.manager'),
$drush_container->get('loader')
);

return $commandHandler;
}

/**
* Rebuild all caches.
*
Expand All @@ -35,19 +53,17 @@ public function rebuild($options = ['cache-clear' => 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();
$root = $this->bootstrapManager->getRoot();
require_once DRUSH_DRUPAL_CORE . '/includes/utility.inc';

$request = $bootstrapManager->bootstrap()->getRequest();
$request = $this->bootstrapManager->bootstrap()->getRequest();
DrupalKernel::bootEnvironment();

$site_path = DrupalKernel::findSitePath($request);
$autoloader = Drush::getContainer()->get('loader');
Settings::initialize($root, $site_path, $autoloader);
Settings::initialize($root, $site_path, $this->autoloader);

// drupal_rebuild() calls drupal_flush_all_caches() itself, so we don't do it manually.
drupal_rebuild($autoloader, $request);
drupal_rebuild($this->autoloader, $request);
$this->logger()->success(dt('Cache rebuild complete.'));
}
}

0 comments on commit 7b024fa

Please sign in to comment.