Browse Source

Merge pull request #1472 from dantleech/improved_base_route_name_generation

Improved base route name detection
Thomas 11 years ago
parent
commit
7bf161d71e
3 changed files with 93 additions and 16 deletions
  1. 11 9
      Admin/Admin.php
  2. 9 1
      CHANGELOG.md
  3. 73 6
      Tests/Admin/BaseAdminTest.php

+ 11 - 9
Admin/Admin.php

@@ -50,6 +50,8 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
     const CONTEXT_MENU       = 'menu';
     const CONTEXT_DASHBOARD  = 'dashboard';
 
+    const CLASS_REGEX        = '@([A-Za-z0-9]*)\\\(Bundle\\\)?([A-Za-z0-9]+)Bundle\\\([A-Za-z0-9].*)\\\([A-Za-z0-9]*)$@';
+
     /**
      * The class name managed by the admin class
      *
@@ -845,7 +847,7 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
     public function getBaseRoutePattern()
     {
         if (!$this->baseRoutePattern) {
-            preg_match('@([A-Za-z0-9]*)\\\([A-Za-z0-9]*)Bundle\\\(Entity|Document|Model|PHPCR)\\\(.*)@', $this->getClass(), $matches);
+            preg_match(self::CLASS_REGEX, $this->getClass(), $matches);
 
             if (!$matches) {
                 throw new \RuntimeException(sprintf('Please define a default `baseRoutePattern` value for the admin class `%s`', get_class($this)));
@@ -854,14 +856,14 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
             if ($this->isChild()) { // the admin class is a child, prefix it with the parent route name
                 $this->baseRoutePattern = sprintf('%s/{id}/%s',
                     $this->getParent()->getBaseRoutePattern(),
-                    $this->urlize($matches[4], '-')
+                    $this->urlize($matches[5], '-')
                 );
             } else {
 
                 $this->baseRoutePattern = sprintf('/%s/%s/%s',
                     $this->urlize($matches[1], '-'),
-                    $this->urlize($matches[2], '-'),
-                    $this->urlize($matches[4], '-')
+                    $this->urlize($matches[3], '-'),
+                    $this->urlize($matches[5], '-')
                 );
             }
         }
@@ -879,23 +881,23 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
     public function getBaseRouteName()
     {
         if (!$this->baseRouteName) {
-            preg_match('@([A-Za-z0-9]*)\\\([A-Za-z0-9]*)Bundle\\\(Entity|Document|Model|PHPCR)\\\(.*)@', $this->getClass(), $matches);
+            preg_match(self::CLASS_REGEX, $this->getClass(), $matches);
 
             if (!$matches) {
-                throw new \RuntimeException(sprintf('Please define a default `baseRouteName` value for the admin class `%s`', get_class($this)));
+                throw new \RuntimeException(sprintf('Cannot automatically determine base route name, please define a default `baseRouteName` value for the admin class `%s`', get_class($this)));
             }
 
             if ($this->isChild()) { // the admin class is a child, prefix it with the parent route name
                 $this->baseRouteName = sprintf('%s_%s',
                     $this->getParent()->getBaseRouteName(),
-                    $this->urlize($matches[4])
+                    $this->urlize($matches[5])
                 );
             } else {
 
                 $this->baseRouteName = sprintf('admin_%s_%s_%s',
                     $this->urlize($matches[1]),
-                    $this->urlize($matches[2]),
-                    $this->urlize($matches[4])
+                    $this->urlize($matches[3]),
+                    $this->urlize($matches[5])
                 );
             }
         }

+ 9 - 1
CHANGELOG.md

@@ -20,6 +20,14 @@ CHANGELOG
           return null !== $this->request;
       }
 
+### 2013-07-23
+
+* [BC BREAK] changed route name/pattern guesser to be more acurate and
+  persistance layer agnostic, this might affect you if you use a namespace scheme
+  similar to the examples below:
+    * **Before** - admin for `Symfony\Cmf\Bundle\FoobarBundle\Document\Bar` generated base route name  `admin_bundle_foobar_bar` and base pattern `/cmf/bundle/bar`
+    * **After** - admin for `Symfony\Cmf\Bundle\FoobarBundle\Document\Bar` generates `admin_cmf_foobar_bar` and `/cmf/foobar/bar`
+
 ### 2013-07-05
 
 *  Remove qTip
@@ -123,4 +131,4 @@ CHANGELOG
 * register routes by using the getUrls from each Admin class
 * build admin information "on demand"
 * create an EntityAdmin and add new abstract method into the Admin class
-* add inline edition for one-to-one association
+* add inline edition for one-to-one association

+ 73 - 6
Tests/Admin/BaseAdminTest.php

@@ -141,10 +141,43 @@ class BaseAdminTest extends \PHPUnit_Framework_TestCase
         $this->assertEquals('Category', $admin->getParentAssociationMapping());
     }
 
-    public function testGetBaseRoutePattern()
+    public function provideGetBaseRoutePattern()
     {
-        $admin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
-        $this->assertEquals('/sonata/news/post', $admin->getBaseRoutePattern());
+        return array(
+            array(
+                'Application\Sonata\NewsBundle\Entity\Post', 
+                '/sonata/news/post'
+            ),
+            array(
+                'Application\Sonata\NewsBundle\Document\Post', 
+                '/sonata/news/post'
+            ),
+            array(
+                'MyApplication\MyBundle\Entity\Post', 
+                '/myapplication/my/post'
+            ),
+            array(
+                'Symfony\Cmf\Bundle\FooBundle\Document\Menu', 
+                '/cmf/foo/menu'
+            ),
+            array(
+                'Symfony\Cmf\Bundle\FooBundle\Doctrine\Phpcr\Menu', 
+                '/cmf/foo/menu'
+            ),
+            array(
+                'Symfony\Bundle\BarBarBundle\Doctrine\Phpcr\Menu', 
+                '/symfony/barbar/menu'
+            ),
+        );
+    }
+
+    /**
+     * @dataProvider provideGetBaseRoutePattern
+     */
+    public function testGetBaseRoutePattern($objFqn, $expected)
+    {
+        $admin = new PostAdmin('sonata.post.admin.post', $objFqn, 'SonataNewsBundle:PostAdmin');
+        $this->assertEquals($expected, $admin->getBaseRoutePattern());
     }
 
     public function testGetBaseRoutePatternWithChildAdmin()
@@ -165,10 +198,44 @@ class BaseAdminTest extends \PHPUnit_Framework_TestCase
         $admin->getBaseRoutePattern();
     }
 
-    public function testGetBaseRouteName()
+    public function provideGetBaseRouteName()
     {
-        $admin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
-        $this->assertEquals('admin_sonata_news_post', $admin->getBaseRouteName());
+        return array(
+            array(
+                'Application\Sonata\NewsBundle\Entity\Post', 
+                'admin_sonata_news_post'
+            ),
+            array(
+                'Application\Sonata\NewsBundle\Document\Post', 
+                'admin_sonata_news_post'
+            ),
+            array(
+                'MyApplication\MyBundle\Entity\Post', 
+                'admin_myapplication_my_post'
+            ),
+            array(
+                'Symfony\Cmf\Bundle\FooBundle\Document\Menu', 
+                'admin_cmf_foo_menu'
+            ),
+            array(
+                'Symfony\Cmf\Bundle\FooBundle\Doctrine\Phpcr\Menu', 
+                'admin_cmf_foo_menu'
+            ),
+            array(
+                'Symfony\Bundle\BarBarBundle\Doctrine\Phpcr\Menu', 
+                'admin_symfony_barbar_menu'
+            ),
+        );
+    }
+
+    /**
+     * @dataProvider provideGetBaseRouteName
+     */
+    public function testGetBaseRouteName($objFqn, $expected)
+    {
+        $admin = new PostAdmin('sonata.post.admin.post', $objFqn, 'SonataNewsBundle:PostAdmin');
+
+        $this->assertEquals($expected, $admin->getBaseRouteName());
     }
 
     public function testGetBaseRouteNameWithChildAdmin()