Selaa lähdekoodia

[FrameworkBundle] converted the special routing resolver to a DIC compiler pass

Fabien Potencier 14 vuotta sitten
vanhempi
commit
385ad72d64

+ 37 - 0
src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/RoutingResolverPass.php

@@ -0,0 +1,37 @@
+<?php
+
+namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler;
+
+use Symfony\Component\DependencyInjection\Reference;
+use Symfony\Component\DependencyInjection\ContainerBuilder;
+use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+/**
+ * Adds tagged routing.loader services to routing.resolver service
+ *
+ * @author Fabien Potencier <fabien.potencier@symfony-project.com>
+ */
+class RoutingResolverPass implements CompilerPassInterface
+{
+    public function process(ContainerBuilder $container)
+    {
+        if (false === $container->hasDefinition('routing.resolver')) {
+            return;
+        }
+
+        $definition = $container->getDefinition('routing.resolver');
+
+        foreach ($container->findTaggedServiceIds('routing.loader') as $id => $attributes) {
+            $definition->addMethodCall('addLoader', array(new Reference($id)));
+        }
+    }
+}

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

@@ -4,6 +4,7 @@ namespace Symfony\Bundle\FrameworkBundle;
 
 use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddSecurityVotersPass;
 use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ConverterManagerPass;
+use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\RoutingResolverPass;
 use Symfony\Component\DependencyInjection\ContainerBuilder;
 use Symfony\Component\HttpKernel\Bundle\Bundle;
 use Symfony\Component\Form\FormConfiguration;
@@ -44,5 +45,6 @@ class FrameworkBundle extends Bundle
 
         $container->addCompilerPass(new AddSecurityVotersPass());
         $container->addCompilerPass(new ConverterManagerPass());
+        $container->addCompilerPass(new RoutingResolverPass());
     }
 }

+ 2 - 4
src/Symfony/Bundle/FrameworkBundle/Resources/config/routing.xml

@@ -7,7 +7,7 @@
     <parameters>
         <parameter key="router.class">Symfony\Component\Routing\Router</parameter>
         <parameter key="routing.loader.class">Symfony\Bundle\FrameworkBundle\Routing\DelegatingLoader</parameter>
-        <parameter key="routing.resolver.class">Symfony\Bundle\FrameworkBundle\Routing\LoaderResolver</parameter>
+        <parameter key="routing.resolver.class">Symfony\Component\Routing\Loader\LoaderResolver</parameter>
         <parameter key="routing.loader.xml.class">Symfony\Component\Routing\Loader\XmlFileLoader</parameter>
         <parameter key="routing.loader.yml.class">Symfony\Component\Routing\Loader\YamlFileLoader</parameter>
         <parameter key="routing.loader.php.class">Symfony\Component\Routing\Loader\PhpFileLoader</parameter>
@@ -20,9 +20,7 @@
     </parameters>
 
     <services>
-        <service id="routing.resolver" class="%routing.resolver.class%">
-            <argument type="service" id="service_container" />
-        </service>
+        <service id="routing.resolver" class="%routing.resolver.class%" />
 
         <service id="routing.loader.xml" class="%routing.loader.xml.class%">
             <tag name="routing.loader" />

+ 0 - 65
src/Symfony/Bundle/FrameworkBundle/Routing/LoaderResolver.php

@@ -1,65 +0,0 @@
-<?php
-
-namespace Symfony\Bundle\FrameworkBundle\Routing;
-
-use Symfony\Component\Routing\Loader\LoaderResolver as BaseLoaderResolver;
-use Symfony\Component\DependencyInjection\ContainerInterface;
-use Symfony\Component\Routing\Loader\LoaderInterface;
-
-/*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
-
-/**
- * This loader resolver automatically registers routing loaders from
- * the container.
- *
- * If also lazy-loads them.
- *
- * @author Fabien Potencier <fabien.potencier@symfony-project.com>
- */
-class LoaderResolver extends BaseLoaderResolver
-{
-    protected $services;
-    protected $container;
-
-    /**
-     * Constructor.
-     *
-     * @param ContainerInterface $container A ContainerInterface instance
-     * @param array              $loaders   An array of LoaderInterface instances to add
-     */
-    public function __construct(ContainerInterface $container, array $loaders = array())
-    {
-        parent::__construct($loaders);
-
-        $this->container = $container;
-        foreach ($container->findTaggedServiceIds('routing.loader') as $id => $attributes) {
-            $this->services[] = $id;
-        }
-    }
-
-    /**
-     * Returns a loader able to load the resource.
-     *
-     * @param mixed  $resource A resource
-     * @param string $type     The resource type
-     *
-     * @return LoaderInterface A LoaderInterface instance
-     */
-    public function resolve($resource, $type = null)
-    {
-        if (count($this->services)) {
-            while ($id = array_shift($this->services)) {
-                $this->addLoader($this->container->get($id));
-            }
-        }
-
-        return parent::resolve($resource, $type);
-    }
-}