Pārlūkot izejas kodu

Added removeAll and removeAllExcept methods in RouteCollection

Andrej Hudec 12 gadi atpakaļ
vecāks
revīzija
ffd2fc61b0

+ 53 - 2
Resources/doc/reference/routing.rst

@@ -65,12 +65,63 @@ defined by its name.
         }
     }
 
+Removing a route
+--------------
+
+Any registered route can be easily removed.
+
+.. code-block:: php
+
+    <?php
+    use Sonata\AdminBundle\Route\RouteCollection;
+
+    class MediaAdmin extends Admin
+    {
+        protected function configureRoutes(RouteCollection $collection)
+        {
+            $collection->remove('delete');
+        }
+    }
+
+
+If you want to disable all default Sonata routes except few whitelisted ones, you can use ``removeAllExcept()`` method.
+This method accepts an array of routes you want to keep active.
+
+.. code-block:: php
+
+    <?php
+    use Sonata\AdminBundle\Route\RouteCollection;
+
+    class MediaAdmin extends Admin
+    {
+        protected function configureRoutes(RouteCollection $collection)
+        {
+            //Only `list` and `edit` route will be active
+            $collection->removeAllExcept(array('list', 'edit'));
+        }
+    }
+
+If you want to remove all default routes, you can use ``removeAll()`` method.
+
+.. code-block:: php
+
+    <?php
+    use Sonata\AdminBundle\Route\RouteCollection;
+
+    class MediaAdmin extends Admin
+    {
+        protected function configureRoutes(RouteCollection $collection)
+        {
+            //All routes are removed
+            $collection->removeAll();
+        }
+    }
 
 Persistent parameters
 ---------------------
 
-In some cases, the interface might be required to pass the same parameters 
-across the different ``Admin``'s actions. Instead of setting them in the 
+In some cases, the interface might be required to pass the same parameters
+across the different ``Admin``'s actions. Instead of setting them in the
 template or doing other weird hacks, you can define a ``getPersistentParameters``
 method. This method will be used when a link is being generated.
 

+ 36 - 0
Route/RouteCollection.php

@@ -136,6 +136,42 @@ class RouteCollection
         return $this;
     }
 
+    /**
+     * Remove all routes except routes in $routeList
+     *
+     * @param array $routeList
+     *
+     * @return \Sonata\AdminBundle\Route\RouteCollection
+     */
+    public function removeAllExcept(array $routeList)
+    {
+        $routeCodeList = array();
+        foreach ($routeList as $name) {
+            $routeCodeList[] = $this->getCode($name);
+        }
+
+        $elements = $this->elements;
+        foreach ($elements as $key => $element) {
+            if (!in_array($key, $routeCodeList)) {
+                unset($this->elements[$key]);
+            }
+        }
+
+        return $this;
+    }
+
+    /**
+     * Remove all routes
+     *
+     * @return \Sonata\AdminBundle\Route\RouteCollection
+     */
+    public function removeAll()
+    {
+        $this->elements = array();
+
+        return $this;
+    }
+
     /**
      * Convert a word in to the format for a symfony action action_name => actionName
      *

+ 17 - 0
Tests/Route/RouteCollectionTest.php

@@ -63,6 +63,23 @@ class RouteCollectionTest extends \PHPUnit_Framework_TestCase
         $route = $routeCollection->get('create');
 
         $this->assertInstanceOf('Symfony\Component\Routing\Route', $route);
+
+        $routeCollection->add('view');
+        $routeCollection->add('edit');
+        $routeCollection->removeAll();
+        $this->assertFalse($routeCollection->has('create'));
+        $this->assertFalse($routeCollection->has('view'));
+        $this->assertFalse($routeCollection->has('edit'));
+
+        $routeCollection->add('create');
+        $routeCollection->add('view');
+        $routeCollection->add('edit');
+        $routeCollection->add('list');
+        $routeCollection->removeAllExcept(array('create','edit'));
+        $this->assertTrue($routeCollection->has('create'));
+        $this->assertTrue($routeCollection->has('edit'));
+        $this->assertFalse($routeCollection->has('view'));
+        $this->assertFalse($routeCollection->has('list'));
     }
 
     public function testChildCollection()