From f425f35feea4ec4394ebc317a10eff25959f916e Mon Sep 17 00:00:00 2001 From: Moshe Weitzman Date: Thu, 22 Feb 2024 16:04:20 -0400 Subject: [PATCH] Stop using SiteAliasAwareInterface (#5877) --- docs/dependency-injection.md | 75 ++----------------- examples/Commands/SiteAliasAlterCommands.php | 23 ++++-- src/Commands/config/ConfigCommands.php | 26 +++---- src/Commands/config/ConfigPullCommands.php | 35 ++++++--- src/Commands/core/ArchiveRestoreCommands.php | 46 +++++++----- src/Commands/core/BrowseCommands.php | 25 +++++-- src/Commands/core/CoreCommands.php | 6 +- src/Commands/core/DeployCommands.php | 31 +++++--- src/Commands/core/DeployHookCommands.php | 33 +++++--- src/Commands/core/DrupalDirectoryCommands.php | 31 +++++--- src/Commands/core/EditCommands.php | 25 +++++-- src/Commands/core/LoginCommands.php | 27 +++---- src/Commands/core/MkCommands.php | 11 +-- src/Commands/core/RoleCommands.php | 34 ++++++--- src/Commands/core/RsyncCommands.php | 37 ++++----- src/Commands/core/SiteCommands.php | 32 +++++--- src/Commands/core/SshCommands.php | 25 +++++-- src/Commands/core/StatusCommands.php | 31 +++++--- src/Commands/core/UpdateDBCommands.php | 25 +++++-- src/Commands/sql/SqlSyncCommands.php | 38 ++++++---- 20 files changed, 360 insertions(+), 256 deletions(-) diff --git a/docs/dependency-injection.md b/docs/dependency-injection.md index 1d8346c2c4..3f22573d19 100644 --- a/docs/dependency-injection.md +++ b/docs/dependency-injection.md @@ -3,35 +3,13 @@ Dependency Injection Drush command files obtain references to the resources they need through a technique called _dependency injection_. When using this programing paradigm, a class by convention will never use the `new` operator to instantiate dependencies. Instead, it will store the other objects it needs in class variables, and provide a way for other code to assign an object to that variable. -Types of Injection ------------------------ - -There are two ways that a class can receive its dependencies. One is called “constructor injection”, and the other is called “setter injection”. - -*Example of constructor injection:* -```php - public function __construct(DependencyType $service) - { - $this->service = $service; - } -``` - -*Example of setter injection:* -```php - public function setService(DependencyType $service) - { - $this->service = $service; - } -``` -The code that is responsible for providing the dependencies a class needs is usually an object called the dependency injection container. - create() method ------------------ :octicons-tag-24: 11.6+ !!! tip - Drush 11 and prior required [dependency injection via a drush.services.yml file](https://www.drush.org/11.x/dependency-injection/#services-files). This approach is deprecated in Drush 12 and will be removed in Drush 13. + Drush 11 and prior required [dependency injection via a drush.services.yml file](https://www.drush.org/11.x/dependency-injection/#services-files). This approach is deprecated in Drush 12+ and will be removed in Drush 13. Drush command files can inject services by adding a create() method to the commandfile. See [creating commands](commands.md) for instructions on how to use the Drupal Code Generator to create a simple command file starter. A create() method and a constructor will look something like this: ```php @@ -69,51 +47,8 @@ mechanism is only usable by PSR-4 discovered commands packaged with Composer projects that are *not* Drupal modules. Inflection -------------- - -!!! tip - - Inflection is deprecated in Drush 12; use `create()` or `createEarly()` instead. - Some classes are no longer available for inflection in Drush 12, and more (or potentially all) - may be removed in Drush 13. - -Drush will also inject dependencies that it provides using a technique called inflection. Inflection is a kind of dependency injection that works by way of a set of provided inflection interfaces, one for each available service. Each of these interfaces will define one or more setter methods (usually only one); these will automatically be called by Drush when the commandfile object is instantiated. The command only needs to implement this method and save the provided object in a class field. There is usually a corresponding trait that may be included via a `use` statement to fulfill this requirement. - -For example: - -```php -siteAliasManager()->getSelf(); - $this->logger()->success(‘The current alias is {name}’, [‘name’ => $selfAlias]); - return new ListDataFromKeys($aliasRecord->export()); - } -} -``` - -All Drush command files extend DrushCommands. DrushCommands implements ConfigAwareInterface, IOAwareInterface, LoggerAwareInterface, which gives access to `$this->getConfig()`, `$this->logger()` and other ways to do input and output. See the [IO documentation](io.md) for more information. - -Any additional services that are desired must be injected by implementing the appropriate inflection interface. - -Additional Interfaces: - -- SiteAliasManagerAwareInterface: The site alias manager [allows alias records to be obtained](site-alias-manager.md). -- CustomEventAwareInterface: Allows command files to [define and fire custom events](hooks.md) that other command files can hook. +----------------- +A command class may implement the following interfaces. When doing so, implement the corresponding trait to satisfy the interface. -Note that although the autoloader and Drush dependency injection container is available and may be injected into your command file if needed, this should be avoided. Favor using services that can be injected from Drupal or Drush. Some of the objects in the container are not part of the Drush public API, and may not maintain compatibility in minor and patch releases. +- [CustomEventAwareInterface](https://github.com/consolidation/annotated-command/blob/4.x/src/Events/CustomEventAwareInterface.php): Allows command files to [define and fire custom events](hooks.md) that other command files can hook. Example: [CacheCommands](https://github.com/drush-ops/drush/blob/13.x/src/Commands/core/CacheCommands.php) +- [StdinAwareInterface](https://github.com/consolidation/annotated-command/blob/4.x/src/Input/StdinAwareInterface.php): Read from standard input. This class contains facilities to redirect stdin to instead read from a file, e.g. in response to an option or argument value. Example: [CacheCommands](https://github.com/drush-ops/drush/blob/13.x/src/Commands/core/CacheCommands.php) \ No newline at end of file diff --git a/examples/Commands/SiteAliasAlterCommands.php b/examples/Commands/SiteAliasAlterCommands.php index a1c2b7a0b7..d8d73ba734 100644 --- a/examples/Commands/SiteAliasAlterCommands.php +++ b/examples/Commands/SiteAliasAlterCommands.php @@ -4,17 +4,30 @@ use Consolidation\AnnotatedCommand\AnnotationData; use Consolidation\AnnotatedCommand\Hooks\HookManager; -use Consolidation\SiteAlias\SiteAliasManagerAwareInterface; -use Consolidation\SiteAlias\SiteAliasManagerAwareTrait; +use Consolidation\SiteAlias\SiteAliasManagerInterface; use Drush\Attributes as CLI; +use League\Container\Container as DrushContainer; use Symfony\Component\Console\Input\InputInterface; /** * Load this example by using the --include option - e.g. `drush --include=/path/to/drush/examples` */ -class SiteAliasAlterCommands extends DrushCommands implements SiteAliasManagerAwareInterface +class SiteAliasAlterCommands extends DrushCommands { - use SiteAliasManagerAwareTrait; + public function __construct( + private readonly SiteAliasManagerInterface $siteAliasManager + ) { + parent::__construct(); + } + + public static function createEarly(DrushContainer $drush_container): self + { + $commandHandler = new static( + $drush_container->get('site.alias.manager'), + ); + + return $commandHandler; + } /** * A few example alterations to site aliases. @@ -22,7 +35,7 @@ class SiteAliasAlterCommands extends DrushCommands implements SiteAliasManagerAw #[CLI\Hook(type: HookManager::PRE_INITIALIZE, target: '*')] public function alter(InputInterface $input, AnnotationData $annotationData) { - $self = $this->siteAliasManager()->getSelf(); + $self = $this->siteAliasManager->getSelf(); if ($self->isRemote()) { // Always pass along ssh keys. if (!$self->has('ssh.options')) { diff --git a/src/Commands/config/ConfigCommands.php b/src/Commands/config/ConfigCommands.php index c65f2f9fd1..8ee9dc4550 100644 --- a/src/Commands/config/ConfigCommands.php +++ b/src/Commands/config/ConfigCommands.php @@ -4,18 +4,18 @@ namespace Drush\Commands\config; -use Consolidation\AnnotatedCommand\Hooks\HookManager; -use Drupal\Core\Config\ConfigDirectoryNotDefinedException; -use Drupal\Core\Config\ImportStorageTransformer; -use Consolidation\AnnotatedCommand\CommandError; use Consolidation\AnnotatedCommand\CommandData; +use Consolidation\AnnotatedCommand\CommandError; +use Consolidation\AnnotatedCommand\Hooks\HookManager; use Consolidation\AnnotatedCommand\Input\StdinAwareInterface; use Consolidation\AnnotatedCommand\Input\StdinAwareTrait; use Consolidation\OutputFormatters\StructuredData\RowsOfFields; -use Consolidation\SiteAlias\SiteAliasManagerAwareTrait; +use Consolidation\SiteAlias\SiteAliasManagerInterface; use Consolidation\SiteProcess\Util\Escape; +use Drupal\Core\Config\ConfigDirectoryNotDefinedException; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Config\FileStorage; +use Drupal\Core\Config\ImportStorageTransformer; use Drupal\Core\Config\StorageComparer; use Drupal\Core\Config\StorageInterface; use Drupal\Core\Site\Settings; @@ -23,23 +23,22 @@ use Drush\Commands\DrushCommands; use Drush\Drush; use Drush\Exec\ExecTrait; -use Drush\SiteAlias\SiteAliasManagerAwareInterface; use Drush\Utils\FsUtils; use Drush\Utils\StringUtils; use JetBrains\PhpStorm\Deprecated; +use League\Container\Container; use Symfony\Component\Console\Completion\CompletionInput; use Symfony\Component\Console\Completion\CompletionSuggestions; use Symfony\Component\Console\Helper\Table; use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\Filesystem\Path; use Symfony\Component\Yaml\Parser; -use Symfony\Component\DependencyInjection\ContainerInterface; -final class ConfigCommands extends DrushCommands implements StdinAwareInterface, SiteAliasManagerAwareInterface +final class ConfigCommands extends DrushCommands implements StdinAwareInterface { use StdinAwareTrait; use ExecTrait; - use SiteAliasManagerAwareTrait; const INTERACT_CONFIG_NAME = 'interact-config-name'; const VALIDATE_CONFIG_NAME = 'validate-config-name'; @@ -58,16 +57,17 @@ public function getConfigFactory(): ConfigFactoryInterface return $this->configFactory; } - protected function __construct(protected ConfigFactoryInterface $configFactory, protected StorageInterface $configStorage) + protected function __construct(protected ConfigFactoryInterface $configFactory, protected StorageInterface $configStorage, protected SiteAliasManagerInterface $siteAliasManager) { parent::__construct(); } - public static function create(ContainerInterface $container): self + public static function create(ContainerInterface $container, Container $drush_container): self { $commandHandler = new static( $container->get('config.factory'), - $container->get('config.storage') + $container->get('config.storage'), + $drush_container->get('site.alias.manager') ); if ($container->has('config.storage.export')) { @@ -242,7 +242,7 @@ public function edit($config_name, $options = []): void // Perform import operation if user did not immediately exit editor. if (!$options['bg']) { $redispatch_options = Drush::redispatchOptions() + ['strict' => 0, 'partial' => true, 'source' => $temp_dir]; - $self = $this->siteAliasManager()->getSelf(); + $self = $this->siteAliasManager->getSelf(); $process = $this->processManager()->drush($self, ConfigImportCommands::IMPORT, [], $redispatch_options); $process->mustRun($process->showRealtime()); } diff --git a/src/Commands/config/ConfigPullCommands.php b/src/Commands/config/ConfigPullCommands.php index a6fdd1be4f..afb4037545 100644 --- a/src/Commands/config/ConfigPullCommands.php +++ b/src/Commands/config/ConfigPullCommands.php @@ -6,22 +6,35 @@ use Consolidation\AnnotatedCommand\CommandData; use Consolidation\AnnotatedCommand\Hooks\HookManager; +use Consolidation\OutputFormatters\StructuredData\PropertyList; +use Consolidation\SiteAlias\HostPath; +use Consolidation\SiteAlias\SiteAliasManagerInterface; use Drush\Attributes as CLI; use Drush\Commands\core\DocsCommands; use Drush\Commands\core\RsyncCommands; use Drush\Commands\DrushCommands; use Drush\Drush; -use Consolidation\SiteAlias\HostPath; -use Consolidation\SiteAlias\SiteAliasManagerAwareInterface; -use Consolidation\SiteAlias\SiteAliasManagerAwareTrait; -use Consolidation\OutputFormatters\StructuredData\PropertyList; +use League\Container\Container as DrushContainer; -final class ConfigPullCommands extends DrushCommands implements SiteAliasManagerAwareInterface +final class ConfigPullCommands extends DrushCommands { - use SiteAliasManagerAwareTrait; - const PULL = 'config:pull'; + public function __construct( + private readonly SiteAliasManagerInterface $siteAliasManager + ) { + parent::__construct(); + } + + public static function createEarly(DrushContainer $drush_container): self + { + $commandHandler = new static( + $drush_container->get('site.alias.manager'), + ); + + return $commandHandler; + } + /** * Export and transfer config from one environment to another. */ @@ -37,7 +50,7 @@ final class ConfigPullCommands extends DrushCommands implements SiteAliasManager public function pull(string $source, string $destination, array $options = ['safe' => false, 'runner' => null, 'format' => 'null']): PropertyList { $global_options = Drush::redispatchOptions() + ['strict' => 0]; - $sourceRecord = $this->siteAliasManager()->get($source); + $sourceRecord = $this->siteAliasManager->get($source); $export_options = [ // Use the standard backup directory on Destination. @@ -61,11 +74,11 @@ public function pull(string $source, string $destination, array $options = ['saf if (!str_contains($destination, ':')) { $destination .= ':%config-sync'; } - $destinationHostPath = HostPath::create($this->siteAliasManager(), $destination); + $destinationHostPath = HostPath::create($this->siteAliasManager, $destination); if (!$runner = $options['runner']) { $destinationRecord = $destinationHostPath->getSiteAlias(); - $runner = $sourceRecord->isRemote() && $destinationRecord->isRemote() ? $destinationRecord : $this->siteAliasManager()->getSelf(); + $runner = $sourceRecord->isRemote() && $destinationRecord->isRemote() ? $destinationRecord : $this->siteAliasManager->getSelf(); } $this->logger() ->notice(dt('Starting to rsync configuration files from !source to !dest.', [ @@ -87,7 +100,7 @@ public function pull(string $source, string $destination, array $options = ['saf public function validateConfigPull(CommandData $commandData): void { if ($commandData->input()->getOption('safe')) { - $destinationRecord = $this->siteAliasManager()->get($commandData->input()->getArgument('destination')); + $destinationRecord = $this->siteAliasManager->get($commandData->input()->getArgument('destination')); $process = $this->processManager()->siteProcess($destinationRecord, ['git', 'diff', '--quiet']); $process->chdirToSiteRoot(); $process->run(); diff --git a/src/Commands/core/ArchiveRestoreCommands.php b/src/Commands/core/ArchiveRestoreCommands.php index 99b8cebaea..0482134e94 100644 --- a/src/Commands/core/ArchiveRestoreCommands.php +++ b/src/Commands/core/ArchiveRestoreCommands.php @@ -4,32 +4,30 @@ namespace Drush\Commands\core; +use Consolidation\SiteAlias\HostPath; +use Consolidation\SiteAlias\SiteAlias; use Consolidation\SiteAlias\SiteAliasManager; +use Consolidation\SiteAlias\SiteAliasManagerInterface; use Consolidation\SiteProcess\ProcessBase; -use Consolidation\SiteAlias\SiteAlias; -use Consolidation\SiteAlias\SiteAliasManagerAwareInterface; -use Consolidation\SiteAlias\SiteAliasManagerAwareTrait; use Drupal; use DrupalFinder\DrupalFinder; use Drush\Attributes as CLI; use Drush\Backend\BackendPathEvaluator; +use Drush\Boot\BootstrapManager; use Drush\Boot\DrupalBootLevels; use Drush\Commands\DrushCommands; -use Drush\Drush; use Drush\Exceptions\UserAbortException; -use Consolidation\SiteAlias\HostPath; use Drush\Sql\SqlBase; use Drush\Utils\FsUtils; use Exception; +use League\Container\Container as DrushContainer; use PharData; use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\Filesystem\Path; use Throwable; -final class ArchiveRestoreCommands extends DrushCommands implements SiteAliasManagerAwareInterface +final class ArchiveRestoreCommands extends DrushCommands { - use SiteAliasManagerAwareTrait; - const RESTORE = 'archive:restore'; private Filesystem $filesystem; private ?string $destinationPath = null; @@ -44,6 +42,23 @@ final class ArchiveRestoreCommands extends DrushCommands implements SiteAliasMan private const TEMP_DIR_NAME = 'uncompressed'; + public function __construct( + private readonly BootstrapManager $bootstrapManager, + private readonly SiteAliasManagerInterface $siteAliasManager + ) { + parent::__construct(); + } + + public static function createEarly(DrushContainer $drush_container): self + { + $commandHandler = new static( + $drush_container->get('bootstrap.manager'), + $drush_container->get('site.alias.manager'), + ); + + return $commandHandler; + } + /** * Restore (import) your code, files, and database. */ @@ -159,8 +174,7 @@ public function restore( // If the destination path was not specified, extract over the current site if (!$this->destinationPath) { - $bootstrapManager = Drush::bootstrapManager(); - $this->destinationPath = $bootstrapManager->getComposerRoot(); + $this->destinationPath = $this->bootstrapManager->getComposerRoot(); } // If there isn't a current site either, then extract to the cwd @@ -347,11 +361,10 @@ protected function fileImportAbsolutePath(?string $destinationRelative): string } // If we are extracting over an existing site, query Drupal to get the files path - $bootstrapManager = Drush::bootstrapManager(); - $path = $bootstrapManager->getComposerRoot(); + $path = $this->bootstrapManager->getComposerRoot(); if (!empty($path)) { try { - $bootstrapManager->doBootstrap(DrupalBootLevels::FULL); + $this->bootstrapManager->doBootstrap(DrupalBootLevels::FULL); return Drupal::service('file_system')->realpath('public://'); } catch (Throwable $t) { $this->logger()->warning('Could not bootstrap Drupal site at destination to determine file path'); @@ -412,12 +425,10 @@ protected function getSiteAlias(?string $site): SiteAlias { $pathEvaluator = new BackendPathEvaluator(); /** @var SiteAliasManager $manager */ - $manager = $this->siteAliasManager(); - if (null !== $site) { $site .= ':%root'; } - $evaluatedPath = HostPath::create($manager, $site); + $evaluatedPath = HostPath::create($this->siteAliasManager, $site); $pathEvaluator->evaluate($evaluatedPath); return $evaluatedPath->getSiteAlias(); @@ -542,8 +553,7 @@ protected function importDatabase(string $databaseDumpPath, array $options): voi } elseif ($options['destination-path']) { throw new Exception('Database connection settings are required if --destination-path option is provided'); } else { - $bootstrapManager = Drush::bootstrapManager(); - $bootstrapManager->doBootstrap(DrupalBootLevels::CONFIGURATION); + $this->bootstrapManager->doBootstrap(DrupalBootLevels::CONFIGURATION); } try { diff --git a/src/Commands/core/BrowseCommands.php b/src/Commands/core/BrowseCommands.php index 53e20efb99..aee66ec372 100644 --- a/src/Commands/core/BrowseCommands.php +++ b/src/Commands/core/BrowseCommands.php @@ -4,22 +4,37 @@ namespace Drush\Commands\core; +use Consolidation\SiteAlias\SiteAliasManagerInterface; use Drupal\Core\Url; use Drush\Attributes as CLI; use Drush\Boot\DrupalBootLevels; use Drush\Commands\DrushCommands; use Drush\Drush; use Drush\Exec\ExecTrait; -use Consolidation\SiteAlias\SiteAliasManagerAwareInterface; -use Consolidation\SiteAlias\SiteAliasManagerAwareTrait; +use League\Container\Container as DrushContainer; +use Symfony\Component\DependencyInjection\ContainerInterface; -final class BrowseCommands extends DrushCommands implements SiteAliasManagerAwareInterface +final class BrowseCommands extends DrushCommands { use ExecTrait; - use SiteAliasManagerAwareTrait; const BROWSE = 'browse'; + public function __construct( + private SiteAliasManagerInterface $siteAliasManager + ) { + parent::__construct(); + } + + public static function create(ContainerInterface $container, DrushContainer $drush_container): self + { + $commandHandler = new static( + $drush_container->get('site.alias.manager'), + ); + + return $commandHandler; + } + /** * Display a link to a given path or open link in a browser. */ @@ -33,7 +48,7 @@ final class BrowseCommands extends DrushCommands implements SiteAliasManagerAwar #[CLI\HandleRemoteCommands] public function browse($path = '', array $options = ['browser' => true, 'redirect-port' => self::REQ]) { - $aliasRecord = $this->siteAliasManager()->getSelf(); + $aliasRecord = $this->siteAliasManager->getSelf(); // Redispatch if called against a remote-host so a browser is started on the // the *local* machine. if ($this->processManager()->hasTransport($aliasRecord)) { diff --git a/src/Commands/core/CoreCommands.php b/src/Commands/core/CoreCommands.php index a3977f8ac0..792a5726e0 100644 --- a/src/Commands/core/CoreCommands.php +++ b/src/Commands/core/CoreCommands.php @@ -9,14 +9,10 @@ use Drush\Commands\DrushCommands; use Drush\Drush; use Consolidation\OutputFormatters\StructuredData\RowsOfFields; -use Consolidation\SiteAlias\SiteAliasManagerAwareInterface; -use Consolidation\SiteAlias\SiteAliasManagerAwareTrait; use Consolidation\OutputFormatters\Options\FormatterOptions; -final class CoreCommands extends DrushCommands implements SiteAliasManagerAwareInterface +final class CoreCommands extends DrushCommands { - use SiteAliasManagerAwareTrait; - const VERSION = 'version'; const GLOBAL_OPTIONS = 'core:global-options'; diff --git a/src/Commands/core/DeployCommands.php b/src/Commands/core/DeployCommands.php index ec6923fa90..5e1b269ffc 100644 --- a/src/Commands/core/DeployCommands.php +++ b/src/Commands/core/DeployCommands.php @@ -5,22 +5,35 @@ namespace Drush\Commands\core; use Consolidation\SiteAlias\SiteAlias; -use Consolidation\SiteAlias\SiteAliasManagerAwareTrait; +use Consolidation\SiteAlias\SiteAliasManagerInterface; use Consolidation\SiteProcess\ProcessManager; +use Drupal\Component\DependencyInjection\ContainerInterface; use Drush\Attributes as CLI; -use Drush\Commands\DrushCommands; +use Drush\Boot\DrupalBootLevels; use Drush\Commands\config\ConfigImportCommands; -use Drush\Commands\core\DeployHookCommands; +use Drush\Commands\DrushCommands; use Drush\Drush; -use Drush\SiteAlias\SiteAliasManagerAwareInterface; -use Drush\Boot\DrupalBootLevels; +use League\Container\Container as DrushContainer; -final class DeployCommands extends DrushCommands implements SiteAliasManagerAwareInterface +final class DeployCommands extends DrushCommands { - use SiteAliasManagerAwareTrait; - const DEPLOY = 'deploy'; + public function __construct( + private readonly SiteAliasManagerInterface $siteAliasManager + ) { + parent::__construct(); + } + + public static function create(ContainerInterface $container, DrushContainer $drush_container): self + { + $commandHandler = new static( + $drush_container->get('site.alias.manager'), + ); + + return $commandHandler; + } + /** * Run several commands after performing a code deployment. */ @@ -31,7 +44,7 @@ final class DeployCommands extends DrushCommands implements SiteAliasManagerAwar #[CLI\Bootstrap(level: DrupalBootLevels::FULL)] public function deploy(): void { - $self = $this->siteAliasManager()->getSelf(); + $self = $this->siteAliasManager->getSelf(); $redispatchOptions = Drush::redispatchOptions(); $manager = $this->processManager(); diff --git a/src/Commands/core/DeployHookCommands.php b/src/Commands/core/DeployHookCommands.php index c9bd8e7a90..48fb92e5e8 100644 --- a/src/Commands/core/DeployHookCommands.php +++ b/src/Commands/core/DeployHookCommands.php @@ -4,30 +4,43 @@ namespace Drush\Commands\core; -use Drush\Attributes as CLI; -use Drush\Boot\DrupalBootLevels; -use Drush\Commands\core\DocsCommands; -use Drush\Log\SuccessInterface; use Consolidation\OutputFormatters\StructuredData\RowsOfFields; use Consolidation\OutputFormatters\StructuredData\UnstructuredListData; -use Consolidation\SiteAlias\SiteAliasManagerAwareTrait; -use Consolidation\SiteAlias\SiteAliasManagerAwareInterface; +use Consolidation\SiteAlias\SiteAliasManagerInterface; +use Drupal\Component\DependencyInjection\ContainerInterface; use Drupal\Core\Update\UpdateRegistry; use Drupal\Core\Utility\Error; +use Drush\Attributes as CLI; +use Drush\Boot\DrupalBootLevels; use Drush\Commands\DrushCommands; use Drush\Drush; use Drush\Exceptions\UserAbortException; +use Drush\Log\SuccessInterface; +use League\Container\Container as DrushContainer; use Psr\Log\LogLevel; -final class DeployHookCommands extends DrushCommands implements SiteAliasManagerAwareInterface +final class DeployHookCommands extends DrushCommands { - use SiteAliasManagerAwareTrait; - const HOOK_STATUS = 'deploy:hook-status'; const HOOK = 'deploy:hook'; const BATCH_PROCESS = 'deploy:batch-process'; const MARK_COMPLETE = 'deploy:mark-complete'; + public function __construct( + private readonly SiteAliasManagerInterface $siteAliasManager + ) { + parent::__construct(); + } + + public static function create(ContainerInterface $container, DrushContainer $drush_container): self + { + $commandHandler = new static( + $drush_container->get('site.alias.manager'), + ); + + return $commandHandler; + } + /** * Get the deploy hook update registry. */ @@ -95,7 +108,7 @@ public function run(): int return self::EXIT_SUCCESS; } - $process = $this->processManager()->drush($this->siteAliasManager()->getSelf(), self::HOOK_STATUS); + $process = $this->processManager()->drush($this->siteAliasManager->getSelf(), self::HOOK_STATUS); $process->mustRun(); $this->output()->writeln($process->getOutput()); diff --git a/src/Commands/core/DrupalDirectoryCommands.php b/src/Commands/core/DrupalDirectoryCommands.php index 1d9ca44aed..220d9ea5b1 100644 --- a/src/Commands/core/DrupalDirectoryCommands.php +++ b/src/Commands/core/DrupalDirectoryCommands.php @@ -4,27 +4,36 @@ namespace Drush\Commands\core; -use Drush\Commands\DrushCommands; use Consolidation\SiteAlias\HostPath; -use Consolidation\SiteAlias\SiteAliasManagerAwareInterface; -use Consolidation\SiteAlias\SiteAliasManagerAwareTrait; +use Consolidation\SiteAlias\SiteAliasManagerInterface; +use Drupal\Component\DependencyInjection\ContainerInterface; use Drush\Attributes as CLI; use Drush\Backend\BackendPathEvaluator; +use Drush\Commands\DrushCommands; +use League\Container\Container as DrushContainer; -final class DrupalDirectoryCommands extends DrushCommands implements SiteAliasManagerAwareInterface +final class DrupalDirectoryCommands extends DrushCommands { - use SiteAliasManagerAwareTrait; - const DIRECTORY = 'drupal:directory'; - /** @var BackendPathEvaluator */ - protected $pathEvaluator; + protected BackendPathEvaluator $pathEvaluator; - public function __construct() - { + public function __construct( + private readonly SiteAliasManagerInterface $siteAliasManager + ) { + parent::__construct(); $this->pathEvaluator = new BackendPathEvaluator(); } + public static function create(ContainerInterface $container, DrushContainer $drush_container): self + { + $commandHandler = new static( + $drush_container->get('site.alias.manager'), + ); + + return $commandHandler; + } + /** * Return the filesystem path for modules/themes and other key folders. */ @@ -69,7 +78,7 @@ protected function getPath($target = 'root', $local_only = false) $target = "%$target"; } // Set up the evaluated path; fail if --local-only and the site alias is remote - $evaluatedPath = HostPath::create($this->siteAliasManager(), $target); + $evaluatedPath = HostPath::create($this->siteAliasManager, $target); if ($local_only && $evaluatedPath->isRemote()) { throw new \Exception(dt('{target} was remote, and --local-only was specified', ['target' => $target])); } diff --git a/src/Commands/core/EditCommands.php b/src/Commands/core/EditCommands.php index 41b52462b1..baa1ddcd0c 100644 --- a/src/Commands/core/EditCommands.php +++ b/src/Commands/core/EditCommands.php @@ -4,22 +4,37 @@ namespace Drush\Commands\core; +use Consolidation\SiteAlias\SiteAliasManagerInterface; use Consolidation\SiteProcess\Util\Escape; +use Drupal\Component\DependencyInjection\ContainerInterface; use Drush\Attributes as CLI; use Drush\Boot\DrupalBootLevels; use Drush\Commands\DrushCommands; use Drush\Drush; -use Consolidation\SiteAlias\SiteAliasManagerAwareInterface; -use Consolidation\SiteAlias\SiteAliasManagerAwareTrait; use Drush\Exec\ExecTrait; +use League\Container\Container as DrushContainer; -final class EditCommands extends DrushCommands implements SiteAliasManagerAwareInterface +final class EditCommands extends DrushCommands { - use SiteAliasManagerAwareTrait; use ExecTrait; const EDIT = 'core:edit'; + public function __construct( + private readonly SiteAliasManagerInterface $siteAliasManager + ) { + parent::__construct(); + } + + public static function create(ContainerInterface $container, DrushContainer $drush_container): self + { + $commandHandler = new static( + $drush_container->get('site.alias.manager'), + ); + + return $commandHandler; + } + /** * Edit drush.yml, site alias, and Drupal settings.php files. */ @@ -90,7 +105,7 @@ public function load($headers = true): array } } - if ($aliases = $this->siteAliasManager()->listAllFilePaths()) { + if ($aliases = $this->siteAliasManager->listAllFilePaths()) { sort($aliases); $aliases = array_combine($aliases, $aliases); if ($headers) { diff --git a/src/Commands/core/LoginCommands.php b/src/Commands/core/LoginCommands.php index 082249ba00..f27a8f0a9f 100644 --- a/src/Commands/core/LoginCommands.php +++ b/src/Commands/core/LoginCommands.php @@ -4,39 +4,34 @@ namespace Drush\Commands\core; -use Drupal\Component\Datetime\TimeInterface; -use Drupal\Core\Config\ConfigFactoryInterface; -use Drupal\Core\Extension\ModuleHandlerInterface; -use Drupal\Core\Language\LanguageManagerInterface; -use Drupal\Core\State\StateInterface; -use Drush\Attributes as CLI; +use Consolidation\SiteAlias\SiteAliasManagerInterface; +use Drupal\Core\Url; use Drupal\user\Entity\User; +use Drush\Attributes as CLI; use Drush\Boot\BootstrapManager; use Drush\Boot\DrupalBootLevels; use Drush\Commands\DrushCommands; use Drush\Drush; use Drush\Exec\ExecTrait; -use Consolidation\SiteAlias\SiteAliasManagerAwareInterface; -use Consolidation\SiteAlias\SiteAliasManagerAwareTrait; -use Drupal\Core\Url; -use Symfony\Component\DependencyInjection\ContainerInterface; -final class LoginCommands extends DrushCommands implements SiteAliasManagerAwareInterface +final class LoginCommands extends DrushCommands { - use SiteAliasManagerAwareTrait; use ExecTrait; const LOGIN = 'user:login'; - public function __construct(private BootstrapManager $bootstrapManager) - { + public function __construct( + private readonly BootstrapManager $bootstrapManager, + private readonly SiteAliasManagerInterface $siteAliasManager + ) { parent::__construct(); } public static function createEarly($drush_container): self { $commandHandler = new static( - $drush_container->get('bootstrap.manager') + $drush_container->get('bootstrap.manager'), + $drush_container->get('site.alias.manager') ); return $commandHandler; @@ -62,7 +57,7 @@ public function login(string $path = '', $options = ['name' => null, 'uid' => nu { // Redispatch if called against a remote-host so a browser is started on the // the *local* machine. - $aliasRecord = $this->siteAliasManager()->getSelf(); + $aliasRecord = $this->siteAliasManager->getSelf(); if ($this->processManager()->hasTransport($aliasRecord)) { $process = $this->processManager()->drush($aliasRecord, self::LOGIN, [$path], Drush::redispatchOptions()); $process->mustRun(); diff --git a/src/Commands/core/MkCommands.php b/src/Commands/core/MkCommands.php index ffaadf7e63..dd8f130402 100644 --- a/src/Commands/core/MkCommands.php +++ b/src/Commands/core/MkCommands.php @@ -6,7 +6,6 @@ use Consolidation\AnnotatedCommand\AnnotatedCommand; use Consolidation\AnnotatedCommand\AnnotationData; -use Consolidation\SiteAlias\SiteAliasManagerAwareTrait; use Drush\Attributes as CLI; use Drush\Boot\DrupalBootLevels; use Drush\Commands\DrushCommands; @@ -14,7 +13,6 @@ use Drush\Commands\help\HelpCLIFormatter; use Drush\Commands\help\ListCommands; use Drush\Drush; -use Drush\SiteAlias\SiteAliasManagerAwareInterface; use Psr\Container\ContainerInterface as DrushContainer; use Symfony\Component\Console\Application; use Symfony\Component\Console\Command\Command; @@ -25,14 +23,13 @@ use Symfony\Component\Filesystem\Path; use Symfony\Component\Yaml\Yaml; -final class MkCommands extends DrushCommands implements SiteAliasManagerAwareInterface +final class MkCommands extends DrushCommands { - use SiteAliasManagerAwareTrait; - protected function __construct( - private ContainerInterface $container, - private DrushContainer $drush_container, + private readonly ContainerInterface $container, + private readonly DrushContainer $drush_container, ) { + parent::__construct(); } public static function create(ContainerInterface $container, DrushContainer $drush_container): self diff --git a/src/Commands/core/RoleCommands.php b/src/Commands/core/RoleCommands.php index a6526451cc..c9a9df2edd 100644 --- a/src/Commands/core/RoleCommands.php +++ b/src/Commands/core/RoleCommands.php @@ -5,27 +5,41 @@ namespace Drush\Commands\core; use Consolidation\OutputFormatters\Options\FormatterOptions; -use Consolidation\SiteAlias\SiteAliasManagerAwareTrait; +use Consolidation\OutputFormatters\StructuredData\RowsOfFields; +use Consolidation\SiteAlias\SiteAliasManagerInterface; +use Drupal\Component\DependencyInjection\ContainerInterface; use Drupal\user\Entity\Role; use Drush\Attributes as CLI; +use Drush\Boot\DrupalBootLevels; use Drush\Commands\DrushCommands; -use Consolidation\OutputFormatters\StructuredData\RowsOfFields; -use Drush\SiteAlias\SiteAliasManagerAwareInterface; use Drush\Utils\StringUtils; +use League\Container\Container as DrushContainer; use Symfony\Component\Console\Completion\CompletionInput; use Symfony\Component\Console\Completion\CompletionSuggestions; -use Drush\Boot\DrupalBootLevels; -final class RoleCommands extends DrushCommands implements SiteAliasManagerAwareInterface +final class RoleCommands extends DrushCommands { - use SiteAliasManagerAwareTrait; - const CREATE = 'role:create'; const DELETE = 'role:delete'; const PERM_ADD = 'role:perm:add'; const PERM_REMOVE = 'role:perm:remove'; const LIST = 'role:list'; + public function __construct( + private readonly SiteAliasManagerInterface $siteAliasManager + ) { + parent::__construct(); + } + + public static function create(ContainerInterface $container, DrushContainer $drush_container): self + { + $commandHandler = new static( + $drush_container->get('site.alias.manager'), + ); + + return $commandHandler; + } + /** * Create a new role. */ @@ -34,7 +48,7 @@ final class RoleCommands extends DrushCommands implements SiteAliasManagerAwareI #[CLI\Argument(name: 'human_readable_name', description: 'A descriptive name for the role.')] #[CLI\Usage(name: "drush role:create 'test_role' 'Test role'", description: "Create a new role with a machine name of 'test_role', and a human-readable name of 'Test role'.")] #[CLI\Bootstrap(level: DrupalBootLevels::FULL)] - public function create($machine_name, $human_readable_name = null) + public function createRole($machine_name, $human_readable_name = null) { $role = Role::create([ 'id' => $machine_name, @@ -78,7 +92,7 @@ public function roleAddPerm($machine_name, $permissions): void $perms = StringUtils::csvToArray($permissions); user_role_grant_permissions($machine_name, $perms); $this->logger()->success(dt('Added "!permissions" to "!role"', ['!permissions' => $permissions, '!role' => $machine_name])); - $this->processManager()->drush($this->siteAliasManager()->getSelf(), CacheRebuildCommands::REBUILD); + $this->processManager()->drush($this->siteAliasManager->getSelf(), CacheRebuildCommands::REBUILD); } /** @@ -97,7 +111,7 @@ public function roleRemovePerm($machine_name, $permissions): void $perms = StringUtils::csvToArray($permissions); user_role_revoke_permissions($machine_name, $perms); $this->logger()->success(dt('Removed "!permissions" to "!role"', ['!permissions' => $permissions, '!role' => $machine_name])); - $this->processManager()->drush($this->siteAliasManager()->getSelf(), CacheRebuildCommands::REBUILD); + $this->processManager()->drush($this->siteAliasManager->getSelf(), CacheRebuildCommands::REBUILD); } /** diff --git a/src/Commands/core/RsyncCommands.php b/src/Commands/core/RsyncCommands.php index 46ef45b691..4839947bb2 100644 --- a/src/Commands/core/RsyncCommands.php +++ b/src/Commands/core/RsyncCommands.php @@ -6,23 +6,18 @@ use Consolidation\AnnotatedCommand\CommandData; use Consolidation\AnnotatedCommand\Hooks\HookManager; -use Consolidation\SiteProcess\ProcessBase; +use Consolidation\SiteAlias\HostPath; +use Consolidation\SiteAlias\SiteAliasManagerInterface; use Consolidation\SiteProcess\Util\Escape; use Drush\Attributes as CLI; -use Drush\Commands\DrushCommands; -use Drush\Drush; -use Drush\Exceptions\UserAbortException; -use Consolidation\SiteAlias\HostPath; -use Consolidation\SiteAlias\SiteAliasManagerAwareInterface; -use Consolidation\SiteAlias\SiteAliasManagerAwareTrait; use Drush\Backend\BackendPathEvaluator; +use Drush\Commands\DrushCommands; use Drush\Config\ConfigLocator; +use Drush\Exceptions\UserAbortException; use Symfony\Component\Console\Event\ConsoleCommandEvent; -final class RsyncCommands extends DrushCommands implements SiteAliasManagerAwareInterface +final class RsyncCommands extends DrushCommands { - use SiteAliasManagerAwareTrait; - /** * These are arguments after the aliases and paths have been evaluated. * @see validate(). @@ -32,16 +27,26 @@ final class RsyncCommands extends DrushCommands implements SiteAliasManagerAware public $sourceEvaluatedPath; /** @var HostPath */ public $targetEvaluatedPath; - /** @var BackendPathEvaluator */ - protected $pathEvaluator; + protected BackendPathEvaluator $pathEvaluator; - public function __construct() - { + public function __construct( + private readonly SiteAliasManagerInterface $siteAliasManager + ) { + parent::__construct(); // TODO: once the BackendInvoke service exists, inject it here // and use it to get the path evaluator $this->pathEvaluator = new BackendPathEvaluator(); } + public static function createEarly($drush_container): self + { + $commandHandler = new static( + $drush_container->get('site.alias.manager') + ); + + return $commandHandler; + } + /** * Rsync Drupal code or files to/from another server using ssh. */ @@ -129,10 +134,8 @@ protected function injectAliasPathParameterOptions($input, $parameterName) // context, that already has the options et. al. from the // site-selection alias ('drush @site rsync ...'), @self. $aliasConfigContext = $this->getConfig()->getContext(ConfigLocator::ALIAS_CONTEXT); - $manager = $this->siteAliasManager(); - $aliasName = $input->getArgument($parameterName); - $evaluatedPath = HostPath::create($manager, $aliasName); + $evaluatedPath = HostPath::create($this->siteAliasManager, $aliasName); $this->pathEvaluator->evaluate($evaluatedPath); $aliasRecord = $evaluatedPath->getSiteAlias(); diff --git a/src/Commands/core/SiteCommands.php b/src/Commands/core/SiteCommands.php index e16e7961dd..65abe2cfff 100644 --- a/src/Commands/core/SiteCommands.php +++ b/src/Commands/core/SiteCommands.php @@ -4,19 +4,33 @@ namespace Drush\Commands\core; +use Consolidation\OutputFormatters\StructuredData\UnstructuredListData; +use Consolidation\SiteAlias\SiteAliasManagerInterface; +use Drupal\Component\DependencyInjection\ContainerInterface; use Drush\Attributes as CLI; use Drush\Commands\DrushCommands; -use Consolidation\SiteAlias\SiteAliasManagerAwareInterface; -use Consolidation\SiteAlias\SiteAliasManagerAwareTrait; -use Consolidation\OutputFormatters\StructuredData\UnstructuredListData; +use League\Container\Container as DrushContainer; -final class SiteCommands extends DrushCommands implements SiteAliasManagerAwareInterface +final class SiteCommands extends DrushCommands { - use SiteAliasManagerAwareTrait; - const SET = 'site:set'; const ALIAS = 'site:alias'; + public function __construct( + private readonly SiteAliasManagerInterface $siteAliasManager + ) { + parent::__construct(); + } + + public static function create(ContainerInterface $container, DrushContainer $drush_container): self + { + $commandHandler = new static( + $drush_container->get('site.alias.manager'), + ); + + return $commandHandler; + } + /** * Set a site alias that will persist for the current session. * @@ -63,7 +77,7 @@ public function siteSet(string $site = '@none'): void } } // Alias record lookup exists. - $aliasRecord = $this->siteAliasManager()->get($site); + $aliasRecord = $this->siteAliasManager->get($site); if ($aliasRecord) { if (file_exists($filename)) { @unlink($last_site_filename); @@ -100,13 +114,13 @@ public function siteAlias($site = null, array $options = ['format' => 'yaml']): { // First check to see if the user provided a specification that matches // multiple sites. - $aliasList = $this->siteAliasManager()->getMultiple($site); + $aliasList = $this->siteAliasManager->getMultiple($site); if (is_array($aliasList) && !empty($aliasList)) { return new UnstructuredListData($this->siteAliasExportList($aliasList, $options)); } // Next check for a specific alias or a site specification. - $aliasRecord = $this->siteAliasManager()->get($site); + $aliasRecord = $this->siteAliasManager->get($site); if ($aliasRecord !== false) { return new UnstructuredListData([$aliasRecord->name() => $aliasRecord->export()]); } diff --git a/src/Commands/core/SshCommands.php b/src/Commands/core/SshCommands.php index 0928a83cc2..9e1388bd17 100644 --- a/src/Commands/core/SshCommands.php +++ b/src/Commands/core/SshCommands.php @@ -4,19 +4,32 @@ namespace Drush\Commands\core; +use Consolidation\SiteAlias\SiteAliasManagerInterface; use Consolidation\SiteProcess\Util\Shell; use Consolidation\SiteProcess\Util\Tty; use Drush\Attributes as CLI; use Drush\Commands\DrushCommands; -use Consolidation\SiteAlias\SiteAliasManagerAwareInterface; -use Consolidation\SiteAlias\SiteAliasManagerAwareTrait; +use League\Container\Container as DrushContainer; -final class SshCommands extends DrushCommands implements SiteAliasManagerAwareInterface +final class SshCommands extends DrushCommands { - use SiteAliasManagerAwareTrait; - const SSH = 'site:ssh'; + public function __construct( + private readonly SiteAliasManagerInterface $siteAliasManager + ) { + parent::__construct(); + } + + public static function createEarly(DrushContainer $drush_container): self + { + $commandHandler = new static( + $drush_container->get('site.alias.manager'), + ); + + return $commandHandler; + } + /** * Connect to a webserver via SSH, and optionally run a shell command. */ @@ -32,7 +45,7 @@ final class SshCommands extends DrushCommands implements SiteAliasManagerAwareIn #[CLI\Topics(topics: [DocsCommands::ALIASES])] public function ssh(array $code, $options = ['cd' => self::REQ]): void { - $alias = $this->siteAliasManager()->getSelf(); + $alias = $this->siteAliasManager->getSelf(); if (empty($code)) { $code[] = 'bash'; diff --git a/src/Commands/core/StatusCommands.php b/src/Commands/core/StatusCommands.php index c89d7b31c8..79ec4e36ef 100644 --- a/src/Commands/core/StatusCommands.php +++ b/src/Commands/core/StatusCommands.php @@ -4,8 +4,11 @@ namespace Drush\Commands\core; +use Consolidation\AnnotatedCommand\CommandData; use Consolidation\AnnotatedCommand\Hooks\HookManager; +use Consolidation\OutputFormatters\Options\FormatterOptions; use Consolidation\OutputFormatters\StructuredData\PropertyList; +use Consolidation\SiteAlias\SiteAliasManagerInterface; use Drupal\Core\Site\Settings; use Drupal\Core\StreamWrapper\PrivateStream; use Drupal\Core\StreamWrapper\PublicStream; @@ -16,19 +19,29 @@ use Drush\Commands\DrushCommands; use Drush\Drush; use Drush\Sql\SqlBase; -use Consolidation\SiteAlias\SiteAliasManagerAwareInterface; -use Consolidation\SiteAlias\SiteAliasManagerAwareTrait; -use Consolidation\OutputFormatters\Options\FormatterOptions; -use Consolidation\AnnotatedCommand\CommandData; use Drush\Utils\StringUtils; +use League\Container\Container as DrushContainer; use Symfony\Component\Filesystem\Path; -final class StatusCommands extends DrushCommands implements SiteAliasManagerAwareInterface +final class StatusCommands extends DrushCommands { - use SiteAliasManagerAwareTrait; - const STATUS = 'core:status'; + public function __construct( + private readonly SiteAliasManagerInterface $siteAliasManager + ) { + parent::__construct(); + } + + public static function createEarly(DrushContainer $drush_container): self + { + $commandHandler = new static( + $drush_container->get('site.alias.manager'), + ); + + return $commandHandler; + } + /** * An overview of the environment - Drush and Drupal. */ @@ -144,10 +157,10 @@ public function getPropertyList($options): array $status_table['drush-temp'] = Path::canonicalize($this->getConfig()->tmp()); $status_table['drush-conf'] = array_map([Path::class, 'canonicalize'], $this->getConfig()->configPaths()); // List available alias files - $alias_files = $this->siteAliasManager()->listAllFilePaths(); + $alias_files = $this->siteAliasManager->listAllFilePaths(); sort($alias_files); $status_table['drush-alias-files'] = $alias_files; - $alias_searchpaths = $this->siteAliasManager()->searchLocations(); + $alias_searchpaths = $this->siteAliasManager->searchLocations(); $status_table['alias-searchpaths'] = array_map([Path::class, 'canonicalize'], $alias_searchpaths); $paths = self::pathAliases($options, $boot_manager, $boot_object); diff --git a/src/Commands/core/UpdateDBCommands.php b/src/Commands/core/UpdateDBCommands.php index 7b95deca13..9cb8bbba54 100644 --- a/src/Commands/core/UpdateDBCommands.php +++ b/src/Commands/core/UpdateDBCommands.php @@ -6,8 +6,7 @@ use Consolidation\OutputFormatters\StructuredData\RowsOfFields; use Consolidation\OutputFormatters\StructuredData\UnstructuredListData; -use Consolidation\SiteAlias\SiteAliasManagerAwareInterface; -use Consolidation\SiteAlias\SiteAliasManagerAwareTrait; +use Consolidation\SiteAlias\SiteAliasManagerInterface; use Drupal\Core\Database\Database; use Drupal\Core\Utility\Error; use Drush\Attributes as CLI; @@ -17,16 +16,30 @@ use Drush\Drush; use Drush\Exceptions\UserAbortException; use Drush\Log\SuccessInterface; +use League\Container\Container as DrushContainer; use Psr\Log\LogLevel; -final class UpdateDBCommands extends DrushCommands implements SiteAliasManagerAwareInterface +final class UpdateDBCommands extends DrushCommands { - use SiteAliasManagerAwareTrait; - const UPDATEDB = 'updatedb'; const STATUS = 'updatedb:status'; const BATCH_PROCESS = 'updatedb:batch-process'; + public function __construct( + private readonly SiteAliasManagerInterface $siteAliasManager + ) { + parent::__construct(); + } + + public static function createEarly(DrushContainer $drush_container): self + { + $commandHandler = new static( + $drush_container->get('site.alias.manager'), + ); + + return $commandHandler; + } + /** * Note - can't inject @database since a method below is static. */ @@ -59,7 +72,7 @@ public function updatedb($options = ['cache-clear' => true]): int $status_options = ['strict' => 0]; $status_options = array_merge(Drush::redispatchOptions(), $status_options); - $process = $this->processManager()->drush($this->siteAliasManager()->getSelf(), self::STATUS, [], $status_options); + $process = $this->processManager()->drush($this->siteAliasManager->getSelf(), self::STATUS, [], $status_options); $process->mustRun(); if ($output = $process->getOutput()) { // We have pending updates - let's run em. diff --git a/src/Commands/sql/SqlSyncCommands.php b/src/Commands/sql/SqlSyncCommands.php index d67227d85c..e12a0b7faa 100644 --- a/src/Commands/sql/SqlSyncCommands.php +++ b/src/Commands/sql/SqlSyncCommands.php @@ -6,25 +6,37 @@ use Consolidation\AnnotatedCommand\CommandData; use Consolidation\AnnotatedCommand\Hooks\HookManager; +use Consolidation\SiteAlias\SiteAlias; +use Consolidation\SiteAlias\SiteAliasManagerInterface; use Drush\Attributes as CLI; -use Drush\Commands\core\CoreCommands; use Drush\Commands\core\DocsCommands; use Drush\Commands\core\RsyncCommands; use Drush\Commands\core\StatusCommands; use Drush\Commands\DrushCommands; use Drush\Drush; use Drush\Exceptions\UserAbortException; -use Consolidation\SiteAlias\SiteAlias; -use Consolidation\SiteAlias\SiteAliasManagerAwareInterface; -use Consolidation\SiteAlias\SiteAliasManagerAwareTrait; +use League\Container\Container as DrushContainer; use Symfony\Component\Filesystem\Path; -final class SqlSyncCommands extends DrushCommands implements SiteAliasManagerAwareInterface +final class SqlSyncCommands extends DrushCommands { - use SiteAliasManagerAwareTrait; - const SYNC = 'sql:sync'; + public function __construct( + private readonly SiteAliasManagerInterface $siteAliasManager + ) { + parent::__construct(); + } + + public static function createEarly(DrushContainer $drush_container): self + { + $commandHandler = new static( + $drush_container->get('site.alias.manager'), + ); + + return $commandHandler; + } + /** * Copy DB data from a source site to a target site. Transfers data via rsync. */ @@ -47,9 +59,8 @@ final class SqlSyncCommands extends DrushCommands implements SiteAliasManagerAwa #[CLI\Topics(topics: [DocsCommands::ALIASES, DocsCommands::POLICY, DocsCommands::CONFIGURATION, DocsCommands::EXAMPLE_SYNC_VIA_HTTP])] public function sqlsync($source, $target, $options = ['no-dump' => false, 'no-sync' => false, 'runner' => self::REQ, 'create-db' => false, 'db-su' => self::REQ, 'db-su-pw' => self::REQ, 'target-dump' => self::REQ, 'source-dump' => self::OPT, 'extra-dump' => self::REQ]): void { - $manager = $this->siteAliasManager(); - $sourceRecord = $manager->get($source); - $targetRecord = $manager->get($target); + $sourceRecord = $this->siteAliasManager->get($source); + $targetRecord = $this->siteAliasManager->get($target); // Append --strict in case we are calling older versions of Drush. $global_options = Drush::redispatchOptions() + ['strict' => 0]; @@ -74,11 +85,10 @@ public function validate(CommandData $commandData): void $source = $commandData->input()->getArgument('source'); $target = $commandData->input()->getArgument('target'); // Get target info for confirmation prompt. - $manager = $this->siteAliasManager(); - if (!$sourceRecord = $manager->get($source)) { + if (!$sourceRecord = $this->siteAliasManager->get($source)) { throw new \Exception(dt('Error: no alias record could be found for source !source', ['!source' => $source])); } - if (!$targetRecord = $manager->get($target)) { + if (!$targetRecord = $this->siteAliasManager->get($target)) { throw new \Exception(dt('Error: no alias record could be found for target !target', ['!target' => $target])); } if (!$commandData->input()->getOption('no-dump') && !$source_db_name = $this->databaseName($sourceRecord)) { @@ -190,7 +200,7 @@ public function rsync(array $options, SiteAlias $sourceRecord, SiteAlias $target $double_dash_options['remove-source-files'] = true; } if (!$runner = $options['runner']) { - $runner = $sourceRecord->isRemote() && $targetRecord->isRemote() ? $targetRecord : $this->siteAliasManager()->getSelf(); + $runner = $sourceRecord->isRemote() && $targetRecord->isRemote() ? $targetRecord : $this->siteAliasManager->getSelf(); } if ($runner == 'source') { $runner = $sourceRecord;