Ejemplo n.º 1
0
 public function compile(RouteInterface $route)
 {
     $name = $route->getName();
     if (!$this->isCached($name)) {
         $this->compiled[$name] = $this->doCompile($route);
     }
     return $this->compiled[$name];
 }
Ejemplo n.º 2
0
 /**
  * 添加路由
  * @param RouteInterface $route
  * @param string|null $name
  */
 function add(RouteInterface $route, $name = null)
 {
     if (!is_null($name)) {
         $this->names[$name] = $route;
     }
     $action = $route->getAction();
     if (is_scalar($action)) {
         $this->actions[$action] = $route;
     }
     $this->routes[] = $route;
 }
Ejemplo n.º 3
0
 /**
  * 添加route
  *
  * @param RouteInterface $route            
  */
 function add(RouteInterface $route)
 {
     if ($route->hasParameter('name')) {
         $routeKey = $route->getParameter('name');
         $this->_names[$routeKey] = $route;
     }
     $action = $route->getParameter('action');
     if (!empty($action) && is_string($action)) {
         $routeKey = $route->getPrefix() . '/' . $action;
         $this->_actions[$routeKey] = $route;
     }
 }
Ejemplo n.º 4
0
 /**
  * Adds a route to the end of the list.
  *
  * @param string $name The route's name
  * @param RouteInterface $route
  *
  * @return Router
  */
 public function add($name, RouteInterface $route)
 {
     // clear it
     unset($this->routes[$name]);
     // Name the route
     $route->setDefault("_name", $name);
     // and Router
     $route->setDefault("_router", $this);
     // add it to the bottom of the list
     $this->routes[$name] = $route;
     return $this;
 }
Ejemplo n.º 5
0
 /**
  * {@inheritdoc}
  */
 public function add($routeName, RouteInterface $route)
 {
     if (!is_string($routeName)) {
         throw new \InvalidArgumentException('Routename must be string.');
     }
     $this->routes[$routeName] =& $route;
     foreach ($route->getMethods() as $method) {
         $this->methodIndex[$method][$routeName] = true;
     }
     foreach ($route->getSchemes() as $scheme) {
         $this->schemeIndex[$scheme][$routeName] = true;
     }
 }
Ejemplo n.º 6
0
 /**
  * 根据请求指派路由
  *
  * @param \Powernote\Net\Request $request
  * @param bool $resetRouted 重置之前可能存在的路由结果【重新路由】
  * @return \Powernote\Net\Response|null
  */
 public function dispatch(\Powernote\Net\Request $request, $resetRouted = false)
 {
     // 如果之前已成功匹配到路由则直接返回上次匹配到的路由
     if ($this->routed instanceof RouteInterface && $resetRouted === false) {
         return $this->routed->response();
     }
     // 如果路由器列表为空则直接返回
     if ($this->isEmpty()) {
         return null;
     }
     // 只保留问号左侧的Path信息
     $request->url = current(explode('?', $request->url));
     // 优先处理组路由
     $routes = $this->matchGroup($request);
     // 如果没有设置任何路由则返回空
     if (count($routes) == 0) {
         return null;
     }
     // 遍历所有的路由,如果匹配成功则返回该路由
     foreach ($routes as $route) {
         if ($route->match($request)) {
             $this->routed = $route;
             return $this->routed->response();
         }
     }
     return null;
 }
Ejemplo n.º 7
0
 public function map($rule, array $target = [], array $conditions = [], array $options = [])
 {
     if (empty($this->routeClass)) {
         $this->routeClass = new Route();
     }
     /**
      * @var route $route
      */
     $route = $this->routeClass->route(['rule' => $rule, 'request' => $this->requestUri, 'target' => $target, 'conditions' => $conditions, 'options' => $options]);
     if ($route->isMatched()) {
         $this->setRoute($route);
         return true;
     }
     return false;
 }
Ejemplo n.º 8
0
 /**
  * {@inheritdoc}
  */
 public function getHandler()
 {
     return $this->route->getHandler();
 }
Ejemplo n.º 9
0
 /**
  * Verifies that the Route matches expected setup.
  *
  * Currently only checks if the http post method is valid.
  * TODO: Check if action is a callable.
  * TODO: Check if route is valid.
  *
  * @param RouteInterface $route
  *
  * @return bool
  */
 protected function isValidRoute(RouteInterface $route)
 {
     $routeHttpMethod = $route->getHttpMethod();
     return in_array($routeHttpMethod, $this->allowedMethods);
 }
Ejemplo n.º 10
0
 /**
  * 添加路由
  *
  * @param RouteInterface $route            
  */
 function add(RouteInterface $route)
 {
     $route->setPreifx($this->_prefix);
     $this->_routes[] = $route;
     RouteStore::newInstance()->add($route);
 }