public function getEventForRequest(RequestInterface $request) { $requestedRoute = $request->getUrl()->getPath(); $requestedMethod = $request->getMethod(); $routeInfo = $this->dispatcher->dispatch($requestedMethod, $requestedRoute); switch ($routeInfo[0]) { case Dispatcher::NOT_FOUND: throw new NotFoundException('Route \'' . $requestedRoute . '\' with method \'' . $requestedMethod . '\' not found.'); break; case Dispatcher::METHOD_NOT_ALLOWED: //$allowedMethods = $routeInfo[1]; throw new Exception('Method not allowed', StatusCode::METHOD_NOT_ALLOWED); break; case Dispatcher::FOUND: /** @var string $eventClass */ $eventClass = $routeInfo[1]; /** @var RouteFoundEvent $event */ $event = new $eventClass(); /** @var string[] $vars */ $vars = $routeInfo[2]; $request->getRouteParameterCollection()->setAll($vars); $event->setRequest($request); return $event; default: throw new Exception('Error in FastRoute!'); } }
public function modify(HeaderCollectionInterface $responseHeader, RequestInterface $request) { $origin = $request->getHeaderCollection()->getHeaderValue(HeaderName::ORIGIN, ''); if (!empty($origin)) { $responseHeader->setHeaderValue(HeaderName::ACCESS_CONTROL_ALLOW_ORIGIN, $origin); } $mustAllowCredentials = !empty($request->getHeaderCollection()->getHeaderValue(HeaderName::AUTHORIZATION, '')); if ($mustAllowCredentials) { $responseHeader->setHeaderValue(HeaderName::ACCESS_CONTROL_ALLOW_CREDENTIALS, 'true'); } }
public function writeResponse(ResponseInterface $response, RequestInterface $request) { //set default header-collection in case, exception was thrown while reading the request if (!$request->getHeaderCollection() instanceof HeaderCollectionInterface) { $request->setHeaderCollection(new HeaderCollection()); } foreach ($this->responseHeaderModifier as $modifier) { $modifier->modify($response->getHeaderCollection(), $request); } $this->statusCodeWriter->writeResponseStatusCode($response->getStatusCode()); $this->headerWriter->writeResponseHeader($response->getHeaderCollection()); $content = $response->getContent(); $requestedContentType = $request->getHeaderCollection()->getHeaderValue(HeaderName::ACCEPT, ContentType::APPLICATION_JSON); $this->contentWriter->writeResponseContent($content, $requestedContentType); }