Selaa lähdekoodia

removed BundleInterface::buildContainer() method (extensions are now automatically registered -- or override the getExtensions() method if you do not follow the conventions)

Fabien Potencier 15 vuotta sitten
vanhempi
commit
9e82497d5b

+ 0 - 11
src/Symfony/Bundle/DoctrineBundle/DoctrineBundle.php

@@ -24,15 +24,4 @@ use Symfony\Bundle\DoctrineBundle\DependencyInjection\DoctrineExtension;
  */
 class DoctrineBundle extends Bundle
 {
-    /**
-     * Customizes the Container instance.
-     *
-     * @param ParameterBagInterface $parameterBag A ParameterBagInterface instance
-     *
-     * @return ContainerBuilder A ContainerBuilder instance
-     */
-    public function buildContainer(ParameterBagInterface $parameterBag)
-    {
-        ContainerBuilder::registerExtension(new DoctrineExtension());
-    }
 }

+ 0 - 11
src/Symfony/Bundle/DoctrineMongoDBBundle/DoctrineMongoDBBundle.php

@@ -16,15 +16,4 @@ use Symfony\Bundle\DoctrineMongoDBBundle\DependencyInjection\DoctrineMongoDBExte
  */
 class DoctrineMongoDBBundle extends Bundle 
 {
-    /**
-     * Customizes the Container instance.
-     *
-     * @param ParameterBagInterface $parameterBag A ParameterBagInterface instance
-     *
-     * @return ContainerBuilder A ContainerBuilder instance
-     */
-    public function buildContainer(ParameterBagInterface $parameterBag)
-    {
-        ContainerBuilder::registerExtension(new DoctrineMongoDBExtension());
-    }
 }

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

@@ -24,15 +24,4 @@ use Symfony\Bundle\FrameworkBundle\DependencyInjection\WebExtension;
  */
 class FrameworkBundle extends Bundle
 {
-    /**
-     * Customizes the Container instance.
-     *
-     * @param ParameterBagInterface $parameterBag A ParameterBagInterface instance
-     *
-     * @return ContainerBuilder A ContainerBuilder instance
-     */
-    public function buildContainer(ParameterBagInterface $parameterBag)
-    {
-        ContainerBuilder::registerExtension(new WebExtension($parameterBag->get('kernel.bundle_dirs'), $parameterBag->get('kernel.bundles')));
-    }
 }

+ 0 - 12
src/Symfony/Bundle/PropelBundle/PropelBundle.php

@@ -9,18 +9,6 @@ use Symfony\Bundle\PropelBundle\DependencyInjection\PropelExtension;
 
 class PropelBundle extends Bundle
 {
-    /**
-     * Customizes the Container instance.
-     *
-     * @param ParameterBagInterface $parameterBag A ParameterBagInterface instance
-     *
-     * @return ContainerBuilder A ContainerBuilder instance
-     */
-    public function buildContainer(ParameterBagInterface $parameterBag)
-    {
-        ContainerBuilder::registerExtension(new PropelExtension());
-    }
-
     public function boot(ContainerInterface $container)
     {
         require_once $container->getParameter('propel.path').'/runtime/lib/Propel.php';

+ 0 - 11
src/Symfony/Bundle/SwiftmailerBundle/SwiftmailerBundle.php

@@ -23,15 +23,4 @@ use Symfony\Bundle\SwiftmailerBundle\DependencyInjection\SwiftmailerExtension;
  */
 class SwiftmailerBundle extends Bundle
 {
-    /**
-     * Customizes the Container instance.
-     *
-     * @param ParameterBagInterface $parameterBag A ParameterBagInterface instance
-     *
-     * @return ContainerBuilder A ContainerBuilder instance
-     */
-    public function buildContainer(ParameterBagInterface $parameterBag)
-    {
-        ContainerBuilder::registerExtension(new SwiftmailerExtension());
-    }
 }

+ 0 - 11
src/Symfony/Bundle/TwigBundle/TwigBundle.php

@@ -23,15 +23,4 @@ use Symfony\Bundle\TwigBundle\DependencyInjection\TwigExtension;
  */
 class TwigBundle extends Bundle
 {
-    /**
-     * Customizes the Container instance.
-     *
-     * @param ParameterBagInterface $parameterBag A ParameterBagInterface instance
-     *
-     * @return ContainerBuilder A ContainerBuilder instance
-     */
-    public function buildContainer(ParameterBagInterface $parameterBag)
-    {
-        ContainerBuilder::registerExtension(new TwigExtension());
-    }
 }

+ 0 - 11
src/Symfony/Bundle/ZendBundle/ZendBundle.php

@@ -24,15 +24,4 @@ use Symfony\Bundle\ZendBundle\DependencyInjection\ZendExtension;
  */
 class ZendBundle extends Bundle
 {
-    /**
-     * Customizes the Container instance.
-     *
-     * @param ParameterBagInterface $parameterBag A ParameterBagInterface instance
-     *
-     * @return ContainerBuilder A ContainerBuilder instance
-     */
-    public function buildContainer(ParameterBagInterface $parameterBag)
-    {
-        ContainerBuilder::registerExtension(new ZendExtension());
-    }
 }

+ 30 - 12
src/Symfony/Framework/Bundle/Bundle.php

@@ -29,17 +29,6 @@ abstract class Bundle implements BundleInterface
     protected $path;
     protected $reflection;
 
-    /**
-     * Customizes the Container instance.
-     *
-     * @param ParameterBagInterface $parameterBag A ParameterBagInterface instance
-     *
-     * @return ContainerBuilder A ContainerBuilder instance
-     */
-    public function buildContainer(ParameterBagInterface $parameterBag)
-    {
-    }
-
     /**
      * Boots the Bundle.
      *
@@ -115,7 +104,36 @@ abstract class Bundle implements BundleInterface
     }
 
     /**
-     * Finds and registers commands for the current bundle.
+     * Finds and registers Dependency Injection Container extensions.
+     *
+     * Override this method if your DIC extensions do not follow the conventions:
+     *
+     * * Extensions are in the 'DependencyInjection/' sub-directory
+     * * Extension class names ends with 'Extension'
+     *
+     * @param ContainerBuilder A ContainerBuilder instance
+     */
+    public function registerExtensions(ContainerBuilder $container)
+    {
+        if (!$dir = realpath($this->getPath().'/DependencyInjection')) {
+            return array();
+        }
+
+        $finder = new Finder();
+        $finder->files()->name('*Extension.php')->in($dir);
+
+        $prefix = $this->namespacePrefix.'\\'.$this->name.'\\DependencyInjection';
+        foreach ($finder as $file) {
+            $class = $prefix.strtr($file->getPath(), array($dir => '', '/' => '\\')).'\\'.basename($file, '.php');
+
+            if ('Extension' === substr($class, -9)) {
+                $container->registerExtension(new $class());
+            }
+        }
+    }
+
+    /**
+     * Finds and registers Commands.
      *
      * @param Application $application An Application instance
      */

+ 0 - 9
src/Symfony/Framework/Bundle/BundleInterface.php

@@ -22,15 +22,6 @@ use Symfony\Components\DependencyInjection\ParameterBag\ParameterBagInterface;
  */
 interface BundleInterface
 {
-    /**
-     * Customizes the Container instance.
-     *
-     * @param ParameterBagInterface $parameterBag A ParameterBagInterface instance
-     *
-     * @return ContainerBuilder A ContainerBuilder instance
-     */
-    public function buildContainer(ParameterBagInterface $parameterBag);
-
     /**
      * Boots the Bundle.
      *

+ 1 - 3
src/Symfony/Framework/Kernel.php

@@ -366,9 +366,7 @@ abstract class Kernel implements HttpKernelInterface, \Serializable
 
         $container = new ContainerBuilder($parameterBag);
         foreach ($this->bundles as $bundle) {
-            if (null !== $c = $bundle->buildContainer($parameterBag)) {
-                $container->merge($c);
-            }
+            $bundle->registerExtensions($container);
 
             if ($this->debug) {
                 $container->addObjectResource($bundle);

+ 0 - 12
src/Symfony/Framework/KernelBundle.php

@@ -27,18 +27,6 @@ use Symfony\Components\DependencyInjection\ContainerBuilder;
  */
 class KernelBundle extends Bundle
 {
-    /**
-     * Customizes the Container instance.
-     *
-     * @param ParameterBagInterface $parameterBag A ParameterBagInterface instance
-     *
-     * @return ContainerBuilder A ContainerBuilder instance
-     */
-    public function buildContainer(ParameterBagInterface $parameterBag)
-    {
-        ContainerBuilder::registerExtension(new KernelExtension());
-    }
-
     /**
      * Boots the Bundle.
      *

+ 20 - 14
src/Symfony/Framework/bootstrap.php

@@ -19,11 +19,6 @@ abstract class Bundle implements BundleInterface
     protected $reflection;
 
     
-    public function buildContainer(ParameterBagInterface $parameterBag)
-    {
-    }
-
-    
     public function boot(ContainerInterface $container)
     {
     }
@@ -74,6 +69,26 @@ abstract class Bundle implements BundleInterface
     }
 
     
+    public function registerExtensions(ContainerBuilder $container)
+    {
+        if (!$dir = realpath($this->getPath().'/DependencyInjection')) {
+            return array();
+        }
+
+        $finder = new Finder();
+        $finder->files()->name('*Extension.php')->in($dir);
+
+        $prefix = $this->namespacePrefix.'\\'.$this->name.'\\DependencyInjection';
+        foreach ($finder as $file) {
+            $class = $prefix.strtr($file->getPath(), array($dir => '', '/' => '\\')).'\\'.basename($file, '.php');
+
+            if ('Extension' === substr($class, -9)) {
+                $container->registerExtension(new $class());
+            }
+        }
+    }
+
+    
     public function registerCommands(Application $application)
     {
         if (!$dir = realpath($this->getPath().'/Command')) {
@@ -115,9 +130,6 @@ use Symfony\Components\DependencyInjection\ParameterBag\ParameterBagInterface;
 interface BundleInterface
 {
     
-    public function buildContainer(ParameterBagInterface $parameterBag);
-
-    
     public function boot(ContainerInterface $container);
 
     
@@ -142,12 +154,6 @@ use Symfony\Components\DependencyInjection\ContainerBuilder;
 class KernelBundle extends Bundle
 {
     
-    public function buildContainer(ParameterBagInterface $parameterBag)
-    {
-        ContainerBuilder::registerExtension(new KernelExtension());
-    }
-
-    
     public function boot(ContainerInterface $container)
     {
         if ($container->has('error_handler')) {