Parcourir la source

added a way for any extension to add classes to the class cache

Fabien Potencier il y a 14 ans
Parent
commit
b7d2528384

+ 42 - 0
src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/AddClassesToCachePass.php

@@ -0,0 +1,42 @@
+<?php
+
+namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler;
+
+use Symfony\Component\DependencyInjection\ContainerBuilder;
+use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
+
+/*
+ * This file is part of the Symfony framework.
+ *
+ * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
+ *
+ * This source file is subject to the MIT license that is bundled
+ * with this source code in the file LICENSE.
+ */
+
+/**
+ * Sets the classes to compile in the cache for the container.
+ *
+ * @author Fabien Potencier <fabien.potencier@symfony-project.com>
+ */
+class AddClassesToCachePass implements CompilerPassInterface
+{
+    /**
+     * {@inheritDoc}
+     */
+    public function process(ContainerBuilder $container)
+    {
+        $classes = array();
+        foreach ($container->getExtensionConfigs() as $name => $configs) {
+            list($namespace, $tag) = explode(':', $name);
+
+            $extension = $container->getExtension($namespace);
+
+            if (method_exists($extension, 'getClassesToCompile')) {
+                $classes = array_merge($classes, $extension->getClassesToCompile());
+            }
+        }
+
+        $container->setParameter('kernel.compiled_classes', array_unique($classes));
+    }
+}

+ 5 - 17
src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

@@ -2,7 +2,6 @@
 
 namespace Symfony\Bundle\FrameworkBundle\DependencyInjection;
 
-use Symfony\Component\DependencyInjection\Extension\Extension;
 use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
 use Symfony\Component\DependencyInjection\Resource\FileResource;
 use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -11,6 +10,7 @@ use Symfony\Component\DependencyInjection\Definition;
 use Symfony\Component\DependencyInjection\Parameter;
 use Symfony\Component\Finder\Finder;
 use Symfony\Component\HttpFoundation\RequestMatcher;
+use Symfony\Component\HttpKernel\DependencyInjection\Extension;
 
 /*
  * This file is part of the Symfony framework.
@@ -120,7 +120,7 @@ class FrameworkExtension extends Extension
 
         $this->registerTranslatorConfiguration($config, $container);
 
-        $this->addCompiledClasses($container, array(
+        $this->addClassesToCompile(array(
             'Symfony\\Component\\HttpFoundation\\ParameterBag',
             'Symfony\\Component\\HttpFoundation\\HeaderBag',
             'Symfony\\Component\\HttpFoundation\\Request',
@@ -230,7 +230,7 @@ class FrameworkExtension extends Extension
         }
 
         // compilation
-        $this->addCompiledClasses($container, array(
+        $this->addClassesToCompile(array(
             'Symfony\\Component\\Templating\\DelegatingEngine',
             'Symfony\\Bundle\\FrameworkBundle\\Templating\\EngineInterface',
         ));
@@ -361,7 +361,7 @@ class FrameworkExtension extends Extension
         }
         $container->setParameter('session.storage.'.strtolower($config['storage_id']).'.options', $options);
 
-        $this->addCompiledClasses($container, array(
+        $this->addClassesToCompile(array(
             'Symfony\\Component\\HttpFoundation\\Session',
             'Symfony\\Component\\HttpFoundation\\SessionStorage\\SessionStorageInterface',
         ));
@@ -382,7 +382,7 @@ class FrameworkExtension extends Extension
 
         $container->setParameter('routing.resource', $config['router']['resource']);
 
-        $this->addCompiledClasses($container, array(
+        $this->addClassesToCompile(array(
             'Symfony\\Component\\Routing\\RouterInterface',
             'Symfony\\Component\\Routing\\Router',
             'Symfony\\Component\\Routing\\Matcher\\UrlMatcherInterface',
@@ -525,18 +525,6 @@ class FrameworkExtension extends Extension
         }
     }
 
-    /**
-     * Add class to be compiled when debug mode is not activated
-     *
-     * @param ContainerBuilder $container A ContainerBuilder instance
-     * @param array            $classes   Classes to be compiled
-     */
-    protected function addCompiledClasses(ContainerBuilder $container, array $classes)
-    {
-        $current = $container->hasParameter('kernel.compiled_classes') ? $container->getParameter('kernel.compiled_classes') : array();
-        $container->setParameter('kernel.compiled_classes', array_merge($current, $classes));
-    }
-
     /**
      * Returns the base path for the XSD files.
      *

+ 3 - 0
src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php

@@ -9,7 +9,9 @@ use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddSecurityVoter
 use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ConverterManagerPass;
 use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\RoutingResolverPass;
 use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ProfilerPass;
+use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddClassesToCachePass;
 use Symfony\Component\DependencyInjection\ContainerBuilder;
+use Symfony\Component\DependencyInjection\Compiler\PassConfig;
 use Symfony\Component\HttpKernel\Bundle\Bundle;
 use Symfony\Component\Form\FormConfiguration;
 
@@ -69,5 +71,6 @@ class FrameworkBundle extends Bundle
         $container->addCompilerPass(new RegisterKernelListenersPass());
         $container->addCompilerPass(new TemplatingPass());
         $container->addCompilerPass(new AddConstraintValidatorsPass());
+        $container->addCompilerPass(new AddClassesToCachePass());
     }
 }

+ 16 - 2
src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php

@@ -2,7 +2,7 @@
 
 namespace Symfony\Bundle\TwigBundle\DependencyInjection;
 
-use Symfony\Component\DependencyInjection\Extension\Extension;
+use Symfony\Component\HttpKernel\DependencyInjection\Extension;
 use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
 use Symfony\Component\DependencyInjection\ContainerBuilder;
 use Symfony\Component\DependencyInjection\Reference;
@@ -34,6 +34,20 @@ class TwigExtension extends Extension
         if (!$container->hasDefinition('twig')) {
             $loader = new XmlFileLoader($container, __DIR__.'/../Resources/config');
             $loader->load('twig.xml');
+
+            $this->addClassesToCompile(array(
+                'Twig_Environment',
+                'Twig_ExtensionInterface',
+                'Twig_Extension',
+                'Twig_Extension_Core',
+                'Twig_Extension_Escaper',
+                'Twig_Extension_Optimizer',
+                'Twig_LoaderInterface',
+                'Twig_Loader_Filesystem',
+                'Twig_Markup',
+                'Twig_TemplateInterface',
+                'Twig_Template',
+            ));
         }
 
         // form resources
@@ -87,7 +101,7 @@ class TwigExtension extends Extension
             if (false !== strpos($key, '-')) {
                 unset($config[$key]);
                 $config[str_replace('-', '_', $key)] = $value;
-            }            
+            }
         }
 
         $container->setParameter('twig.options', array_replace($container->getParameter('twig.options'), $config));

+ 0 - 1
src/Symfony/Component/DependencyInjection/Compiler/MergeExtensionConfigurationPass.php

@@ -43,7 +43,6 @@ class MergeExtensionConfigurationPass implements CompilerPassInterface
             $container->merge($tmpContainer);
         }
 
-        $container->setExtensionConfigs(array());
         $container->addDefinitions($definitions);
         $container->addAliases($aliases);
         $container->getParameterBag()->add($parameters);

+ 2 - 0
src/Symfony/Component/DependencyInjection/ContainerBuilder.php

@@ -352,6 +352,8 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
     {
         $this->compiler->compile($this);
 
+        $this->setExtensionConfigs(array());
+
         parent::compile();
     }
 

+ 40 - 0
src/Symfony/Component/HttpKernel/DependencyInjection/Extension.php

@@ -0,0 +1,40 @@
+<?php
+
+namespace Symfony\Component\HttpKernel\DependencyInjection;
+
+use Symfony\Component\DependencyInjection\Extension\Extension as BaseExtension;
+use Symfony\Component\DependencyInjection\ContainerBuilder;
+
+/*
+ * This file is part of the Symfony framework.
+ *
+ * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
+ *
+ * This source file is subject to the MIT license that is bundled
+ * with this source code in the file LICENSE.
+ */
+
+/**
+ * Provides useful features shared by many extensions.
+ *
+ * @author Fabien Potencier <fabien.potencier@symfony-project.com>
+ */
+abstract class Extension extends BaseExtension
+{
+    protected $classes = array();
+
+    public function getClassesToCompile()
+    {
+        return $this->classes;
+    }
+
+    /**
+     * Adds classes to be compiled when debug mode is not enabled.
+     *
+     * @param array $classes Classes to be compiled
+     */
+    protected function addClassesToCompile(array $classes)
+    {
+        $this->classes = array_merge($this->classes, $classes);
+    }
+}