/** * @inheritdoc * @param array $matches The list of regex matches */ public function isMatch(ParsedRoute $route, Request $request, array &$matches = []) { $isMatch = preg_match($route->getHostRegex(), $request->getHeaders()->get("HOST"), $matches) === 1; // Remove the subject array_shift($matches); return $isMatch; }
/** * Registers view functions * * @param Request $request The current request * @param ITranspiler $transpiler The transpiler to register to * @param UrlGenerator $urlGenerator What generates URLs from routes * @param ISession $session The current session */ public function run(Request $request, ITranspiler $transpiler, UrlGenerator $urlGenerator, ISession $session) { // Add the ability to display a hidden input with the current CSRF token $transpiler->registerViewFunction("csrfInput", function () use($session) { return sprintf('<input type="hidden" name="%s" value="%s">', CsrfTokenChecker::TOKEN_INPUT_NAME, $session->get(CsrfTokenChecker::TOKEN_INPUT_NAME)); }); // Add the ability to display the CSRF token $transpiler->registerViewFunction("csrfToken", function () use($session) { return $session->get(CsrfTokenChecker::TOKEN_INPUT_NAME); }); // Add the ability to tell if the current route is a particular route $transpiler->registerViewFunction("currentRouteIs", function ($routeName) use($request, $urlGenerator) { $regex = call_user_func([$urlGenerator, "createRegexFromName"], $routeName); // Strip the delimiters $regex = substr($regex, 1, -1); // Check if the returned regex is a path or full URL regex if (preg_match("#^\\^http(s)?\\\\://#", $regex) === 1) { return $request->isUrl($regex, true); } else { return $request->isPath($regex, true); } }); // Add the ability to generate URLs to named routes from views $transpiler->registerViewFunction("route", function ($routeName) use($urlGenerator) { return call_user_func_array([$urlGenerator, "createFromName"], func_get_args()); }); }
/** * Starts the session * * @param Request $request The current request */ protected function startSession(Request $request) { $this->gc(); $this->session->setId($request->getCookies()->get($this->session->getName())); $this->sessionHandler->open(null, $this->session->getName()); $sessionVars = @unserialize($this->sessionHandler->read($this->session->getId())); if ($sessionVars === false) { $sessionVars = []; } $this->session->start($sessionVars); }
/** * Gets whether or not the token should even be checked * * @param Request $request The current request * @return bool True if the token should be checked, otherwise false */ private function tokenShouldNotBeChecked(Request $request) { return in_array($request->getMethod(), [Request::METHOD_GET, Request::METHOD_HEAD, Request::METHOD_OPTIONS]); }
/** * @inheritdoc */ public function isMatch(ParsedRoute $route, Request $request) { return $route->isSecure() && $request->isSecure() || !$route->isSecure(); }
/** * Routes a request * * @param Request $request The request to route * @return Response The response from the controller * @throws RouteException Thrown if the controller or method could not be called * @throws HttpException Thrown if there was no matching route */ public function route(Request $request) { $method = $request->getMethod(); /** @var ParsedRoute $route */ foreach ($this->routeCollection->get($method) as $route) { $compiledRoute = $this->compiler->compile($route, $request); if ($compiledRoute->isMatch()) { $this->matchedRoute = $compiledRoute; return $this->dispatcher->dispatch($this->matchedRoute, $request, $this->matchedController); } } // If we've gotten here, we've got a missing route throw new HttpException(404); }
/** * @inheritdoc */ public function registerBindings(IContainer $container) { $container->bind(Request::class, Request::createFromGlobals()); }