Skip to content

Commit

Permalink
SearchExtension: ignores classes that do not have autowired parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
h4kuna committed Jun 6, 2024
1 parent f9ca71d commit ed4a800
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/DI/Extensions/SearchExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ public function findClasses(\stdClass $config): array
&& (!$rejectRE || !preg_match($rejectRE, $rc->name))
&& (!$acceptParent || Arrays::some($acceptParent, fn($nm) => $rc->isSubclassOf($nm)))
&& (!$rejectParent || Arrays::every($rejectParent, fn($nm) => !$rc->isSubclassOf($nm)))
&& (self::hasAutowireArguments($rc->getConstructor()))
) {
$found[] = $rc->name;
}
Expand Down Expand Up @@ -156,4 +157,18 @@ private static function buildNameRegexp(array $masks): ?string

return $res ? '#^(' . implode('|', $res) . ')$#i' : null;
}


private static function hasAutowireArguments(?\ReflectionMethod $constructor): bool
{
if ($constructor !== null) {
try {
Nette\DI\Resolver::autowireArguments($constructor, [], static fn() => new \stdClass());
} catch (Nette\DI\ServiceCreationException) {
return false;
}
}

return true;
}
}

0 comments on commit ed4a800

Please sign in to comment.