Skip to content
This repository has been archived by the owner on Dec 5, 2022. It is now read-only.

Commit

Permalink
[ClassLoader][Routing] Fix namespace parsing on php 8.
Browse files Browse the repository at this point in the history
  • Loading branch information
derrabus committed Aug 9, 2020
1 parent e4636a4 commit 04a7e4c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
7 changes: 6 additions & 1 deletion ClassCollectionLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,11 @@ public static function fixNamespaceDeclarations($source)
$inNamespace = false;
$tokens = token_get_all($source);

$nsTokens = [T_WHITESPACE => true, 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]) || 'b"' === $token) {
Expand All @@ -230,7 +235,7 @@ public static function fixNamespaceDeclarations($source)
$rawChunk .= $token[1];

// namespace name and whitespaces
while (isset($tokens[++$i][1]) && \in_array($tokens[$i][0], [T_WHITESPACE, T_NS_SEPARATOR, T_STRING])) {
while (isset($tokens[++$i][1], $nsTokens[$tokens[$i][0]])) {
$rawChunk .= $tokens[$i][1];
}
if ('{' === $tokens[$i]) {
Expand Down
7 changes: 6 additions & 1 deletion ClassMapGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ private static function findClasses($path)
$contents = file_get_contents($path);
$tokens = token_get_all($contents);

$nsTokens = [T_STRING => true, T_NS_SEPARATOR => true];
if (\defined('T_NAME_QUALIFIED')) {
$nsTokens[T_NAME_QUALIFIED] = true;
}

$classes = [];

$namespace = '';
Expand All @@ -110,7 +115,7 @@ private static function findClasses($path)
$namespace = '';
// If there is a namespace, extract it
while (isset($tokens[++$i][1])) {
if (\in_array($tokens[$i][0], [T_STRING, T_NS_SEPARATOR])) {
if (isset($nsTokens[$tokens[$i][0]])) {
$namespace .= $tokens[$i][1];
}
}
Expand Down

0 comments on commit 04a7e4c

Please sign in to comment.