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

Breadcrumb

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

function AttributeFileLoader::findClass

Returns the full class name for the first class in the file.

2 calls to AttributeFileLoader::findClass()
AttributeDirectoryLoader::load in vendor/symfony/routing/Loader/AttributeDirectoryLoader.php
AttributeFileLoader::load in vendor/symfony/routing/Loader/AttributeFileLoader.php
Loads from attributes from a file.

File

vendor/symfony/routing/Loader/AttributeFileLoader.php, line 72

Class

AttributeFileLoader
AttributeFileLoader loads routing information from attributes set on a PHP class and its methods.

Namespace

Symfony\Component\Routing\Loader

Code

protected function findClass(string $file) : string|false {
    $class = false;
    $namespace = false;
    $tokens = token_get_all(file_get_contents($file));
    if (1 === \count($tokens) && \T_INLINE_HTML === $tokens[0][0]) {
        throw new \InvalidArgumentException(\sprintf('The file "%s" does not contain PHP code. Did you forget to add the "<?php" start tag at the beginning of the file?', $file));
    }
    $nsTokens = [
        \T_NS_SEPARATOR => true,
        \T_STRING => true,
    ];
    if (\defined('T_NAME_QUALIFIED')) {
        $nsTokens[\T_NAME_QUALIFIED] = true;
    }
    for ($i = 0; isset($tokens[$i]); ++$i) {
        $token = $tokens[$i];
        if (!isset($token[1])) {
            continue;
        }
        if (true === $class && \T_STRING === $token[0]) {
            return $namespace . '\\' . $token[1];
        }
        if (true === $namespace && isset($nsTokens[$token[0]])) {
            $namespace = $token[1];
            while (isset($tokens[++$i][1], $nsTokens[$tokens[$i][0]])) {
                $namespace .= $tokens[$i][1];
            }
            $token = $tokens[$i];
        }
        if (\T_CLASS === $token[0]) {
            // Skip usage of ::class constant and anonymous classes
            $skipClassToken = false;
            for ($j = $i - 1; $j > 0; --$j) {
                if (!isset($tokens[$j][1])) {
                    if ('(' === $tokens[$j] || ',' === $tokens[$j]) {
                        $skipClassToken = true;
                    }
                    break;
                }
                if (\T_DOUBLE_COLON === $tokens[$j][0] || \T_NEW === $tokens[$j][0]) {
                    $skipClassToken = true;
                    break;
                }
                elseif (!\in_array($tokens[$j][0], [
                    \T_WHITESPACE,
                    \T_DOC_COMMENT,
                    \T_COMMENT,
                ])) {
                    break;
                }
            }
            if (!$skipClassToken) {
                $class = true;
            }
        }
        if (\T_NAMESPACE === $token[0]) {
            $namespace = true;
        }
    }
    return false;
}

API Navigation

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