function ConsoleCompilerPass::getClasses

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

Gets command classes for the provided namespaces.

Parameters

array<class-string, string> $namespaces: An array of namespaces with keys as class strings and values as paths.

Return value

\Generator<class-string> Generates class strings.

Throws

\ReflectionException

1 call to ConsoleCompilerPass::getClasses()
ConsoleCompilerPass::process in core/lib/Drupal/Core/DependencyInjection/Compiler/ConsoleCompilerPass.php

File

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

Class

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

Namespace

Drupal\Core\DependencyInjection\Compiler

Code

protected function getClasses(array $namespaces) : \Generator {
  foreach ($namespaces as $namespace => $dirs) {
    $dirs = (array) $dirs;
    foreach ($dirs as $dir) {
      $dir .= '/Command';
      if (!file_exists($dir)) {
        continue;
      }
      $namespace .= '\\Command';
      $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir, \FilesystemIterator::KEY_AS_PATHNAME | \FilesystemIterator::CURRENT_AS_FILEINFO | \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::SELF_FIRST);
      /** @var \SplFileInfo $fileinfo */
      foreach ($iterator as $fileinfo) {
        if ($fileinfo->getExtension() !== 'php') {
          continue;
        }
        /** @var \RecursiveDirectoryIterator|null $subDir */
        $subDir = $iterator->getSubIterator();
        if (NULL === $subDir) {
          continue;
        }
        $subDir = $subDir->getSubPath();
        $subDir = $subDir !== '' ? str_replace(DIRECTORY_SEPARATOR, '\\', $subDir) . '\\' : '';
        /** @var class-string $class */
        $class = $namespace . '\\' . $subDir . $fileinfo->getBasename('.php');
        try {
          $reflectionClass = new \ReflectionClass($class);
        } catch (\Error) {
          // Skip commands where the hierarchy is unresolvable due to
          // optional dependencies.
          continue;
        }
        if (count($reflectionClass->getAttributes(AsCommand::class)) > 0) {
          yield $class;
        }
      }
    }
  }
}

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