/** * 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; }
/** * Run the controller. * * @param Dispatcher $dp DI container. * * @return mixed */ public function response(Dispatcher $dp) { return $dp->invoke($this->controller); }
/** * Process an HTTP error. * * @param int $status HTTP status code. * @param Request $req HTTP request. * @param \Exception $ex Exception. * * @return Response */ public function runError($status, Request $req, \Exception $ex = null) { if (!($handler = $this->findErrorHandler($status))) { return new Response((string) $ex, $status); } $dp = new Dispatcher($this->c); $bag = new ParameterBag(); $bag->setRequest($req); $bag->addArray(['e' => $ex, 'ex' => $ex, 'err' => $ex, 'error' => $ex, 'exception' => $ex, 'req' => $req, 'request' => $req, 'router' => $this->facade]); $dp->setNamedArgs($bag); $dp->setTypedArg('Exception', $ex); $dp->setTypedArg('Ranyuen\\Little\\Request', $req); $dp->setTypedArg('Symfony\\Component\\HttpFoundation\\Request', $req); $dp->setTypedArg('Ranyuen\\Little\\Router', $this->facade); try { $res = $dp->invoke($handler); } catch (\Exception $ex2) { if (500 === $status) { return new Response((string) $ex2, 500); } return $this->runError(500, (string) $ex2); } $res = $this->toResponse($res); $res->setStatusCode($status); return $res; }