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
Exemplo 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;
 }
Exemplo n.º 2
0
 private function compileRoutes(RouteCollection $routes, $supportsRedirections, $parentPrefix = null)
 {
     $code = '';
     $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(), '{');
             $optimized = false;
             if ($optimizable) {
                 for ($j = $i; $j < $keysCount; $j++) {
                     if (null === $keys[$j]) {
                         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)) {\n", var_export($prefix, true));
                     $optimized = true;
                 }
             }
             if ($optimized) {
                 foreach (explode("\n", $this->compileRoutes($route, $supportsRedirections, $prefix)) as $line) {
                     if ('' !== $line) {
                         $code .= '    ';
                         // apply extra indention
                     }
                     $code .= $line . "\n";
                 }
                 $code = substr($code, 0, -2);
                 // remove redundant last two line breaks
                 $code .= "        }\n\n";
             } else {
                 $code .= $this->compileRoutes($route, $supportsRedirections, $prefix);
             }
         } else {
             $code .= $this->compileRoute($route, $name, $supportsRedirections, $parentPrefix) . "\n";
         }
     }
     return $code;
 }
Exemplo n.º 3
0
 /**
  * This method will be called if this command is triggered
  *
  * @param ArgumentsContainer $arguments
  *
  * @return mixed
  */
 public function execute(ArgumentsContainer $arguments)
 {
     $this->dispatcher->trigger(new SimpleEvent('thinframe.routes.pre_load'));
     $routesTable = [];
     foreach ($this->routeCollection->getIterator() as $name => $route) {
         /* @var $route Route */
         if (count($route->getMethods()) == 0) {
             $routesTable[$route->getPath()] = ['action' => $name, 'method' => '*'];
         }
         foreach ($route->getMethods() as $method) {
             $routesTable[$route->getPath()] = ['action' => $name, 'method' => $method];
         }
     }
     $maxSize = max(array_map("strlen", array_keys($routesTable)));
     foreach ($routesTable as $path => $details) {
         $this->outputDriver->send("[format effects='bold'] {method} \t {path} \t {action} [/format]" . PHP_EOL, ['method' => $details['method'], 'path' => str_pad($path, $maxSize + 4, " ", STR_PAD_RIGHT), 'action' => $details['action']]);
     }
 }
Exemplo n.º 4
0
 /**
  * Get an array of all routes (1 level deep) for all routes in the system.
  *
  * @param \Symfony\Component\Routing\RouteCollection $routes
  *
  * @return array
  */
 private static function pullRoutesFromCollection(\Symfony\Component\Routing\RouteCollection $routes)
 {
     $routesArray = array();
     foreach ($routes->getIterator() as $key => $route) {
         if ($route instanceof \Symfony\Component\Routing\RouteCollection) {
             $routesArray += self::pullRoutesFromCollection($route);
         } else {
             $routesArray[] = $route;
         }
     }
     return $routesArray;
 }
Exemplo n.º 5
0
 public function testUniqueRouteWithGivenName()
 {
     $collection1 = new RouteCollection();
     $collection1->add('foo', new Route('/old'));
     $collection2 = new RouteCollection();
     $collection3 = new RouteCollection();
     $collection3->add('foo', $new = new Route('/new'));
     $collection1->addCollection($collection2);
     $collection2->addCollection($collection3);
     $collection1->add('stay', new Route('/stay'));
     $iterator = $collection1->getIterator();
     $this->assertSame($new, $collection1->get('foo'), '->get() returns new route that overrode previous one');
     // size of 2 because collection1 contains collection2 and /stay but not /old anymore
     $this->assertCount(2, $iterator, '->addCollection() removes previous routes when adding new routes with the same name');
     $this->assertInstanceOf('Symfony\\Component\\Routing\\RouteCollection', $iterator->current(), '->getIterator returns both Routes and RouteCollections');
     $iterator->next();
     $this->assertInstanceOf('Symfony\\Component\\Routing\\Route', $iterator->current(), '->getIterator returns both Routes and RouteCollections');
 }
Exemplo n.º 6
0
 public function testUniqueRouteWithGivenName()
 {
     $collection1 = new RouteCollection();
     $collection1->add('foo', new Route('/old'));
     $collection2 = new RouteCollection();
     $collection3 = new RouteCollection();
     $collection3->add('foo', $new = new Route('/new'));
     $collection2->addCollection($collection3);
     $collection1->addCollection($collection2);
     $this->assertSame($new, $collection1->get('foo'), '->get() returns new route that overrode previous one');
     // size of 1 because collection1 contains /new but not /old anymore
     $this->assertCount(1, $collection1->getIterator(), '->addCollection() removes previous routes when adding new routes with the same name');
 }
 /**
  * Adds a name prefix to the route name of all collection routes.
  *
  * @param RouteCollection $collection    Route collection
  * @param array $namePrefix              NamePrefix to add in each route name of the route collection
  *
  * @return RouteCollection
  */
 public function addParentNamePrefix(RouteCollection $collection, $namePrefix)
 {
     if (!isset($namePrefix) || ($namePrefix = trim($namePrefix)) === "") {
         return $collection;
     }
     $iterator = $collection->getIterator();
     foreach ($iterator as $key1 => $route1) {
         $collection->add($namePrefix . $key1, $route1);
         $collection->remove($key1);
     }
     return $collection;
 }
Exemplo n.º 8
0
 /**
  * @return array
  */
 public function getRouteList()
 {
     return $this->SymfonyRouteCollection->getIterator()->getArrayCopy();
 }