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
 /**
  * @SuppressWarnings(PHPMD.StaticAccess)
  */
 private function compileParam($element)
 {
     $regex = '';
     if (isset($this->conditions[$element['val']]) && is_string($cond = $this->conditions[$element['val']])) {
         if (Dispatcher::isRegex($cond)) {
             preg_match($cond, '#\\A.(.*).[imsxeADSUXJu]*\\z#', $matches);
             $cond = $matches[1];
         } else {
             $cond = preg_quote($cond, '#');
         }
         $regex .= '(?<' . $element['val'] . '>' . $cond . ')';
     } elseif ($element['is_wildcard']) {
         $regex .= '(?<' . $element['val'] . '>.+?)';
     } else {
         $regex .= '(?<' . $element['val'] . '>\\w+?)';
     }
     if ($element['is_optional']) {
         $regex .= '?';
     }
     return $regex;
 }
Example #3
0
 /**
  * Dose the param match the condition?
  *
  * @param ParameterBag $bag Params.
  * @param Dispatcher   $dp  Dispatcher with params.
  *
  * @return bool
  *
  * @SuppressWarnings(PHPMD.StaticAccess)
  */
 public function isMatch(ParameterBag $bag, Dispatcher $dp)
 {
     if ($this->invokable) {
         try {
             return $dp->invoke($this->invokable);
         } catch (\Exception $ex) {
             // This exception must be ignored.
             return false;
         }
     }
     $value = $bag[$this->name];
     if (is_null($value)) {
         return false;
     }
     if (!Dispatcher::isRegex($this->pattern)) {
         return $this->pattern === $value;
     }
     $value = (string) $value;
     return !!preg_match($this->pattern, $value, $m) && $m[0] === $value;
 }