4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpKernel\DependencyInjection;
14 use Composer\Autoload\ClassLoader;
15 use Symfony\Component\Debug\DebugClassLoader;
16 use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
17 use Symfony\Component\DependencyInjection\ContainerBuilder;
18 use Symfony\Component\HttpKernel\Kernel;
21 * Sets the classes to compile in the cache for the container.
23 * @author Fabien Potencier <fabien@symfony.com>
25 class AddAnnotatedClassesToCachePass implements CompilerPassInterface
29 public function __construct(Kernel $kernel)
31 $this->kernel = $kernel;
37 public function process(ContainerBuilder $container)
40 $annotatedClasses = array();
41 foreach ($container->getExtensions() as $extension) {
42 if ($extension instanceof Extension) {
43 if (\PHP_VERSION_ID < 70000) {
44 $classes = array_merge($classes, $extension->getClassesToCompile());
46 $annotatedClasses = array_merge($annotatedClasses, $extension->getAnnotatedClassesToCompile());
50 $existingClasses = $this->getClassesInComposerClassMaps();
52 if (\PHP_VERSION_ID < 70000) {
53 $classes = $container->getParameterBag()->resolveValue($classes);
54 $this->kernel->setClassCache($this->expandClasses($classes, $existingClasses));
56 $annotatedClasses = $container->getParameterBag()->resolveValue($annotatedClasses);
57 $this->kernel->setAnnotatedClassCache($this->expandClasses($annotatedClasses, $existingClasses));
61 * Expands the given class patterns using a list of existing classes.
63 * @param array $patterns The class patterns to expand
64 * @param array $classes The existing classes to match against the patterns
66 * @return array A list of classes derivated from the patterns
68 private function expandClasses(array $patterns, array $classes)
72 // Explicit classes declared in the patterns are returned directly
73 foreach ($patterns as $key => $pattern) {
74 if ('\\' !== substr($pattern, -1) && false === strpos($pattern, '*')) {
75 unset($patterns[$key]);
76 $expanded[] = ltrim($pattern, '\\');
80 // Match patterns with the classes list
81 $regexps = $this->patternsToRegexps($patterns);
83 foreach ($classes as $class) {
84 $class = ltrim($class, '\\');
86 if ($this->matchAnyRegexps($class, $regexps)) {
91 return array_unique($expanded);
94 private function getClassesInComposerClassMaps()
98 foreach (spl_autoload_functions() as $function) {
99 if (!\is_array($function)) {
103 if ($function[0] instanceof DebugClassLoader) {
104 $function = $function[0]->getClassLoader();
107 if (\is_array($function) && $function[0] instanceof ClassLoader) {
108 $classes += array_filter($function[0]->getClassMap());
112 return array_keys($classes);
115 private function patternsToRegexps($patterns)
119 foreach ($patterns as $pattern) {
121 $regex = preg_quote(ltrim($pattern, '\\'));
123 // Wildcards * and **
124 $regex = strtr($regex, array('\\*\\*' => '.*?', '\\*' => '[^\\\\]*?'));
126 // If this class does not end by a slash, anchor the end
127 if ('\\' !== substr($regex, -1)) {
131 $regexps[] = '{^\\\\'.$regex.'}';
137 private function matchAnyRegexps($class, $regexps)
139 $blacklisted = false !== strpos($class, 'Test');
141 foreach ($regexps as $regex) {
142 if ($blacklisted && false === strpos($regex, 'Test')) {
146 if (preg_match($regex, '\\'.$class)) {