Skip to content

Commit 4decca0

Browse files
authored
Minimize Drush dependencies in cli and jn:get commands (#6531)
1 parent 398ef6e commit 4decca0

File tree

2 files changed

+23
-53
lines changed

2 files changed

+23
-53
lines changed

src/Commands/core/CliCommand.php

Lines changed: 18 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,36 @@
99
use Drupal\Component\DependencyInjection\Container;
1010
use Drupal\Component\Render\MarkupInterface;
1111
use Drupal\Core\Config\ConfigBase;
12+
use Drupal\Core\Config\ConfigFactoryInterface;
1213
use Drupal\Core\Config\Entity\ConfigEntityInterface;
14+
use Drupal\Core\DrupalKernelInterface;
1315
use Drupal\Core\Entity\ContentEntityInterface;
1416
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
1517
use Drupal\Core\Entity\EntityTypeManagerInterface;
1618
use Drupal\Core\Entity\EntityTypeRepositoryInterface;
1719
use Drupal\Core\Field\FieldItemInterface;
1820
use Drupal\Core\Field\FieldItemListInterface;
21+
use Drupal\Core\Render\HtmlResponse;
1922
use Drush\Attributes as CLI;
2023
use Drush\Command\HelpLinks;
2124
use Drush\Commands\AutowireTrait;
22-
use Drush\Config\DrushConfig;
2325
use Drush\Drush;
2426
use Drush\Formatters\FormatterTrait;
2527
use Drush\Psysh\Caster;
2628
use Drush\Psysh\DrushCommand;
2729
use Drush\Psysh\DrushHelpCommand;
2830
use Drush\Psysh\Shell;
2931
use Drush\Runtime\Runtime;
30-
use Drush\Utils\FsUtils;
3132
use Psy\Configuration;
3233
use Psy\VersionUpdater\Checker;
3334
use Symfony\Component\Console\Attribute\AsCommand;
3435
use Symfony\Component\Console\Command\Command;
3536
use Symfony\Component\Console\Input\InputInterface;
3637
use Symfony\Component\Console\Input\InputOption;
3738
use Symfony\Component\Console\Output\OutputInterface;
39+
use Symfony\Component\DependencyInjection\Attribute\Autowire;
40+
use Symfony\Component\HttpFoundation\RequestStack;
41+
use Symfony\Component\HttpKernel\TerminableInterface;
3842

3943
#[AsCommand(
4044
name: self::NAME,
@@ -51,19 +55,21 @@ final class CliCommand extends Command
5155
public const string NAME = 'php:cli';
5256

5357
public function __construct(
58+
protected readonly ConfigFactoryInterface $configFactory,
5459
protected readonly EntityTypeManagerInterface $entityTypeManager,
5560
protected readonly EntityTypeRepositoryInterface $entityTypeRepository,
5661
protected readonly EntityTypeBundleInfoInterface $entityTypeBundleInfo,
5762
protected readonly FormatterManager $formatterManager,
58-
protected readonly DrushConfig $drushConfig,
63+
#[Autowire(service: 'kernel')]
64+
protected readonly DrupalKernelInterface $kernel,
65+
protected readonly RequestStack $requestStack,
5966
) {
6067
parent::__construct();
6168
}
6269

6370
protected function configure(): void
6471
{
6572
$this
66-
->addOption('version-history', null, InputOption::VALUE_NONE, 'Use command history based on Drupal version. Default is per site.')
6773
->addOption('cwd', null, InputOption::VALUE_REQUIRED, 'A directory to change to before launching the shell. Default is the project root directory')
6874
->addUsage('$node = Node::load(1)')
6975
->addUsage('$node = NodeArticle::load(1)')
@@ -76,12 +82,12 @@ public function execute(InputInterface $input, OutputInterface $output): int
7682
$configuration = new Configuration();
7783

7884
// Set the Drush specific history file path.
79-
$configuration->setHistoryFile($this->historyPath($input));
85+
$configuration->setHistoryFile('private://' . self::NAME . '.history');
8086

8187
$configuration->setStartupMessage(
8288
sprintf(
8389
'<aside>%s (Drupal %s)</aside>',
84-
\Drupal::config('system.site')->get('name'),
90+
$this->configFactory->get('system.site')->get('name'),
8591
\Drupal::VERSION
8692
)
8793
);
@@ -112,12 +118,10 @@ public function execute(InputInterface $input, OutputInterface $output): int
112118
// database connection when they are killed. So when we return back to the
113119
// parent process after, there is no connection. This will be called after the
114120
// command in preflight still, but the subscriber instances are already
115-
// created from before. Call terminate() regardless, this is a no-op for all
116-
// DrupalBoot classes except DrupalBoot8.
117-
// @phpstan-ignore if.alwaysTrue
118-
if ($bootstrap = Drush::bootstrap()) {
119-
$bootstrap->terminate();
120-
}
121+
// created from before.
122+
$response = new HtmlResponse();
123+
assert($this->kernel instanceof TerminableInterface);
124+
$this->kernel->terminate($this->requestStack->getCurrentRequest(), $response);
121125

122126
// If the cwd option is passed, lets change the current working directory to wherever
123127
// the user wants to go before we launch psysh.
@@ -142,10 +146,10 @@ private function getDrushCommands(): array
142146
self::NAME,
143147
'php:cli',
144148
'php',
145-
PhpCommands::EVAL,
149+
PhpEvalCommand::NAME,
146150
'eval',
147151
'ev',
148-
PhpCommands::SCRIPT,
152+
PhpScriptCommand::NAME,
149153
'scr',
150154
];
151155
$php_keywords = $this->getPhpKeywords();
@@ -190,42 +194,6 @@ private function getCasters(): array
190194
];
191195
}
192196

193-
/**
194-
* Returns the file path for the CLI history.
195-
*
196-
* This can either be site-specific (default) or Drupal version specific.
197-
*/
198-
private function historyPath(InputInterface $input): string
199-
{
200-
$cli_directory = FsUtils::getBackupDirParent();
201-
$drupal_major_version = Drush::getMajorVersion();
202-
203-
// If there is no drupal version (and thus no root). Just use the current
204-
// path.
205-
// @todo Could use a global file within drush?
206-
if (!$drupal_major_version) {
207-
$file_name = 'global-' . md5($this->drushConfig->cwd());
208-
} elseif ($input->getOption('version-history')) {
209-
// If only the Drupal version is being used for the history.
210-
$file_name = "drupal-$drupal_major_version";
211-
} else {
212-
// If there is an alias, use that in the site specific name. Otherwise,
213-
// use a hash of the root path.
214-
$aliasRecord = Drush::aliasManager()->getSelf();
215-
216-
if ($aliasRecord->name()) {
217-
$site_suffix = ltrim($aliasRecord->name(), '@');
218-
} else {
219-
$drupal_root = Drush::bootstrapManager()->getRoot();
220-
$site_suffix = md5($drupal_root);
221-
}
222-
223-
$file_name = "drupal-site-$site_suffix";
224-
}
225-
226-
return "$cli_directory/$file_name";
227-
}
228-
229197
/**
230198
* Returns a list of PHP keywords.
231199
*

src/Commands/core/JsonapiGetCommand.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@
66

77
use Consolidation\OutputFormatters\FormatterManager;
88
use Consolidation\OutputFormatters\StructuredData\UnstructuredData;
9+
use Drupal\Core\DrupalKernelInterface;
910
use Drush\Attributes as CLI;
1011
use Drush\Commands\AutowireTrait;
11-
use Drush\Drush;
1212
use Drush\Formatters\FormatterTrait;
1313
use Symfony\Component\Console\Attribute\AsCommand;
1414
use Symfony\Component\Console\Command\Command;
1515
use Symfony\Component\Console\Input\InputArgument;
1616
use Symfony\Component\Console\Input\InputInterface;
1717
use Symfony\Component\Console\Output\OutputInterface;
18+
use Symfony\Component\DependencyInjection\Attribute\Autowire;
1819
use Symfony\Component\HttpFoundation\Request;
1920
use Symfony\Component\HttpKernel\HttpKernelInterface;
2021

@@ -34,6 +35,8 @@ final class JsonapiGetCommand extends Command
3435

3536
public function __construct(
3637
protected readonly FormatterManager $formatterManager,
38+
#[Autowire(service: 'kernel')]
39+
protected readonly DrupalKernelInterface $kernel,
3740
) {
3841
parent::__construct();
3942
}
@@ -57,9 +60,8 @@ public function execute(InputInterface $input, OutputInterface $output): int
5760

5861
public function doExecute(string $url): UnstructuredData
5962
{
60-
$kernel = Drush::bootstrap()->getKernel();
6163
$sub_request = Request::create($url, 'GET');
62-
$subResponse = $kernel->handle($sub_request, HttpKernelInterface::SUB_REQUEST);
64+
$subResponse = $this->kernel->handle($sub_request, HttpKernelInterface::SUB_REQUEST);
6365
return new UnstructuredData(json_decode($subResponse->getContent()));
6466
}
6567
}

0 commit comments

Comments
 (0)