You are here

function autoload_seek_classes in Autoload 7

Seek classes by base of a namespace.

Parameters

string $namespace_base: Namespace base.

string|null $subtype: Return only subclasses of particular subtype.

bool $include_file_info: Whether file info should be returned.

Return value

string[]|\stdClass[] List of namespaces of classes or objects, representing file information.

2 calls to autoload_seek_classes()
AuxiliaryTest::assertClasses in src/Tests/Unit/AuxiliaryTest.php
Assert sought classes.
SeekerTest::test in src/Tests/Unit/SeekerTest.php
Unit tests for auxiliary functions.

File

./autoload.module, line 159

Code

function autoload_seek_classes($namespace_base, $subtype = NULL, $include_file_info = FALSE) {
  static $cache;
  if (NULL === $cache) {
    $cache = cache_get("{$namespace_base}:{$subtype}");
  }
  if (FALSE === $cache || empty($cache->data)) {
    $paths = autoload_paths();
    $list = array();
    if (isset($paths[$namespace_base])) {
      $mask = implode('|', array_map('preg_quote', autoload_extensions()));
      foreach ($paths[$namespace_base] as $path => $data) {
        foreach (file_scan_directory($path, "/({$mask})\$/") as $uri => $file) {
          $blockers = array(
            $path,
            '.' . pathinfo($uri, PATHINFO_EXTENSION),
          );
          $class = explode('/', trim(str_replace($blockers, '', $uri), '/'));
          if ('' !== $data['psr-4']) {

            // Trim trailing slash which is typical to PSR-4.
            array_unshift($class, rtrim($data['psr-4'], '/'));
          }
          elseif ($class[0] !== $namespace_base) {
            array_unshift($class, $namespace_base);
          }
          $class = autoload_path_to_namespace(implode('/', $class));
          if (class_exists($class)) {
            $file->path = $path;
            $file->module_name = $data['name'];
            $file->module_path = $data['path'];
            $list[$class] = $include_file_info ? $file : $class;
          }
        }
      }
      if (isset($subtype) && (class_exists($subtype) || interface_exists($subtype))) {
        foreach ($list as $class => $item) {
          if (!is_subclass_of($class, $subtype)) {
            unset($list[$class]);
          }
        }
      }
    }
    if (!empty($list)) {
      cache_set("{$namespace_base}:{$subtype}", $list);
    }
    return $list;
  }
  return $cache->data;
}