Skip to content

Commit

Permalink
added $topLevelOnly
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Nov 6, 2023
1 parent 9ac9cd8 commit 3ccc6fd
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/RobotLoader/RobotLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class RobotLoader

/** @var string[] */
public array $acceptFiles = ['*.php'];
public bool $topLevelOnly = true;
private bool $autoRebuild = true;
private bool $reportParseErrors = true;

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}

Expand Down
22 changes: 22 additions & 0 deletions tests/Loaders/RobotLoader.topLevel.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/**
* Test: Nette\Loaders\RobotLoader top level.
*/

declare(strict_types=1);

use Nette\Loaders\RobotLoader;
use Tester\Assert;


require __DIR__ . '/../bootstrap.php';


$loader = new RobotLoader;
$loader->setTempDirectory(getTempDir());
$loader->topLevelOnly = false;
$loader->addDirectory(__DIR__ . '/files');
$loader->register();

Assert::true(class_exists('ConditionalClass')); // files/conditional.class.php
4 changes: 4 additions & 0 deletions tests/Loaders/files/conditional.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@
class ConditionalClass
{
}
} else {
class ConditionalClass
{
}
}

0 comments on commit 3ccc6fd

Please sign in to comment.