Example #1
0
 /**
  * Appends route to routing table
  *
  * @param    Route   $route     The route object
  * @param    array   $parts     optional The list of the parts in the route path
  * @return   Item    Returns the terminal item which contains the route
  */
 public function appendRoute(Route $route, array &$parts = null)
 {
     if ($parts === null) {
         $parts = explode('/', trim($route->getPath(), '/'));
     }
     $part = array_shift($parts);
     $pathPart = new PathPart($part);
     if (strpos($part, ':') !== false) {
         //This is regexp part
         $pathPart->type = PathPart::TYPE_REGEXP;
         $pathPart->value = '~^' . preg_replace_callback('~:([\\w]+)\\+?~', function ($m) use($route, $pathPart) {
             $requirements = $route->getRequirements();
             if (isset($requirements[$m[1]])) {
                 return '(?<' . $m[1] . '>' . $requirements[$m[1]] . ')';
             }
             if (substr($m[0], -1) === '+') {
                 $pathPart->type = PathPart::TYPE_REGEXP_PATH;
                 return '(?<' . $m[1] . '>.+)';
             }
             return '(?<' . $m[1] . '>[^/]+)';
         }, str_replace(')', ')?', $part)) . '$~';
     }
     if (!$this->offsetExists($pathPart->value)) {
         /* @var $item Item */
         $item = new Item($pathPart);
         $this[$pathPart->value] = $item;
     } else {
         $item = $this[$pathPart->value];
     }
     if (empty($parts)) {
         //This is the terminal item
         if (!in_array($route, $item->routes)) {
             $item->routes[] = $route;
         }
         return $item;
     }
     return $item->getTable()->appendRoute($route, $parts);
 }