diff --git a/src/Application.php b/src/Application.php index 78fce26ec..6bafcd963 100644 --- a/src/Application.php +++ b/src/Application.php @@ -59,110 +59,46 @@ public function getLongVersion() */ public function doRun(InputInterface $input, OutputInterface $output) { - $this->registerGenerators(); - $this->registerCommands(); - $clear = $this->container->get('console.configuration_manager') - ->getConfiguration() - ->get('application.clear')?:false; - if ($clear === true || $clear === 'true') { - $output->write(sprintf("\033\143")); - } + $this->validateCommands(); - $exitCode = parent::doRun($input, $output); - return $exitCode; + return parent::doRun($input, $output); } - private function registerGenerators() + public function validateCommands() { - if ($this->container->hasParameter('drupal.generators')) { - $consoleGenerators = $this->container->getParameter( - 'drupal.generators' - ); - } else { - $consoleGenerators = array_keys( - $this->container->findTaggedServiceIds('drupal.generator') - ); - } - - foreach ($consoleGenerators as $name) { - if (!$this->container->has($name)) { - continue; - } - - try { - $generator = $this->container->get($name); - } catch (\Exception $e) { - echo $name . ' - ' . $e->getMessage() . PHP_EOL; - - continue; - } - - if (!$generator) { - continue; - } + $consoleCommands = $this->container + ->findTaggedServiceIds('drupal.command'); - if (method_exists($generator, 'setRenderer')) { - $generator->setRenderer( - $this->container->get('console.renderer') - ); - } - - if (method_exists($generator, 'setFileQueue')) { - $generator->setFileQueue( - $this->container->get('console.file_queue') - ); - } - - if (method_exists($generator, 'setCountCodeLines')) { - $generator->setCountCodeLines( - $this->container->get('console.count_code_lines') - ); - } + if (!$consoleCommands) { + return; } - } - private function registerCommands() - { - if ($this->container->hasParameter('drupal.commands')) { - $consoleCommands = $this->container->getParameter( - 'drupal.commands' - ); - } else { - $consoleCommands = array_keys( - $this->container->findTaggedServiceIds('drupal.command') - ); - $this->container->setParameter( - 'console.warning', - 'application.site.errors.settings' - ); + if (!$this->container->hasParameter('console.service_definitions')) { + return; } - $serviceDefinitions = []; - $annotationValidator = null; - $annotationCommandReader = null; - if ($this->container->hasParameter('console.service_definitions')) { - $serviceDefinitions = $this->container - ->getParameter('console.service_definitions'); - - /** - * @var DrupalCommandAnnotationReader $annotationCommandReader - */ - $annotationCommandReader = $this->container - ->get('console.annotation_command_reader'); - - /** - * @var AnnotationValidator $annotationValidator - */ - $annotationValidator = $this->container - ->get('console.annotation_validator'); + $serviceDefinitions = $this->container + ->getParameter('console.service_definitions'); + + if (!$serviceDefinitions) { + return; } - $aliases = $this->container->get('console.configuration_manager') - ->getConfiguration() - ->get('application.commands.aliases')?:[]; + /** + * @var DrupalCommandAnnotationReader $annotationCommandReader + */ + $annotationCommandReader = $this->container + ->get('console.annotation_command_reader'); - foreach ($consoleCommands as $name) { + /** + * @var AnnotationValidator $annotationValidator + */ + $annotationValidator = $this->container + ->get('console.annotation_validator'); + $invalidCommands = []; + + foreach ($consoleCommands as $name => $tags) { AnnotationRegistry::reset(); AnnotationRegistry::registerLoader( [ @@ -172,66 +108,40 @@ private function registerCommands() ); if (!$this->container->has($name)) { + $invalidCommands[] = $name; continue; } - if ($annotationValidator && $annotationCommandReader) { - if (!$serviceDefinition = $serviceDefinitions[$name]) { - continue; - } - - if (!$annotationValidator->isValidCommand($serviceDefinition->getClass())) { - continue; - } - - $annotation = $annotationCommandReader - ->readAnnotation($serviceDefinition->getClass()); - if ($annotation) { - $this->container->get('console.translator_manager') - ->addResourceTranslationsByExtension( - $annotation['extension'], - $annotation['extensionType'] - ); - } - } - - try { - $command = $this->container->get($name); - } catch (\Exception $e) { - echo $name . ' - ' . $e->getMessage() . PHP_EOL; - + if (!$serviceDefinition = $serviceDefinitions[$name]) { + $invalidCommands[] = $name; continue; } - if (!$command) { + if (!$annotationValidator->isValidCommand( + $serviceDefinition->getClass() + ) + ) { + $invalidCommands[] = $name; continue; } - if (method_exists($command, 'setTranslator')) { - $command->setTranslator( - $this->container->get('console.translator_manager') - ); - } - - if (method_exists($command, 'setContainer')) { - $command->setContainer( - $this->container->get('service_container') - ); + $annotation = $annotationCommandReader + ->readAnnotation($serviceDefinition->getClass()); + if ($annotation) { + $this->container->get('console.translator_manager') + ->addResourceTranslationsByExtension( + $annotation['extension'], + $annotation['extensionType'] + ); } + } - if (array_key_exists($command->getName(), $aliases)) { - $commandAliases = array_unique(array_merge( - $command->getAliases()?$command->getAliases():[], - array_key_exists($command->getName(), $aliases)?$aliases[$command->getName()]:[] - )); - if (!is_array($commandAliases)) { - $commandAliases = [$commandAliases]; - } - $command->setAliases($commandAliases); - } + $this->container->set( + 'console.invalid_commands', + $invalidCommands + ); - $this->add($command); - } + return; } public function getData() @@ -399,11 +309,4 @@ private function commandData($commandName) return $data; } - - public function setContainer($container) - { - $this->container = $container; - $this->registerGenerators(); - $this->registerCommands(); - } } diff --git a/src/Bootstrap/AddServicesCompilerPass.php b/src/Bootstrap/AddServicesCompilerPass.php index 9cffe28dd..efd16bdb7 100644 --- a/src/Bootstrap/AddServicesCompilerPass.php +++ b/src/Bootstrap/AddServicesCompilerPass.php @@ -38,7 +38,7 @@ class AddServicesCompilerPass implements CompilerPassInterface /** * AddCommandsCompilerPass constructor. * - * @param string $root + * @param string $root */ public function __construct($root) { @@ -91,6 +91,12 @@ public function process(ContainerBuilder $container) 'console.service_definitions', $container->getDefinitions() ); + + // Set console.invalid_commands service + $container->set( + 'console.invalid_commands', + null + ); } protected function addDrupalConsoleServiceFiles($servicesFiles) diff --git a/src/Bootstrap/Drupal.php b/src/Bootstrap/Drupal.php index c039f5b88..317849ade 100644 --- a/src/Bootstrap/Drupal.php +++ b/src/Bootstrap/Drupal.php @@ -6,14 +6,16 @@ use Symfony\Component\Console\Input\ArrayInput; use Symfony\Component\Console\Output\ConsoleOutput; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\DependencyInjection\Compiler\PassConfig; +use Drupal\Component\FileCache\FileCacheFactory; +use Drupal\Core\Site\Settings; use Drupal\Console\Core\Style\DrupalStyle; use Drupal\Console\Core\Utils\ArgvInputReader; use Drupal\Console\Core\Bootstrap\DrupalConsoleCore; use Drupal\Console\Core\Utils\DrupalFinder; -use Drupal\Component\FileCache\FileCacheFactory; -use Drupal\Core\Site\Settings; +use Drupal\Console\Core\Bootstrap\DrupalInterface; -class Drupal +class Drupal implements DrupalInterface { protected $autoload; @@ -74,10 +76,10 @@ public function boot() } } - $rebuildServicesFile = false; - if ($command=='cache:rebuild' || $command=='cr') { - $rebuildServicesFile = true; - } + // $rebuildServicesFile = false; + // if ($command=='cache:rebuild' || $command=='cr') { + // $rebuildServicesFile = true; + // } if ($debug) { $io->writeln('➤ Creating request'); diff --git a/src/Bootstrap/DrupalKernelTrait.php b/src/Bootstrap/DrupalKernelTrait.php index 63cbaed5b..b93174052 100644 --- a/src/Bootstrap/DrupalKernelTrait.php +++ b/src/Bootstrap/DrupalKernelTrait.php @@ -27,17 +27,7 @@ public static function createFromRequest(Request $request, $class_loader, $envir $kernel = new static($environment, $class_loader, $allow_dumping, $app_root); static::bootEnvironment($app_root); $kernel->initializeSettings($request); - // Calling the request handle causes that a page request "/" is - // processed for any console execution even: help or --version and - // with sites that have globally displayed blocks contexts are not - // ready for blocks plugins so this causes lot of problems like: - // https://github.com/hechoendrupal/drupal-console/issues/3091 and - // https://github.com/hechoendrupal/drupal-console/issues/3553 Also - // handle does a initializeContainer which originally was invalidated - // and rebuild at Console Drupal Bootstrap. By disabling handle - // and processing the boot() at Bootstrap commands that do not - // depend on requests works well. - // $kernel->handle($request); + return $kernel; } diff --git a/src/Bootstrap/DrupalServiceModifier.php b/src/Bootstrap/DrupalServiceModifier.php index 86cba73e2..155f023c3 100644 --- a/src/Bootstrap/DrupalServiceModifier.php +++ b/src/Bootstrap/DrupalServiceModifier.php @@ -7,6 +7,7 @@ /** * Class DrupalServiceModifier + * * @package Drupal\Console\Bootstrap */ class DrupalServiceModifier implements ServiceModifierInterface @@ -29,9 +30,9 @@ class DrupalServiceModifier implements ServiceModifierInterface /** * DrupalServiceModifier constructor. * - * @param string $root - * @param string $serviceTag - * @param string $generatorTag + * @param string $root + * @param string $serviceTag + * @param string $generatorTag */ public function __construct( $root = null, @@ -52,11 +53,11 @@ public function alter(ContainerBuilder $container) new AddServicesCompilerPass($this->root) ); - $container->addCompilerPass( - new FindCommandsCompilerPass($this->commandTag) - ); - $container->addCompilerPass( - new FindGeneratorsCompilerPass($this->generatorTag) - ); + // $container->addCompilerPass( + // new FindCommandsCompilerPass($this->commandTag) + // ); + // $container->addCompilerPass( + // new FindGeneratorsCompilerPass($this->generatorTag) + // ); } }