Provides a value object representing a URI for HTTP requests.
Instances of this class are considered immutable; all methods that
might change state are implemented such that they retain the internal
state of the current instance and return a new instance that contains the
changed state.
/** * Forward the request to the target url and return the response. * * @param string $target * @throws UnexpectedValueException * @return Response */ public function to($target) { if (is_null($this->request)) { throw new UnexpectedValueException('Missing request instance.'); } $target = new Uri($target); // Overwrite target scheme and host. $uri = $this->request->getUri()->withScheme($target->getScheme())->withHost($target->getHost()); // Check for custom port. if ($port = $target->getPort()) { $uri = $uri->withPort($port); } // Check for subdirectory. if ($path = $target->getPath()) { $uri = $uri->withPath(rtrim($path, '/') . '/' . ltrim($uri->getPath(), '/')); } $request = $this->request->withUri($uri); $stack = $this->filters; $stack[] = function (RequestInterface $request, ResponseInterface $response, callable $next) { $response = $this->adapter->send($request); return $next($request, $response); }; $relay = (new RelayBuilder())->newInstance($stack); return $relay($request, new Response()); }
public function __invoke(Route $route, Console $console) : int { $basePath = $route->getMatchedParam('path'); $path = realpath($basePath) . '/data/blog'; $cache = realpath($basePath) . '/data/cache/posts'; $baseUri = new Uri('https://mwop.net'); $middleware = $this->blogMiddleware; $console->writeLine('Generating static cache for blog posts', Color::GREEN); // Prepare final handler for middleware $failed = false; $done = function ($req, $res, $err = null) use(&$failed) { $failed = $err ? true : false; }; $parser = new Parser(null, new CommonMarkParser()); foreach (new MarkdownFileFilter($path) as $fileInfo) { $document = $parser->parse(file_get_contents($fileInfo->getPathname())); $metadata = $document->getYAML(); $message = ' ' . $metadata['id']; $length = strlen($message); $width = $console->getWidth(); $console->write($message, Color::BLUE); $canonical = $baseUri->withPath(sprintf('/blog/%s.html', $metadata['id'])); $request = (new Request(new PsrRequest([], [], $canonical, 'GET')))->withUri($canonical)->withAttribute('id', $metadata['id']); $failed = false; $response = $middleware($request, new Response(), $done); if (!$failed) { $this->cacheResponse($metadata['id'], $cache, $response->getBody()); } $this->reportComplete($console, $width, $length, !$failed); } $console->writeLine('ALL DONE', Color::GREEN); return 0; }
public function __invoke($route, $console) { $appId = $route->getMatchedParam('appId'); $site = $route->getMatchedParam('site'); $uri = new Uri(trim($site, '/') . '/'); $port = $uri->getPort(); if (!$port) { $port = $uri->getScheme() == 'https' ? 443 : 80; } $rules = file_get_contents($this->dist); $rules = str_replace('%SCHEME%', $uri->getScheme(), $rules); $rules = str_replace('%HOST%', $uri->getHost(), $rules); $rules = str_replace('%PORT%', $port, $rules); $rules = str_replace('%APPID%', $appId, $rules); file_put_contents($this->xml, $rules); return 0; }
/** * Takes a path and returns an absolute path. * * This method is implemented in the way that browsers work, see * https://url.spec.whatwg.org/#relative-state for more information about the * possible cases. * * @param string $path * A path from the internal browser content. * * @return string * The $path with $base_url prepended, if necessary. */ protected function getAbsoluteUrl($path) { global $base_url, $base_path; $parts = parse_url($path); // In case the $path has a host, it is already an absolute URL and we are // done. if (!empty($parts['host'])) { return $path; } // In case the $path contains just a query, we turn it into an absolute URL // with the same scheme, host and path, see // https://url.spec.whatwg.org/#relative-state. if (array_keys($parts) === ['query']) { $current_uri = new Uri($this->getUrl()); return (string) $current_uri->withQuery($parts['query']); } if (empty($parts['host'])) { // Ensure that we have a string (and no xpath object). $path = (string) $path; // Strip $base_path, if existent. $length = strlen($base_path); if (substr($path, 0, $length) === $base_path) { $path = substr($path, $length); } // Ensure that we have an absolute path. if (empty($path) || $path[0] !== '/') { $path = '/' . $path; } // Finally, prepend the $base_url. $path = $base_url . $path; } return $path; }