99use Drupal \Component \DependencyInjection \Container ;
1010use Drupal \Component \Render \MarkupInterface ;
1111use Drupal \Core \Config \ConfigBase ;
12+ use Drupal \Core \Config \ConfigFactoryInterface ;
1213use Drupal \Core \Config \Entity \ConfigEntityInterface ;
14+ use Drupal \Core \DrupalKernelInterface ;
1315use Drupal \Core \Entity \ContentEntityInterface ;
1416use Drupal \Core \Entity \EntityTypeBundleInfoInterface ;
1517use Drupal \Core \Entity \EntityTypeManagerInterface ;
1618use Drupal \Core \Entity \EntityTypeRepositoryInterface ;
1719use Drupal \Core \Field \FieldItemInterface ;
1820use Drupal \Core \Field \FieldItemListInterface ;
21+ use Drupal \Core \Render \HtmlResponse ;
1922use Drush \Attributes as CLI ;
2023use Drush \Command \HelpLinks ;
2124use Drush \Commands \AutowireTrait ;
22- use Drush \Config \DrushConfig ;
2325use Drush \Drush ;
2426use Drush \Formatters \FormatterTrait ;
2527use Drush \Psysh \Caster ;
2628use Drush \Psysh \DrushCommand ;
2729use Drush \Psysh \DrushHelpCommand ;
2830use Drush \Psysh \Shell ;
2931use Drush \Runtime \Runtime ;
30- use Drush \Utils \FsUtils ;
3132use Psy \Configuration ;
3233use Psy \VersionUpdater \Checker ;
3334use Symfony \Component \Console \Attribute \AsCommand ;
3435use Symfony \Component \Console \Command \Command ;
3536use Symfony \Component \Console \Input \InputInterface ;
3637use Symfony \Component \Console \Input \InputOption ;
3738use 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 *
0 commit comments