/** * @param $pathinfo * @param Request $request * * @throws \Exception * * @return array */ protected function doMatch($pathinfo, Request $request = null) { $matchedRoute = $this->router->match($pathinfo, $request->server->all()); if ($matchedRoute === false) { throw new ResourceNotFoundException(); } $routeParams = $matchedRoute->params; // The 'action' key always exists and defaults to the Route Name, so we check accordingly if (!isset($routeParams['controller']) && $routeParams['action'] === $matchedRoute->name) { throw new \Exception('Matched the route: ' . $matchedRoute->name . ' but unable to locate any controller/action params to dispatch'); } // We need _controller, to that symfony ControllerResolver can pick this up if (!isset($routeParams['_controller'])) { if (isset($routeParams['controller'])) { $routeParams['_controller'] = $routeParams['controller']; } elseif (isset($routeParams['action'])) { $routeParams['_controller'] = $routeParams['action']; } else { throw new \Exception('Unable to determine the controller from route: ' . $matchedRoute->name); } } $routeParams['_route'] = $matchedRoute->name; // If the controller is an Object, and 'action' is defaulted to the route name - we default to __invoke if ($routeParams['action'] === $matchedRoute->name) { $routeParams['action'] = '__invoke'; } if (false === strpos($routeParams['_controller'], '::') && isset($routeParams['action'])) { $routeParams['_controller'] = sprintf('%s::%s', $routeParams['_controller'], $routeParams['action']); } return $routeParams; }
/** * Marshal a RouteResult representing a route failure. * * If the route failure is due to the HTTP method, passes the allowed * methods when creating the result. * * @return RouteResult */ private function marshalFailedRoute() { $failedRoute = $this->router->getFailedRoute(); if (!$failedRoute->failedMethod()) { return RouteResult::fromRouteFailure(); } return RouteResult::fromRouteFailure($failedRoute->method); }
/** * Marshal a RouteResult representing a route failure. * * If the route failure is due to the HTTP method, passes the allowed * methods when creating the result. * * @param Request $request * @return RouteResult */ private function marshalFailedRoute(Request $request) { $failedRoute = $this->router->getFailedRoute(); if ($failedRoute->failedMethod()) { return RouteResult::fromRouteFailure($failedRoute->method); } // Check to see if the route regex matched; if so, and we have an entry // for the path, register a 405. list($path) = explode('^', $failedRoute->name); if (isset($failedRoute->failed) && $failedRoute->failed !== AuraRoute::FAILED_REGEX && array_key_exists($path, $this->routes)) { return RouteResult::fromRouteFailure($this->routes[$path]); } return RouteResult::fromRouteFailure(); }
/** * Inject a route into the underlying Aura.Router instance. * * @param Route $route */ private function injectRoute(Route $route) { $path = $route->getPath(); $auraRoute = $this->router->add($route->getName(), $path, $route->getMiddleware()); foreach ($route->getOptions() as $key => $value) { switch ($key) { case 'tokens': $auraRoute->addTokens($value); break; case 'values': $auraRoute->addValues($value); break; } } $allowedMethods = $route->getAllowedMethods(); if (Route::HTTP_METHOD_ANY === $allowedMethods) { return; } $auraRoute->setServer(['REQUEST_METHOD' => implode('|', $allowedMethods)]); if (array_key_exists($path, $this->routes)) { $allowedMethods = array_merge($this->routes[$path], $allowedMethods); } $this->routes[$path] = $allowedMethods; }
/** * Returns a full route path. * * @param $name * @param array $args * @return false|string */ public function getRouteForName($name, array $args = array()) { return $this->router->generate($name, $args); }
/** * Add home route to given router * * @param Router $router */ public function addTo(Router $router) { $router->addGet('home', '/')->setValues(array('action' => 'home.index'))->setAccept(array('application/json', 'text/html')); }
/** * Searches for the target route on the router, returning it or false if it does not exist. * * @return \Aura\Router\Route|false */ protected function matchRoute() { $this->matched_route = $this->router->match($this->request->getPathInfo(), $this->request->server->all()); return $this->matched_route; }
/** * @param Configuration $config * @param Request $request */ public function __construct(Configuration $config, Request $request) { $this->request = $request; $container = $config->get('routes', []); if (!$container) { throw new \InvalidArgumentException('Routes must be defined in the configuration'); } if ($config->get('cache.config.enabled', FALSE)) { $cachePath = $config->getApplicationPath() . DIRECTORY_SEPARATOR . 'storage' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR . 'router'; if (file_exists($cachePath)) { $oldRouter = unserialize(file_get_contents($cachePath)); foreach ($oldRouter as $prop => $value) { $this->{$prop} = $value; } return; } } parent::__construct(new RouteCollection(new RouteFactory('Arhitect\\Router\\Adapter\\RouteAdapter')), new Generator()); foreach ($container as $routeName => $details) { if (isset($details['middlewares'])) { if (is_array($details['middlewares'])) { $this->middlewares[$routeName] = $details['middlewares']; } } if (isset($details['extends']) && FALSE != $details['extends']) { $path = [$routeName]; $parent = isset($container[$details['extends']]) ? $container[$details['extends']] : NULL; $parentName = isset($container[$details['extends']]) ? $details['extends'] : NULL; while ($parent) { $path[] = $parentName; if (isset($parent['extends'])) { $parentName = $parent['extends']; $parent = $container[$parent['extends']]; } else { $parent = NULL; } } $call = []; $that = $this; foreach ($path as $offset => $item) { if (!$call) { $call[$item] = function ($router) use($that, $container, $item) { /** @var Route $route */ $route = $router->add($item, $container[$item]['path']); $container[$item]['path'] = $route->path; $tmp = $that->addRoute($route->name, $container[$item]); $router->offsetSet($route->name, $tmp); }; continue; } if ($offset == count($path) - 1) { break; } $call[$item] = function ($router) use($container, $item, $call, $offset, $path) { $router->attach($item, $container[$item]['path'], $call[$path[$offset - 1]]); }; } $this->attach($path[$offset], $container[$path[$offset]]['path'], $call[$path[$offset - 1]]); continue; } if (!$this->offsetExists($routeName)) { $this->offsetSet($routeName, $this->addRoute($routeName, $details)); } } if ($config->get('cache.config.enabled', FALSE)) { file_put_contents($cachePath, serialize($this)); } }
/** * @inheritdoc */ public function generate($route, array $parameters = null) { return $this->router->generate($route, $parameters); }
/** * Add poll routes to given router * * @param Router $router */ public function addTo(Router $router) { $router->addGet('poll.get', '/poll/{id}')->setValues(array('action' => 'poll.get'))->addTokens(array('id' => Uuid::VALID_PATTERN))->setAccept(array('application/json', 'text/html')); $router->addPost('poll.create', '/poll/create')->setValues(array('action' => 'poll.create'))->setAccept(array('application/json', 'text/html')); }