Browse Source

Allow to add extensions on multiple target

tyx 12 years ago
parent
commit
d70cf68b39

+ 18 - 13
DependencyInjection/Compiler/ExtensionCompilerPass.php

@@ -27,23 +27,28 @@ class ExtensionCompilerPass implements CompilerPassInterface
     public function process(ContainerBuilder $container)
     {
         $universalExtensions = array();
-        foreach ($container->findTaggedServiceIds('sonata.admin.extension') as $id => $attributes) {
 
-            $target = false;
-            if (isset($attributes[0]['target'])) {
-                $target = $attributes[0]['target'];
-            }
+        foreach ($container->findTaggedServiceIds('sonata.admin.extension') as $id => $tags) {
+            foreach ($tags as $attributes) {
+                $target = false;
 
-            if (isset($attributes[0]['global']) && $attributes[0]['global']) {
-                $universalExtensions[] = $id;
-            }
+                if (isset($attributes['target'])) {
+                    $target = $attributes['target'];
+                }
 
-            if (!$target || !$container->hasDefinition($target)) {
-                continue;
-            }
+                if (isset($attributes['global']) && $attributes['global']) {
+                    $universalExtensions[] = $id;
+                }
+
+                if (!$target || !$container->hasDefinition($target)) {
+                    continue;
+                }
 
-            $container->getDefinition($target)
-                ->addMethodCall('addExtension', array(new Reference($id)));
+                $container
+                    ->getDefinition($target)
+                    ->addMethodCall('addExtension', array(new Reference($id)))
+                ;
+            }
         }
 
         $extensionConfig = $container->getParameter('sonata.admin.extension.map');

+ 2 - 1
Resources/doc/reference/extensions.rst

@@ -30,7 +30,7 @@ Configuration
 There are two ways to configure your extensions and connect them to an admin.
 
 You can include this information in the service definition of your extension.
-Add the tag *sonata.admin.extension* and use the *target* attribute to point to the admin you want to modify.
+Add the tag *sonata.admin.extension* and use the *target* attribute to point to the admin you want to modify. Please note you can specify as many tags you want.
 Set the *global* attribute to *true* and the extension will be added to all admins.
 
 .. configuration-block::
@@ -42,6 +42,7 @@ Set the *global* attribute to *true* and the extension will be added to all admi
                 class: Acme\Demo\BlogBundle\Admin\Extension\PublishStatusAdminExtension
                 tags:
                     - { name: sonata.admin.extension, target: acme.demo.admin.article }
+                    - { name: sonata.admin.extension, target: acme.demo.admin.blog }
 
             acme.demo.order.extension:
                 class: Acme\Demo\BlogBundle\Admin\Extension\OrderAdminExtension

+ 4 - 2
Tests/DependencyInjection/Compiler/ExtensionCompilerPassTest.php

@@ -236,10 +236,11 @@ class ExtensionCompilerPassTest extends \PHPUnit_Framework_TestCase
 
         $def = $container->get('sonata_article_admin');
         $extensions = $def->getExtensions();
-        $this->assertCount(3, $extensions);
+        $this->assertCount(4, $extensions);
         $this->assertInstanceOf(get_class($this->securityExtension), $extensions[0]);
         $this->assertInstanceOf(get_class($this->publishExtension), $extensions[1]);
         $this->assertInstanceOf(get_class($this->orderExtension), $extensions[2]);
+        $this->assertInstanceOf(get_class($this->filterExtension), $extensions[3]);
 
         $def = $container->get('sonata_news_admin');
         $extensions = $def->getExtensions();
@@ -343,7 +344,8 @@ class ExtensionCompilerPassTest extends \PHPUnit_Framework_TestCase
         $container
             ->register('sonata_extension_filter')
             ->setClass(get_class($this->filterExtension))
-            ->addTag('sonata.admin.extension', array('target' => 'sonata_news_admin'));
+            ->addTag('sonata.admin.extension', array('target' => 'sonata_news_admin'))
+            ->addTag('sonata.admin.extension', array('target' => 'sonata_article_admin'));
 
         return $container;
     }