class AddAnnotatedClassesToCachePass
Sets the classes to compile in the cache for the container.
@author Fabien Potencier <fabien@symfony.com>
Hierarchy
- class \Symfony\Component\HttpKernel\DependencyInjection\AddAnnotatedClassesToCachePass implements \Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface
Expanded class hierarchy of AddAnnotatedClassesToCachePass
Deprecated
since Symfony 7.1, to be removed in 8.0
File
-
vendor/
symfony/ http-kernel/ DependencyInjection/ AddAnnotatedClassesToCachePass.php, line 29
Namespace
Symfony\Component\HttpKernel\DependencyInjectionView source
class AddAnnotatedClassesToCachePass implements CompilerPassInterface {
public function __construct(Kernel $kernel) {
}
public function process(ContainerBuilder $container) : void {
$annotatedClasses = [];
foreach ($container->getExtensions() as $extension) {
if ($extension instanceof Extension) {
$annotatedClasses[] = $extension->getAnnotatedClassesToCompile();
}
}
$annotatedClasses = array_merge($this->kernel
->getAnnotatedClassesToCompile(), ...$annotatedClasses);
$existingClasses = $this->getClassesInComposerClassMaps();
$annotatedClasses = $container->getParameterBag()
->resolveValue($annotatedClasses);
$this->kernel
->setAnnotatedClassCache($this->expandClasses($annotatedClasses, $existingClasses));
}
/**
* Expands the given class patterns using a list of existing classes.
*
* @param array $patterns The class patterns to expand
* @param array $classes The existing classes to match against the patterns
*/
private function expandClasses(array $patterns, array $classes) : array {
$expanded = [];
// Explicit classes declared in the patterns are returned directly
foreach ($patterns as $key => $pattern) {
if (!str_ends_with($pattern, '\\') && !str_contains($pattern, '*')) {
unset($patterns[$key]);
$expanded[] = ltrim($pattern, '\\');
}
}
// Match patterns with the classes list
$regexps = $this->patternsToRegexps($patterns);
foreach ($classes as $class) {
$class = ltrim($class, '\\');
if ($this->matchAnyRegexps($class, $regexps)) {
$expanded[] = $class;
}
}
return array_unique($expanded);
}
private function getClassesInComposerClassMaps() : array {
$classes = [];
foreach (spl_autoload_functions() as $function) {
if (!\is_array($function)) {
continue;
}
if ($function[0] instanceof DebugClassLoader) {
$function = $function[0]->getClassLoader();
}
if (\is_array($function) && $function[0] instanceof ClassLoader) {
$classes += array_filter($function[0]->getClassMap());
}
}
return array_keys($classes);
}
private function patternsToRegexps(array $patterns) : array {
$regexps = [];
foreach ($patterns as $pattern) {
// Escape user input
$regex = preg_quote(ltrim($pattern, '\\'));
// Wildcards * and **
$regex = strtr($regex, [
'\\*\\*' => '.*?',
'\\*' => '[^\\\\]*?',
]);
// If this class does not end by a slash, anchor the end
if (!str_ends_with($regex, '\\')) {
$regex .= '$';
}
$regexps[] = '{^\\\\' . $regex . '}';
}
return $regexps;
}
private function matchAnyRegexps(string $class, array $regexps) : bool {
$isTest = str_contains($class, 'Test');
foreach ($regexps as $regex) {
if ($isTest && !str_contains($regex, 'Test')) {
continue;
}
if (preg_match($regex, '\\' . $class)) {
return true;
}
}
return false;
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title |
---|---|---|---|---|
AddAnnotatedClassesToCachePass::expandClasses | private | function | Expands the given class patterns using a list of existing classes. | |
AddAnnotatedClassesToCachePass::getClassesInComposerClassMaps | private | function | ||
AddAnnotatedClassesToCachePass::matchAnyRegexps | private | function | ||
AddAnnotatedClassesToCachePass::patternsToRegexps | private | function | ||
AddAnnotatedClassesToCachePass::process | public | function | You can modify the container here before it is dumped to PHP code. | Overrides CompilerPassInterface::process |
AddAnnotatedClassesToCachePass::__construct | public | function |