/** * Get a response. * * @param array $vars Extra values. * * @return mixed */ public function response(array $vars = []) { $this->dp->setNamedArgs($vars); $app = $this; foreach (array_reverse($this->router->getStacks()) as $stackClass) { $app = new $stackClass($app); } return $app->handle($this->req); }
/** * 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; }
/** * @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; }
/** * Run the controller. * * @param Dispatcher $dp DI container. * * @return mixed */ public function response(Dispatcher $dp) { return $dp->invoke($this->controller); }
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; } } }