/** * @param Route $route * @return RouteCollection */ public function addRoute(Route $route) : self { $id = $route->setRouteCollection($this)->getID(); $this->routes[$id] = $route; if (null !== ($name = $route->getName())) { $this->namedRoutes[$name] = $id; } return $this; }
/** * @param Context $context * @param Route $route * @param Router $router * @return bool */ public function pass(Router $router, Context $context, Route $route) : bool { $filter = $route->get('filter'); if (!is_callable($filter)) { return true; } $values = $router->extract($context, $route); $bindings = $router->bind($values, $route->getBindings()); $arguments = $router->buildArguments($filter, $bindings); return $filter(...$arguments); }
/** * @param BaseRouter|Router $router * @param BaseContext|Context $context * @param BaseRoute|Route $route * @return bool */ public function pass(BaseRouter $router, BaseContext $context, BaseRoute $route) : bool { /** @var CallbackFilter[] $filters */ $filters = $route->get('filters', []) + $router->getRouteCollection()->getFilters(); foreach ($route->get('before', []) as $name) { if (isset($filters[$name])) { if ($filters[$name]->setBindMode(false)->pass($router, $context, $route) === false) { return false; } } } return true; }
/** * @return callable[] */ public function getBindings() : array { if (!isset($this->cache[__FUNCTION__])) { /** @var RouteCollection $collection */ $collection = $this->collection; $this->cache[__FUNCTION__] = parent::getBindings() + $collection->getBindings(); } return $this->cache[__FUNCTION__]; }
/** * @param BaseRouter|BaseRouter $router * @param BaseContext|Context $context * @param BaseRoute|Route $route * @return bool */ public function pass(BaseRouter $router, BaseContext $context, BaseRoute $route) : bool { //match secure if (null !== ($secure = $route->get('secure'))) { if ($secure !== $context->isSecure()) { return false; } } //match method if (!in_array($context->method(), $route->get('method', ['GET']))) { return false; } // match domain if (null !== ($domain = $route->get('domain'))) { $regex = $router->getRouteCollection()->getDomainCompiler()->getRegex($domain, $route->getWildcards()); return preg_match($regex, $context->domain()); } return true; }
/** * @param BaseContext|Context $context * @param BaseRoute|Route $route * @return array */ public function extract(BaseContext $context, BaseRoute $route) : array { /** @var Context $context */ /** @var Route $route */ $names = []; if (null !== ($domain = $route->get('domain'))) { $names += $this->getRouteCollection()->getDomainCompiler()->getNames($domain); } $names += $this->getCompiler()->getNames($route->getPattern()); $regex = $this->getRouteCollection()->getRegex($route->getID()); $values = $this->getCompiler()->getValues($regex, (string) $context); return array_intersect_key($values, array_flip($names)) + $route->getDefaults(); }
/** * @param BaseRouter|Router $router * @param BaseContext|Context $context * @param BaseRoute|Route $route * @return DispatcherInterface */ public function resolve(BaseRouter $router, BaseContext $context, BaseRoute $route) : DispatcherInterface { $dispatcher = $route->get('dispatcher', 'default'); $factory = $this->collection->get($dispatcher); return $factory(); }
/** * Register a new view * * @param string $pattern * @param callable $resolver * @param int $priority * * @return Route */ public function handle(string $pattern, callable $resolver, int $priority = 0) : Route { $route = new Route($pattern, $resolver); $route->set('priority', $priority); $this->collection->addRoute($route); $this->cache = array(); //clear cache return $route; }