Skip to content

Commit

Permalink
More Di in commandfiles. (#5566)
Browse files Browse the repository at this point in the history
* More Di in commandfiles.

* DI for SiteInstallCommands

* Call parent constructor

* Revert si changes
  • Loading branch information
weitzman committed May 15, 2023
1 parent eb53975 commit 0b14016
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/Commands/core/CacheCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public function __construct(
private $pluginCacheClearer,
private BootstrapManager $bootstrapManager
) {
parent::__construct();
}

public static function create(ContainerInterface $container, $drush_container): self
Expand All @@ -57,7 +58,6 @@ public static function create(ContainerInterface $container, $drush_container):
$container->get('asset.js.collection_optimizer'),
$container->get('asset.css.collection_optimizer'),
$container->get('plugin.cache_clearer'),
// Note: pulling from Drush container, not Drupal.
$drush_container->get('bootstrap.manager')
);

Expand Down
12 changes: 5 additions & 7 deletions src/Commands/core/EditCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,17 @@ public function load($headers = true): array
}
}

if (Drush::bootstrapManager()->hasBootstrapped(DrupalBootLevels::FULL)) {
$site_root = \Drupal::service('kernel')->getSitePath();
$bootstrapManager = Drush::bootstrapManager();
if ($bootstrapManager->hasBootstrapped(DrupalBootLevels::FULL)) {
$boot = $bootstrapManager->bootstrap();
$site_root = $boot->getKernel()->getSitePath();
$path = realpath($site_root . '/settings.php');
$drupal[$path] = $path;
if (file_exists($site_root . '/settings.local.php')) {
$path = realpath($site_root . '/settings.local.php');
$drupal[$path] = $path;
}
if ($path = realpath(DRUPAL_ROOT . '/.htaccess')) {
if ($path = realpath($bootstrapManager->getRoot() . '/.htaccess')) {
$drupal[$path] = $path;
}
if ($headers) {
Expand All @@ -133,10 +135,6 @@ public function bashFiles(): array
if ($bashrc = self::findBashrc($home)) {
$bashFiles[$bashrc] = $bashrc;
}
$prompt = $home . '/.drush/drush.prompt.sh';
if (file_exists($prompt)) {
$bashFiles[$prompt] = $prompt;
}
return $bashFiles;
}

Expand Down
29 changes: 26 additions & 3 deletions src/Commands/core/LoginCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,22 @@

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 Drupal\user\Entity\User;
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
{
Expand All @@ -21,6 +28,22 @@ final class LoginCommands extends DrushCommands implements SiteAliasManagerAware

const LOGIN = 'user:login';

public function __construct(protected TimeInterface $time, protected LanguageManagerInterface $languageManager, private BootstrapManager $bootstrapManager)
{
parent::__construct();
}

public static function create(ContainerInterface $container, $drush_container): self
{
$commandHandler = new static(
$container->get('datetime.time'),
$container->get('language_manager'),
$drush_container->get('bootstrap.manager')
);

return $commandHandler;
}

/**
* Display a one time login link for user ID 1, or another user.
*/
Expand All @@ -47,7 +70,7 @@ public function login(string $path = '', $options = ['name' => null, 'uid' => nu
$process->mustRun();
$link = $process->getOutput();
} else {
if (!Drush::bootstrapManager()->doBootstrap(DrupalBootLevels::FULL)) {
if (!$this->bootstrapManager->doBootstrap(DrupalBootLevels::FULL)) {
throw new \Exception(dt('Unable to bootstrap Drupal.'));
}

Expand All @@ -72,7 +95,7 @@ public function login(string $path = '', $options = ['name' => null, 'uid' => nu
throw new \InvalidArgumentException(dt('Account !name is blocked and thus cannot login. The user:unblock command may be helpful.', ['!name' => $account->getAccountName()]));
}

$timestamp = \Drupal::time()->getRequestTime();
$timestamp = $this->time->getRequestTime();
$link = Url::fromRoute(
'user.reset.login',
[
Expand All @@ -83,7 +106,7 @@ public function login(string $path = '', $options = ['name' => null, 'uid' => nu
[
'absolute' => true,
'query' => $path ? ['destination' => $path] : [],
'language' => \Drupal::languageManager()->getLanguage($account->getPreferredLangcode()),
'language' => $this->languageManager->getLanguage($account->getPreferredLangcode()),
]
)->toString();
}
Expand Down

0 comments on commit 0b14016

Please sign in to comment.