function ConsoleCompilerPass::process

Same name and namespace in other branches
  1. 11.x core/lib/Drupal/Core/DependencyInjection/Compiler/ConsoleCompilerPass.php \Drupal\Core\DependencyInjection\Compiler\ConsoleCompilerPass::process()

File

core/lib/Drupal/Core/DependencyInjection/Compiler/ConsoleCompilerPass.php, line 24

Class

ConsoleCompilerPass
Compiler pass for console commands registered via #[AsCommand] attributes.

Namespace

Drupal\Core\DependencyInjection\Compiler

Code

public function process(ContainerBuilder $container) : void {
  // Get all classes with the #[AsCommand] attribute.
  foreach ($this->getClasses($container->getParameter('container.namespaces')) as $className) {
    // Don't create a service definition if this class is already a service.
    if ($container->hasDefinition($className) || $container->hasAlias($className)) {
      continue;
    }
    // Check the full class hierarchy exists, in case the discovered class
    // extends class of optional dependencies, like Drush or Drupal Console.
    $reflection = new \ReflectionClass($className);
    while ($parent = $reflection->getParentClass()) {
      if (!class_exists($parent->getName())) {
        continue 2;
      }
      $reflection = $parent;
    }
    $definition = new Definition($className);
    $definition->setAutowired(TRUE)
      ->setPublic(TRUE)
      ->setTags([
      'console.command' => [],
    ]);
    $container->setDefinition($className, $definition);
  }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.