Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. FileLoader.php

function FileLoader::findClasses

1 call to FileLoader::findClasses()
FileLoader::registerClasses in vendor/symfony/dependency-injection/Loader/FileLoader.php
Registers a set of classes as services using PSR-4 for discovery.

File

vendor/symfony/dependency-injection/Loader/FileLoader.php, line 307

Class

FileLoader
FileLoader is the abstract class used by all built-in loaders that are file based.

Namespace

Symfony\Component\DependencyInjection\Loader

Code

private function findClasses(string $namespace, string $pattern, array $excludePatterns, ?RegisterAutoconfigureAttributesPass $autoconfigureAttributes, ?string $source) : array {
    $parameterBag = $this->container
        ->getParameterBag();
    $excludePaths = [];
    $excludePrefix = null;
    $excludePatterns = $parameterBag->unescapeValue($parameterBag->resolveValue($excludePatterns));
    foreach ($excludePatterns as $excludePattern) {
        foreach ($this->glob($excludePattern, true, $resource, true, true) as $path => $info) {
            $excludePrefix ??= $resource->getPrefix();
            // normalize Windows slashes and remove trailing slashes
            $excludePaths[rtrim(str_replace('\\', '/', $path), '/')] = true;
        }
    }
    $pattern = $parameterBag->unescapeValue($parameterBag->resolveValue($pattern));
    $classes = [];
    $prefixLen = null;
    foreach ($this->glob($pattern, true, $resource, false, false, $excludePaths) as $path => $info) {
        if (null === $prefixLen) {
            $prefixLen = \strlen($resource->getPrefix());
            if ($excludePrefix && !str_starts_with($excludePrefix, $resource->getPrefix())) {
                throw new InvalidArgumentException(\sprintf('Invalid "exclude" pattern when importing classes for "%s": make sure your "exclude" pattern (%s) is a subset of the "resource" pattern (%s).', $namespace, $excludePattern, $pattern));
            }
        }
        if (isset($excludePaths[str_replace('\\', '/', $path)])) {
            continue;
        }
        if (!str_ends_with($path, '.php')) {
            continue;
        }
        $class = $namespace . ltrim(str_replace('/', '\\', substr($path, $prefixLen, -4)), '\\');
        if (!preg_match('/^[a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*+(?:\\\\[a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*+)*+$/', $class)) {
            continue;
        }
        try {
            $r = $this->container
                ->getReflectionClass($class);
        } catch (\ReflectionException $e) {
            $classes[$class] = $e->getMessage();
            continue;
        }
        // check to make sure the expected class exists
        if (!$r) {
            throw new InvalidArgumentException(\sprintf('Expected to find class "%s" in file "%s" while importing services from resource "%s", but it was not found! Check the namespace prefix used with the resource.', $class, $path, $pattern));
        }
        if ($r->isInstantiable() || $r->isInterface()) {
            $classes[$class] = null;
        }
        if ($autoconfigureAttributes && !$r->isInstantiable()) {
            $autoconfigureAttributes->processClass($this->container, $r);
        }
    }
    // track only for new & removed files
    if ($resource instanceof GlobResource) {
        $this->container
            ->addResource($resource);
    }
    else {
        foreach ($resource as $path) {
            $this->container
                ->fileExists($path, false);
        }
    }
    if (null !== $prefixLen) {
        foreach ($excludePaths as $path => $_) {
            $class = $namespace . ltrim(str_replace('/', '\\', substr($path, $prefixLen, str_ends_with($path, '.php') ? -4 : null)), '\\');
            $this->addContainerExcludedTag($class, $source);
        }
    }
    return $classes;
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal