/** * @param Event $event * @param Request $request * @param Route $route * @return Request */ function __invoke(Event $event, Request $request, Route $route) { if (!preg_match('(\\G' . $route->regex() . ')', $request->path(), $match, null, $request->length())) { return null; } $request[Arg::CONTROLLER] = $route->controller(); $request[Arg::LENGTH] = $request->length() + strlen($match[0]); $request[Arg::MATCHED] = $request->length() == strlen($request->path()); $request[Arg::PARAMS] = $this->params($match, $route->defaults() + $request->params()); return $request->matched() || $route->children() && $event->stop() ? $request : null; }
/** * @param Route $parent * @param array|Route $route * @param array $path * @param bool $start * @return array|Route * @throws \RuntimeException */ function __invoke(Route $parent, $route, array $path, $start = false) { if ($root = $parent->child($path[0])) { return $this($root, $route, array_slice($path, 1)); } isset($path[1]) && Exception::runtime('Parent route not found: ' . $route[Arg::NAME]); $route[Arg::NAME] = $path[0]; $start && empty($route[Arg::ROUTE]) && isset($route[Arg::NAME]) && ($route[Arg::ROUTE] = $route[Arg::NAME]); !$start && empty($route[Arg::ROUTE]) && ($route[Arg::ROUTE] = Arg::SEPARATOR . $path[0]); $route = $this->definition($route); $parent->add($path[0], $route); return $route; }
/** * @param Route $route * @param Definition $definition * @return Route */ public function __invoke(Route $route, Definition $definition) { if (!$definition->wildcard()) { return $route; } $params = $route->params(); $parts = explode('/', trim(substr($route->path(), $route->length()), '/')); for ($i = 0, $n = count($parts); $i < $n; $i += 2) { if (!isset($parts[$i + 1]) || isset($params[$parts[$i]])) { continue; } $params[$parts[$i]] = $parts[$i + 1]; } $route->set(Route::PARAMS, $params); $route->set(Route::MATCHED, true); return $route; }
/** * @param Route $route * @param Route $parent * @return Route */ protected function merge(Route $route, Route $parent) { ($options = $parent->options()) && ($route[Arg::OPTIONS] = $route->options() + $options); ($middleware = $parent[Arg::MIDDLEWARE]) && ($route[Arg::MIDDLEWARE] = array_merge($middleware, $route[Arg::MIDDLEWARE] ?: [])); return $route; }
/** * @param Request $request * @param Route $route * @return Request|NotFound */ function __invoke(Request $request, Route $route) { return !$route->host() || $this->match($request, $route) ? $request : ($this->optional($route, Arg::HOST) ? null : new NotFound()); }
/** * @param Route $route * @param Definition $definition * @return Route */ public function __invoke(Route $route, Definition $definition) { return !$definition->method() || in_array($route->method(), (array) $definition->method()) ? $route : null; }
/** * @param Route $route */ public function __invoke(Route $route) { $route->set(Route::PATH, urldecode($route->path()) ?: '/'); }
/** * @param Request $request * @param Route $route * @return Request|BadRequest */ function __invoke(Request $request, Route $route) { return !$route->scheme() || $this->match($request, $route) ? $request : new BadRequest(); }
/** * @param Route $route * @return mixed */ protected function options(Route $route) { return $route->options() + $this->options; }
/** * @param Request $request * @param Route $route * @return Request */ function __invoke(Request $request, Route $route) { if (!$route->wildcard()) { return $request; } $params = $this->params($request); $options = $this->options($route); $request[Arg::PARAMS] = $this->match($params, $options, $this->parts($params, $options)); return $request; }
/** * @param Request $request * @param Route $route * @return Request|MethodNotAllowed */ function __invoke(Request $request, Route $route) { return !$route->method() || $this->match($request, $route) ? $request : ($this->optional($route, Arg::METHOD) ? null : new MethodNotAllowed()); }
/** * @param Request $request * @param Route $route * @return Request */ function __invoke(Request $request, Route $route) { $route->actions() && ($controller = $route->action($request->method())) && ($request[Arg::CONTROLLER] = $controller); return $request; }
/** * @param Route $route * @return \Closure|null */ protected function wildcard(Route $route) { return !$route->wildcard() ? null : function ($path, array $params = []) use($route) { foreach ($params as $key => $value) { null !== $value && ($path .= Arg::SEPARATOR . $key . Arg::SEPARATOR . $value); } return $path; }; }