Example #1
0
 /**
  * Match and process an HTTP request.
  *
  * @param Request $req    HTTP request.
  * @param string  $prefix URI prefix of the group.
  *
  * @return BoundRoute|null When match this returns BoundRoute, else null.
  *
  * @SuppressWarnings(PHPMD.StaticAccess)
  */
 public function matchRequest(Request $req, $prefix = '')
 {
     if (!in_array($req->getMethod(), $this->methods)) {
         return;
     }
     if ('/' !== substr($this->rawPath, 0, 1) && Dispatcher::isRegex($this->rawPath)) {
         preg_match('#\\A(.)(.*)(.[imsxeADSUXJu]*)\\z#', $this->rawPath, $matches);
         $compiledPath = $matches[1] . '\\A(?:' . preg_quote($prefix, $matches[1]) . ')' . $matches[2] . '\\z' . $matches[3];
     } else {
         $compiledPath = (new PathCompiler($prefix . $this->rawPath))->compile();
     }
     if (!preg_match($compiledPath, $req->getPathInfo(), $matches)) {
         return;
     }
     $dp = new Dispatcher($this->c);
     $bag = new ParameterBag();
     $bag->setRequest($req);
     $bag->addArray(array_reduce($dp->getParameters($this->controller), function ($carry, $param) {
         if ($param->isDefaultValueAvailable()) {
             $carry[$param->name] = $param->getDefaultValue();
         }
         return $carry;
     }, []));
     $bag->addArray($matches);
     $bag->addArray($this->c);
     $bag->addArray(['matches' => $matches, 'req' => $req, 'request' => $req, 'router' => $this->router]);
     $dp->setNamedArgs($bag);
     $dp->setTypedArg('Ranyuen\\Little\\Request', $req);
     $dp->setTypedArg('Symfony\\Component\\HttpFoundation\\Request', $req);
     $dp->setTypedArg('Ranyuen\\Little\\Router', $this->router);
     foreach ($this->conditions as $cond) {
         if (!$cond->isMatch($bag, $dp)) {
             return;
         }
     }
     return new BoundRoute($this->facade, $this->router, $req, $dp);
 }
Example #2
0
 private function findNamedRoute($name, Request $req, $prefix = '')
 {
     if (isset($this->namedRoutes[$name])) {
         $dp = new Dispatcher($this->c);
         $bag = new ParameterBag();
         $bag->setRequest($req);
         $bag->addArray(['req' => $req, 'request' => $req, 'router' => $this->facade]);
         $dp->setNamedArgs($bag);
         $dp->setTypedArg('Ranyuen\\Little\\Request', $req);
         $dp->setTypedArg('Symfony\\Component\\HttpFoundation\\Request', $req);
         $dp->setTypedArg('Ranyuen\\Little\\Router', $this->facade);
         return new BoundRoute($this->namedRoutes[$name], $this->facade, $req, $dp);
     }
     foreach ($this->routes as $route) {
         if (!(is_array($route) && $route[1] instanceof Router)) {
             continue;
         }
         list($path, $router) = $route;
         if ($route = $router->findMatchedRoute($name, $req, $prefix . $path)) {
             return $route;
         }
     }
 }