firebase / php-jwt

PHP package for JWT
BSD 3-Clause "New" or "Revised" License
9.3k stars 1.26k forks source link

Issues with composer autoloader in Symfony project #542

Closed celdaran closed 2 months ago

celdaran commented 8 months ago

I have a sample script working just fine in a plain test.php file, because I wanted to make sure it worked before I integrated it into my Symfony project. But transplanting the code is giving me this error:

Attempted to load class "CachedKeySet" from namespace "Firebase\JWT".
Did you forget a "use" statement for another namespace?

Long story short, I've resorted to step-debugging through composer's autoloader to try and find out why CachedKeySet is invisible.

These lines appear in autoload_static.php:

'Firebase\\JWT\\BeforeValidException' => __DIR__ . '/..' . '/firebase/php-jwt/src/BeforeValidException.php',
'Firebase\\JWT\\CachedKeySet' => __DIR__ . '/..' . '/firebase/php-jwt/src/CachedKeySet.php',
'Firebase\\JWT\\ExpiredException' => __DIR__ . '/..' . '/firebase/php-jwt/src/ExpiredException.php',
etc.

Similar lines in autoload_classmap.php:

'Firebase\\JWT\\BeforeValidException' => $vendorDir . '/firebase/php-jwt/src/BeforeValidException.php',
'Firebase\\JWT\\CachedKeySet' => $vendorDir . '/firebase/php-jwt/src/CachedKeySet.php',
'Firebase\\JWT\\ExpiredException' => $vendorDir . '/firebase/php-jwt/src/ExpiredException.php',
etc.

And primarily where I would expect to find it, in autoload_psr4.php:

'Firebase\\JWT\\' => array($vendorDir . '/firebase/php-jwt/src', $vendorDir . '/firebase/php-jwt/src'),

But at the point where it attempts to actually load the file, it comes up missing. In composer's ClassLoader, there's this code:

    private function findFileWithExtension($class, $ext)
    {
        // PSR-4 lookup
        $logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . $ext;

        $first = $class[0];
        if (isset($this->prefixLengthsPsr4[$first])) {
            $subPath = $class;
            while (false !== $lastPos = strrpos($subPath, '\\')) {
                $subPath = substr($subPath, 0, $lastPos);
                $search = $subPath . '\\';
                if (isset($this->prefixDirsPsr4[$search])) {
                    $pathEnd = DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $lastPos + 1);
                    foreach ($this->prefixDirsPsr4[$search] as $dir) {
                        if (file_exists($file = $dir . $pathEnd)) {
                            return $file;
                        }
                    }
                }
            }
        }

There's no element in prefixLengthsPsr4['F'] and so it bails immediately and never loads.

Any clues?

yash30201 commented 2 months ago

Hi @celdaran , sorry for the delay in coming to this issue.

Do you still face this issue?

yash30201 commented 2 months ago

Closing due to inactivity, please feel free to reopen this is issue still exists.