Symfony\Component\Routing\RouteCollection::getIterator PHP Method

getIterator() public method

Gets the current RouteCollection as an Iterator that includes all routes and child route collections.
public getIterator ( ) : ArrayIterator
return ArrayIterator An \ArrayIterator interface
    public function getIterator()
    {
        return new \ArrayIterator($this->routes);
    }

Usage Example

Ejemplo n.º 1
0
 private function compileRoutes(RouteCollection $routes, $supportsRedirections, $parentPrefix = null)
 {
     $code = array();
     $routeIterator = $routes->getIterator();
     $keys = array_keys($routeIterator->getArrayCopy());
     $keysCount = count($keys);
     $i = 0;
     foreach ($routeIterator as $name => $route) {
         $i++;
         if ($route instanceof RouteCollection) {
             $prefix = $route->getPrefix();
             $optimizable = $prefix && count($route->all()) > 1 && false === strpos($route->getPrefix(), '{');
             $indent = '';
             if ($optimizable) {
                 for ($j = $i; $j < $keysCount; $j++) {
                     if ($keys[$j] === null) {
                         continue;
                     }
                     $testRoute = $routeIterator->offsetGet($keys[$j]);
                     $isCollection = $testRoute instanceof RouteCollection;
                     $testPrefix = $isCollection ? $testRoute->getPrefix() : $testRoute->getPattern();
                     if (0 === strpos($testPrefix, $prefix)) {
                         $routeIterator->offsetUnset($keys[$j]);
                         if ($isCollection) {
                             $route->addCollection($testRoute);
                         } else {
                             $route->add($keys[$j], $testRoute);
                         }
                         $i++;
                         $keys[$j] = null;
                     }
                 }
                 if ($prefix !== $parentPrefix) {
                     $code[] = sprintf("        if (0 === strpos(\$pathinfo, %s)) {", var_export($prefix, true));
                     $indent = '    ';
                 }
             }
             foreach ($this->compileRoutes($route, $supportsRedirections, $prefix) as $line) {
                 foreach (explode("\n", $line) as $l) {
                     if ($l) {
                         $code[] = $indent . $l;
                     } else {
                         $code[] = $l;
                     }
                 }
             }
             if ($optimizable && $prefix !== $parentPrefix) {
                 $code[] = "        }\n";
             }
         } else {
             foreach ($this->compileRoute($route, $name, $supportsRedirections, $parentPrefix) as $line) {
                 $code[] = $line;
             }
         }
     }
     return $code;
 }
All Usage Examples Of Symfony\Component\Routing\RouteCollection::getIterator