public function addLanguage(ServerRequestInterface $request, $language) { $path = $request->getUri()->getPath(); if ($language !== $this->environment->getDefaultLanguage()) { $path = '/' . $language . ($path === '/' ? '' : $path); } return $request->withUri($request->getUri()->withPath($path)); }
/** * Guesses the base URI of the application. * * @param Psr\Http\Message\ServerRequestInterface $request * @return Psr\Http\Message\ServerRequestInterface */ public static function guess(\Psr\Http\Message\ServerRequestInterface $request) { $server = $request->getServerParams(); $basename = basename($server['SCRIPT_FILENAME']); $position = strpos($server['SCRIPT_NAME'], $basename) - 1; $rootUri = substr($server['SCRIPT_NAME'], 0, $position); $oldPath = $request->getUri()->getPath(); $newPath = str_replace($rootUri, '', $oldPath); $uri = $request->getUri()->withPath($newPath); return $request->withUri($uri); }
/** * Updates the request URI to remove the base directory. * * @param string $base The base path to remove. * @param \Psr\Http\Message\ServerRequestInterface $request The request to modify. * @return \Psr\Http\Message\ServerRequestInterface The modified request. */ protected static function updatePath($base, $request) { $uri = $request->getUri(); $path = $uri->getPath(); if (strlen($base) > 0 && strpos($path, $base) === 0) { $path = substr($path, strlen($base)); } if (empty($path) || $path === '/' || $path === '//' || $path === '/index.php') { $path = '/'; } $endsWithIndex = '/webroot/index.php'; $endsWithLength = strlen($endsWithIndex); if (strlen($path) >= $endsWithLength && substr($path, -$endsWithLength) === $endsWithIndex) { $path = '/'; } return $request->withUri($uri->withPath($path)); }
/** * @return \Psr\Http\Message\RequestInterface */ public function convertToProxiedRequest() { $proxiedUri = $this->request->getUri()->withScheme($this->proxyUri->getScheme())->withHost($this->proxyUri->getHost())->withPath(rtrim($this->proxyUri->getPath(), '/') . $this->request->getUri()->getPath()); if ($this->proxyUri->getPort() != null) { $proxiedUri = $proxiedUri->withPort($this->proxyUri->getPort()); } else { $proxiedUri = $proxiedUri->withPort($proxiedUri->getScheme() === "https" ? 443 : 80); } $proxiedRequest = $this->request->withUri($proxiedUri)->withHeader("host", $this->proxyUri->getHost()); if ($proxiedRequest->getBody()->getSize() === null && $proxiedRequest->getHeaderLine("User-Agent") == "Http_TestCase/1.0") { // FIXME // Prevents an incompatibility with PHP-VCR : // Without this the stream size is null and not 0 so CurlFactory#applyBody is applied and it sets a // CURLOPT_READFUNCTION on the request, but not a CURLOPT_INFILESIZE ; which makes VCR fails. // See https://github.com/guzzle/guzzle/commit/0a3065ea4639c1df8b9220bc8ca3fb529d7f8b52#commitcomment-12551295 $proxiedRequest = $proxiedRequest->withBody(\GuzzleHttp\Psr7\stream_for()); } return $proxiedRequest; }
/** * {@inheritdoc} */ public function withUri(UriInterface $uri, $preserveHost = false) { return new static($this->wrapped->withUri($uri, $preserveHost)); }
/** * {@inheritDoc} */ public function withUri(UriInterface $uri, $preserveHost = false) { return new self($this->app, $this->psrRequest->withUri($uri, $preserveHost)); }
/** * Execute the middleware. * * @param ServerRequestInterface $request * @param ResponseInterface $response * @param callable $next * * @return ResponseInterface */ public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) { return $next($request->withUri(self::getRealUri($request), true), $response); }
/** * Strip the route from the request path * * @param ServerRequestInterface $request * @param string $route * @return ServerRequestInterface */ private function stripRouteFromPath(ServerRequestInterface $request, $route) { $this->removed = $route; $uri = $request->getUri(); $path = $this->getTruncatedPath($route, $uri->getPath()); $new = $uri->withPath($path); // Root path of route is treated differently if ($path === '/' && '/' === substr($uri->getPath(), -1)) { $this->removed .= '/'; } return $request->withUri($new); }
/** * Allow mutating the URI * * {@inheritdoc} */ public function withUri(UriInterface $uri, $preserveHost = false) { $new = $this->psrRequest->withUri($uri, $preserveHost); return new self($new, $this->originalRequest); }
/** * Filters the request base URI to filter development and debugging stuff. * * @param Request $request Request to be filtered * @param array|\stdClass $filters ArrayAccess filters * * @author Benjamin Carl <*****@*****.**> * * @return Request Filtered and prepared request instance * @static */ protected static function filterRequest(Request $request, $filters) { $tmp = []; foreach ($filters as $filter) { $tmp[] = $filter; } $filter = new \Doozr_Filter($tmp); return $request->withUri($request->getUri()->withPath($filter->apply($request->getUri()->getPath()))); }
/** * @inheritDoc */ public function withUri(UriInterface $uri, $preserveHost = false) { $self = clone $this; $self->serverRequest = $this->serverRequest->withUri($uri, $preserveHost); return $self; }