diff --git a/src/RobotLoader/RobotLoader.php b/src/RobotLoader/RobotLoader.php index d214e07..3bdce2b 100644 --- a/src/RobotLoader/RobotLoader.php +++ b/src/RobotLoader/RobotLoader.php @@ -34,6 +34,7 @@ class RobotLoader /** @var string[] */ public array $acceptFiles = ['*.php']; + public bool $topLevelOnly = true; private bool $autoRebuild = true; private bool $reportParseErrors = true; @@ -361,7 +362,7 @@ private function scanPhp(string $file): array $namespace = $name ? $name . '\\' : ''; $minLevel = $token->text === '{' ? 1 : 0; - } elseif ($name && $level === $minLevel) { + } elseif ($name && ($level === $minLevel || !$this->topLevelOnly)) { $classes[] = $namespace . $name; } diff --git a/tests/Loaders/RobotLoader.topLevel.phpt b/tests/Loaders/RobotLoader.topLevel.phpt new file mode 100644 index 0000000..93d2bd3 --- /dev/null +++ b/tests/Loaders/RobotLoader.topLevel.phpt @@ -0,0 +1,22 @@ +setTempDirectory(getTempDir()); +$loader->topLevelOnly = false; +$loader->addDirectory(__DIR__ . '/files'); +$loader->register(); + +Assert::true(class_exists('ConditionalClass')); // files/conditional.class.php diff --git a/tests/Loaders/files/conditional.class.php b/tests/Loaders/files/conditional.class.php index 60c2e5e..a3bc4e6 100644 --- a/tests/Loaders/files/conditional.class.php +++ b/tests/Loaders/files/conditional.class.php @@ -2,7 +2,7 @@ declare(strict_types=1); -if (false) { +if (true) { class ConditionalClass { }