You are here

public static function TestDiscovery::scanDirectory in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/simpletest/src/TestDiscovery.php \Drupal\simpletest\TestDiscovery::scanDirectory()

Scans a given directory for class files.

@todo Limit to '*Test.php' files (~10% less files to reflect/introspect).

Parameters

string $namespace_prefix: The namespace prefix to use for discovered classes. Must contain a trailing namespace separator (backslash). For example: 'Drupal\\node\\Tests\\'

string $path: The directory path to scan. For example: '/path/to/drupal/core/modules/node/tests/src'

Return value

array An associative array whose keys are fully-qualified class names and whose values are corresponding filesystem pathnames.

Throws

\InvalidArgumentException If $namespace_prefix does not end in a namespace separator (backslash).

See also

https://www.drupal.org/node/2296635

1 call to TestDiscovery::scanDirectory()
TestDiscovery::findAllClassFiles in core/modules/simpletest/src/TestDiscovery.php
Discovers all class files in all available extensions.

File

core/modules/simpletest/src/TestDiscovery.php, line 254
Contains \Drupal\simpletest\TestDiscovery.

Class

TestDiscovery
Discovers available tests.

Namespace

Drupal\simpletest

Code

public static function scanDirectory($namespace_prefix, $path) {
  if (substr($namespace_prefix, -1) !== '\\') {
    throw new \InvalidArgumentException("Namespace prefix for {$path} must contain a trailing namespace separator.");
  }
  $flags = \FilesystemIterator::UNIX_PATHS;
  $flags |= \FilesystemIterator::SKIP_DOTS;
  $flags |= \FilesystemIterator::FOLLOW_SYMLINKS;
  $flags |= \FilesystemIterator::CURRENT_AS_SELF;
  $iterator = new \RecursiveDirectoryIterator($path, $flags);
  $filter = new \RecursiveCallbackFilterIterator($iterator, function ($current, $key, $iterator) {
    if ($iterator
      ->hasChildren()) {
      return TRUE;
    }
    return $current
      ->isFile() && $current
      ->getExtension() === 'php';
  });
  $files = new \RecursiveIteratorIterator($filter);
  $classes = array();
  foreach ($files as $fileinfo) {
    $class = $namespace_prefix;
    if ('' !== ($subpath = $fileinfo
      ->getSubPath())) {
      $class .= strtr($subpath, '/', '\\') . '\\';
    }
    $class .= $fileinfo
      ->getBasename('.php');
    $classes[$class] = $fileinfo
      ->getPathname();
  }
  return $classes;
}