/** * createMap * * @param RouteCollectionInterface $routes * * @return array */ private function createMap(RouteCollectionInterface $routes) { $i = 0; $m = []; $rpl = sprintf(self::NGRP_RPLC, Ps::EXP_DELIM); $expr = []; $pfx = 'r' . time(); $expr['prefix'] = $pfx; foreach ($routes->all() as $name => $route) { foreach ($route->getMethods() as $method) { $m[$method][$name] = $route; } } foreach ($m as $mn => $rts) { $mn = strtolower($mn); $expr[$mn]['map'] = []; $regex = []; foreach ($rts as $rn => $rt) { $ctx = $rt->getContext(); $index = $pfx . '_' . $this->replaceRouteName($rn, $i); $ctxRegex = preg_replace($rpl, '$1r' . (string) $i . '_$2>', $ctx->getRegex(true)); $regex[] = sprintf('(?P<%s>%s)', $index, $ctxRegex); $expr[$mn]['map'][$index] = [$i, $rn, 'r' . $i . '_']; $i++; } $i = 0; $expr[$mn]['regex'] = sprintf('%1$s(?:^' . join('|', $regex) . ')$%1$sx', Ps::EXP_DELIM); } return $expr; }
/** * {@inheritdoc} */ public function generate($name, array $parameters = [], $host = null, $type = self::RELATIVE_PATH) { if (null === $this->routes || !($route = $this->routes->get($name))) { throw new \InvalidArgumentException(sprintf('A route with name "%s" could not be found.', $name)); } return $this->compilePath($route, $parameters, $host ?: $route->getHost(), $type, $name); }
/** * createMatchContextFromParameters * * @param array $parameters * @param string $url * * @return MatchContextInterface */ private function createMatchContextFromParameters(array $vars, $name, $request) { $handler = $this->routes->get($name)->getHandler(); return new MatchContext(Matcher::MATCH, $name, $request, $handler, $vars); }
/** * reverseMapRoute * * @param RouteCollectionInterface $routes * @param array $matches * @param array $map * * @return array [Lucid\Mux\RouteInterface, array] */ private function reverseMapRoute(RouteCollectionInterface $routes, array $matches, array $map = []) { foreach ($matches = array_filter($matches[0]) as $key => $subject) { if (is_int($key)) { continue; } if (!isset($map[$key])) { continue; } list($index, $name, $prefix) = $map[$key]; try { $route = $routes->get($name); } catch (\Exception $e) { throw new RuntimeException('Route does not exists.'); } $route = $routes->get($name); $args = []; $keys = $route->getContext()->getVars(); $args = array_combine($keys, array_map(function ($key) use($matches, $prefix) { return isset($matches[$prefix . $key]) ? $this->getArgValue($matches[$prefix . $key]) : null; }, $keys)); return [$name, $route, array_merge($route->getDefaults(), array_filter($args))]; } throw new RuntimeException('No match found.'); }
/** * createMaps * * @param RouteCollectionInterface $routes * * @return void */ private function createMaps(RouteCollectionInterface $routes) { foreach ($routes->all() as $name => $route) { foreach ($route->getMethods() as $method) { $this->mMap[$method][] = $name; } foreach ($route->getSchemes() as $scheme) { $this->scMap[$scheme][] = $name; } $this->spMap[$route->getContext()->getStaticPath()][] = $name; } }
/** * reduce * * @param RouteCollectionInterface $routes * @param RequestContext $context * * @return RouteCollectionInterface */ private function filterByMethodAndScheme(RouteCollectionInterface $routes, Request $context) { return $routes->findByMethod($context->getMethod())->findByScheme($context->getScheme()); }