/** * index * * @param ServerRequestInterface $request * @param ResponseInterface $response * @param callable $next * @return ResponseInterface */ public function indexAction(ServerRequestInterface $request, ResponseInterface $response, callable $next) { $name = $request->getAttribute("name", "Unknown"); $template = new Template("layout.phtml"); $response->getBody()->write($template->render(["name" => $name])); return $next($request, $response); }
/** * {@inheritdoc} * * @throws PreconditionFailedException */ public function verify(ServerRequestInterface $request, HttpResource $definition) { if ((string) $request->method() !== MethodInterface::GET && $request->headers()->has('Range')) { throw new PreconditionFailedException(); } if (!$definition->isRangeable() && $request->headers()->has('Range')) { throw new PreconditionFailedException(); } }
/** * {@inheritdoc} */ public function build(SetInterface $identities, ServerRequestInterface $request, HttpResource $definition, SpecificationInterface $specification = null, Range $range = null) : MapInterface { $map = new Map('string', HeaderInterface::class); if ($identities->size() === 0) { return $map; } $path = $request->url()->path(); return $map->put('Link', new Link($identities->reduce(new Set(HeaderValueInterface::class), function (Set $carry, IdentityInterface $identity) use($path) : Set { return $carry->add(new LinkValue(Url::fromString(rtrim((string) $path, '/') . '/' . $identity), 'resource', new Map('string', ParameterInterface::class))); }))); }
/** * {@inheritdoc} * * @throws NotAcceptableException */ public function verify(ServerRequestInterface $request, HttpResource $definition) { $types = $this->formats->mediaTypes()->reduce([], function (array $carry, MediaType $type) { $carry[] = (string) $type; return $carry; }); $best = $this->negotiator->getBest((string) $request->headers()->get('Accept')->values()->join(', '), $types); if (!$best instanceof Accept) { throw new NotAcceptableException(); } }
/** * adds the session to the request arguments * sets userAgent and clientIp * * @param ServerRequestInterface $request * @param ResponseInterface $response * @param callable $next * @return ResponseInterface */ public function init(ServerRequestInterface $request, ResponseInterface $response, callable $next) { $server = $request->getServerParams(); $this->userAgent = $server["HTTP_USER_AGENT"] ?? "-"; $ip = $server["HTTP_X_FORWARDED_FOR"] ?? $server["REMOTE_ADDR"]; if (strpos($ip, ",") !== false) { $ip = substr($ip, 0, strpos($ip, ",")); } $this->clientIp = isset($ip[0]) ? $ip : null; return $next($request->withAttribute(self::REQUEST_ATTRIBUTE, $this), $response); }
/** * adds the database to the request attributes * * @param ServerRequestInterface $request * @param ResponseInterface $response * @param callable $next * @return ResponseInterface */ public function init(ServerRequestInterface $request, ResponseInterface $response, callable $next) { return $next($request->withAttribute(self::REQUEST_ATTRIBUTE, $this), $response); }
/** * @return array */ public function headers() : array { return $this->wrapped->getHeaders(); }
/** * Finds route which matches with the request. * * @param \Psr\Http\Message\RequestInterface $request * @param array $attributes Matched route attributes * * @throws \Elegant\Http\Exception\MethodNotAllowedException * @throws \Elegant\Http\Exception\NotFoundException * * @return \Elegant\Http\Route\Route */ public final function findByRequest(ServerRequestInterface $request) { $routes = $this->gets(); $uri = $request->getUri(); foreach ($routes as $route) { if ($route->matchesWithRequest($request, $attrs)) { if ('OPTIONS' === $request->getMethod()) { return $this->createOptions($uri->getPath(), $this->collectAllowedMethods($uri)); } $attributes = $attrs; return $route; } } if ($allowedMethods = $this->collectAllowedMethods($uri)) { throw new MethodNotAllowedException($allowedMethods); } throw new NotFoundException(); }
/** * @param Formats $formats * @param ServerRequestInterface $request * * @return MapInterface<string, HeaderInterface> */ private function buildHeaderFrom(Formats $formats, ServerRequestInterface $request) : MapInterface { $map = new Map('string', HeaderInterface::class); $format = $formats->matching((string) $request->headers()->get('Accept')->values()->join(', ')); return $map->put('Content-Type', new ContentType(new ContentTypeValue($format->preferredMediaType()->topLevel(), $format->preferredMediaType()->subType(), new Map('string', ParameterInterface::class)))); }
/** * {@inheritdoc} */ public function build(IdentityInterface $identity, ServerRequestInterface $request, HttpResource $definition, HttpResourceInterface $resource) : MapInterface { $map = new Map('string', HeaderInterface::class); return $map->put('Location', new Location(new LocationValue(Url::fromString(rtrim((string) $request->url()->path(), '/') . '/' . $identity)))); }