diff --git a/src/RobotLoader/RobotLoader.php b/src/RobotLoader/RobotLoader.php index d214e07..7409d53 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; @@ -229,7 +230,7 @@ private function refreshClasses(): void $classes[$file] = []; // prevents the error when adding the same file twice foreach ($foundClasses as $class) { - if (isset($this->classes[$class])) { + if (isset($this->classes[$class]) && $this->classes[$class][0] !== $file) { throw new Nette\InvalidStateException(sprintf( 'Ambiguous class %s resolution; defined in %s and in %s.', $class, @@ -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..0f8d38f 100644 --- a/tests/Loaders/files/conditional.class.php +++ b/tests/Loaders/files/conditional.class.php @@ -6,4 +6,8 @@ class ConditionalClass { } +} else { + class ConditionalClass + { + } }