Ejemplo n.º 1
0
 public function match($url, abstractRoute $preferRoute = null)
 {
     $this->lastUrl = $url;
     $this->lastRoute = null;
     if ($preferRoute) {
         $this->matchRoute($url, $preferRoute);
     } else {
         if (!empty($this->dbRouter)) {
             $result = $this->dbRouter->getByURL();
             if ($result) {
                 $this->lastRoute = $result;
             }
         } else {
             foreach ($this->collection->getAll() as $route) {
                 $result = $this->matchRoute($url, $route);
                 if ($result) {
                     $this->lastRoute = $route;
                 }
             }
         }
     }
     if ($this->lastRoute) {
         $result = $this->parseRoute($url, $this->lastRoute);
         if ($result) {
             return $result;
         }
     } else {
         /** @todo move part to new method */
         $result = $this->collection->findRouteBy("error", "path");
         if ($result) {
             return $result;
         }
     }
     return null;
 }
Ejemplo n.º 2
0
 public function testCollection()
 {
     $my = new RouteCollection();
     $route = new Route(array("path" => "/abstract", "class" => "index"));
     $my->addRoute(new Route(array("path" => "/abstract", "class" => "index")));
     $this->assertEquals(null, $my->findRouteBy("", "path"));
     $this->assertEquals($route, $my->findRouteBy("/abstract", "path"));
     $this->assertEquals(array($route), $my->getAll());
     $route = new Route(array("path" => "/abstract/class", "class" => "index"));
     $this->assertEquals($my, $my->mergeCollection((new RouteCollection())->addRoute($route)));
     $this->assertEquals($route, $my->findRouteBy("/abstract/class", "path"));
 }
Ejemplo n.º 3
0
 public function testRouter()
 {
     $my = new RouteCollection();
     $route3 = new Route(array("path" => "error", "class" => "index"));
     $my->addRoute($route3);
     $route = new Route(array("path" => "/[abstract]", "class" => "[abstract]"));
     $my->addRoute($route);
     $this->assertEquals(null, $my->findRouteBy("", "path"));
     $this->assertEquals($route, $my->findRouteBy("/[abstract]", "path"));
     $this->assertEquals(array($route3, $route), $my->getAll());
     $route2 = new Route(array("path" => "/abstract/[class]", "class" => "[class]"));
     $this->assertEquals($my, $my->mergeCollection((new RouteCollection())->addRoute($route2)));
     $found = $my->findRouteBy("/abstract/[class]", "path");
     $this->assertEquals($route2, $my->findRouteBy("/abstract/[class]", "path"));
     $router = new Router();
     $router->setRouteCollection($my);
     $this->assertEquals(new Route(array("path" => "/[abstract]", "class" => "abstract")), $router->match("/abstract"));
     $this->assertEquals($route3, $router->match("/abstract/class/?a"));
     $this->assertEquals(new Route(array("path" => "/abstract/[class]", "class" => "tempor")), $router->match("/abstract/tempor"));
 }
Ejemplo n.º 4
0
 public function mergeCollection(self $collection)
 {
     $this->routes = array_merge($collection->getAll(), $this->routes);
     return $this;
 }