Zend\Diactoros\Uri::withPath PHP Метод

withPath() публичный Метод

public withPath ( $path )
    public function withPath($path)
    {
        if (!is_string($path)) {
            throw new InvalidArgumentException('Invalid path provided; must be a string');
        }
        if (strpos($path, '?') !== false) {
            throw new InvalidArgumentException('Invalid path provided; must not contain a query string');
        }
        if (strpos($path, '#') !== false) {
            throw new InvalidArgumentException('Invalid path provided; must not contain a URI fragment');
        }
        $path = $this->filterPath($path);
        if ($path === $this->path) {
            // Do nothing if no change was made.
            return clone $this;
        }
        $new = clone $this;
        $new->path = $path;
        return $new;
    }

Usage Example

Пример #1
0
 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;
 }