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\ContainerBuilder;
17 use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
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 AddClassesToCachePass 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 $classes = array_merge($classes, $extension->getClassesToCompile());
44 $annotatedClasses = array_merge($annotatedClasses, $extension->getAnnotatedClassesToCompile());
48 $classes = $container->getParameterBag()->resolveValue($classes);
49 $annotatedClasses = $container->getParameterBag()->resolveValue($annotatedClasses);
50 $existingClasses = $this->getClassesInComposerClassMaps();
52 $this->kernel->setClassCache($this->expandClasses($classes, $existingClasses));
53 $this->kernel->setAnnotatedClassCache($this->expandClasses($annotatedClasses, $existingClasses));
57 * Expands the given class patterns using a list of existing classes.
59 * @param array $patterns The class patterns to expand
60 * @param array $classes The existing classes to match against the patterns
62 * @return array A list of classes derivated from the patterns
64 private function expandClasses(array $patterns, array $classes)
68 // Explicit classes declared in the patterns are returned directly
69 foreach ($patterns as $key => $pattern) {
70 if (substr($pattern, -1) !== '\\' && false === strpos($pattern, '*')) {
71 unset($patterns[$key]);
72 $expanded[] = ltrim($pattern, '\\');
76 // Match patterns with the classes list
77 $regexps = $this->patternsToRegexps($patterns);
79 foreach ($classes as $class) {
80 $class = ltrim($class, '\\');
82 if ($this->matchAnyRegexps($class, $regexps)) {
87 return array_unique($expanded);
90 private function getClassesInComposerClassMaps()
94 foreach (spl_autoload_functions() as $function) {
95 if (!is_array($function)) {
99 if ($function[0] instanceof DebugClassLoader) {
100 $function = $function[0]->getClassLoader();
103 if (is_array($function) && $function[0] instanceof ClassLoader) {
104 $classes += array_filter($function[0]->getClassMap());
108 return array_keys($classes);
111 private function patternsToRegexps($patterns)
115 foreach ($patterns as $pattern) {
117 $regex = preg_quote(ltrim($pattern, '\\'));
119 // Wildcards * and **
120 $regex = strtr($regex, array('\\*\\*' => '.*?', '\\*' => '[^\\\\]*?'));
122 // If this class does not end by a slash, anchor the end
123 if (substr($regex, -1) !== '\\') {
127 $regexps[] = '{^\\\\'.$regex.'}';
133 private function matchAnyRegexps($class, $regexps)
135 $blacklisted = false !== strpos($class, 'Test');
137 foreach ($regexps as $regex) {
138 if ($blacklisted && false === strpos($regex, 'Test')) {
142 if (preg_match($regex, '\\'.$class)) {