فهرست منبع

[Routing] fixed side-effect in the PHP matcher dumper

Fabien Potencier 13 سال پیش
والد
کامیت
cbb4bbae97

+ 2 - 3
src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php

@@ -54,7 +54,8 @@ class PhpMatcherDumper extends MatcherDumper
 
     private function addMatcher($supportsRedirections)
     {
-        $code = implode("\n", $this->compileRoutes($this->getRoutes(), $supportsRedirections));
+        // we need to deep clone the routes as we will modify the structure to optimize the dump
+        $code = implode("\n", $this->compileRoutes(clone $this->getRoutes(), $supportsRedirections));
 
         return <<<EOF
 
@@ -74,7 +75,6 @@ EOF;
     {
         $code = array();
 
-        $routes = clone $routes;
         $routeIterator = $routes->getIterator();
         $keys = array_keys($routeIterator->getArrayCopy());
         $keysCount = count($keys);
@@ -83,7 +83,6 @@ EOF;
         foreach ($routeIterator as $name => $route) {
             $i++;
 
-            $route = clone $route;
             if ($route instanceof RouteCollection) {
                 $prefix = $route->getPrefix();
                 $optimizable = $prefix && count($route->all()) > 1 && false === strpos($route->getPrefix(), '{');

+ 5 - 0
src/Symfony/Component/Routing/Route.php

@@ -50,6 +50,11 @@ class Route
         $this->setOptions($options);
     }
 
+    public function __clone()
+    {
+        $this->compiled = null;
+    }
+
     /**
      * Returns the pattern.
      *

+ 11 - 0
src/Symfony/Component/Routing/RouteCollection.php

@@ -42,6 +42,17 @@ class RouteCollection implements \IteratorAggregate
         $this->prefix = '';
     }
 
+    public function __clone()
+    {
+        $parent = $this;
+        foreach ($this->routes as $name => $route) {
+            $this->routes[$name] = clone $route;
+            if ($route instanceof RouteCollection) {
+                $this->routes[$name]->setParent($this);
+            }
+        }
+    }
+
     /**
      * Gets the parent RouteCollection.
      *