Uri example

/** * Set the appropriate URL on the request based on the location header. */
    private static function redirectUri(
        RequestInterface $request,
        ResponseInterface $response,
        array $protocols
    ): UriInterface {
        $location = Psr7\UriResolver::resolve(
            $request->getUri(),
            new Psr7\Uri($response->getHeaderLine('Location'))
        );

        // Ensure that the redirect URI is allowed based on the protocols.         if (!\in_array($location->getScheme()$protocols)) {
            throw new BadResponseException(\sprintf('Redirect URI, %s, does not use one of the allowed redirect protocols: %s', $location, \implode(', ', $protocols))$request$response);
        }

        return $location;
    }
}
$data[] = [new BooleanData(DataDefinition::create('boolean')), 'test', FALSE];
    $data[] = [new FloatData(DataDefinition::create('float')), 1.5, TRUE];
    $data[] = [new FloatData(DataDefinition::create('float')), 'test', FALSE];
    $data[] = [new IntegerData(DataDefinition::create('integer')), 1, TRUE];
    $data[] = [new IntegerData(DataDefinition::create('integer')), 1.5, FALSE];
    $data[] = [new IntegerData(DataDefinition::create('integer')), 'test', FALSE];
    $data[] = [new StringData(DataDefinition::create('string')), 'test', TRUE];
    $data[] = [new StringData(DataDefinition::create('string'))new TranslatableMarkup('test'), TRUE];
    // It is odd that 1 is a valid string.     // $data[] = [$this->createMock('Drupal\Core\TypedData\Type\StringInterface'), 1, FALSE];     $data[] = [new StringData(DataDefinition::create('string'))[], FALSE];
    $data[] = [new Uri(DataDefinition::create('uri')), 'http://www.example.com', TRUE];
    $data[] = [new Uri(DataDefinition::create('uri')), 'https://www.example.com', TRUE];
    $data[] = [new Uri(DataDefinition::create('uri')), 'Invalid', FALSE];
    $data[] = [new Uri(DataDefinition::create('uri')), 'entity:node/1', TRUE];
    $data[] = [new Uri(DataDefinition::create('uri')), 'base:', TRUE];
    $data[] = [new Uri(DataDefinition::create('uri')), 'base:node', TRUE];
    $data[] = [new Uri(DataDefinition::create('uri')), 'internal:', TRUE];
    $data[] = [new Uri(DataDefinition::create('uri')), 'public://', FALSE];
    $data[] = [new Uri(DataDefinition::create('uri')), 'public://foo.png', TRUE];
    $data[] = [new Uri(DataDefinition::create('uri')), 'private://', FALSE];
    $data[] = [new Uri(DataDefinition::create('uri')), 'private://foo.png', TRUE];
    $data[] = [new Uri(DataDefinition::create('uri')), 'example.com', FALSE];

    

    public function __construct(
        string $method,
        $uri,
        array $headers = [],
        $body = null,
        string $version = '1.1'
    ) {
        $this->assertMethod($method);
        if (!($uri instanceof UriInterface)) {
            $uri = new Uri($uri);
        }

        $this->method = strtoupper($method);
        $this->uri = $uri;
        $this->setHeaders($headers);
        $this->protocol = $version;

        if (!isset($this->headerNames['host'])) {
            $this->updateHostFromUri();
        }

        
$this->assertCount(0, $this->factory->createRequest($serverRequest)->request);
    }

    public function testCreateRequestWithUri()
    {
        $serverRequest = new ServerRequest(
            '1.1',
            [],
            new Stream(),
            '/',
            'GET',
            new Uri('http://les-tilleuls.coop/about/kevin'),
            [],
            [],
            [],
            [],
            null,
            []
        );

        $this->assertEquals('/about/kevin', $this->factory->createRequest($serverRequest)->getPathInfo());
    }

    

        return new Response($code[], null, '1.1', $reasonPhrase);
    }

    public function createRequest(string $method$uri): RequestInterface
    {
        return new Request($method$uri);
    }

    public function createUri(string $uri = ''): UriInterface
    {
        return new Uri($uri);
    }
}

    public static function uriFor($uri): UriInterface
    {
        if ($uri instanceof UriInterface) {
            return $uri;
        }

        if (is_string($uri)) {
            return new Uri($uri);
        }

        throw new \InvalidArgumentException('URI must be a string or UriInterface');
    }
}
/** * HTTP middleware that replaces request endpoint for a test one. */
  public function __invoke(): \Closure {
    return function D$handler) {
      return function DRequestInterface $request, array $options) use ($handler): PromiseInterface {
        $test_end_point = \Drupal::state()->get('announce_test_endpoint');
        if ($test_end_point && str_contains($request->getUri(), '://www.drupal.org/announcements.json')) {
          // Only override $uri if it matches the advisories JSON feed to avoid           // changing any other uses of the 'http_client' service during tests with           // this module installed.           $request = $request->withUri(new Uri($test_end_point));
        }
        return $handler($request$options);
      };
    };
  }

  /** * Sets the test endpoint for the advisories JSON feed. * * @param string $test_endpoint * The test endpoint. */
UriInterface|string $uri = null,
        private readonly array $server = [],
        private readonly array $cookies = [],
        private readonly array $query = [],
        private readonly array $uploadedFiles = [],
        private readonly array|object|null $data = null,
        private readonly array $attributes = [],
    ) {
        parent::__construct($version$headers$body);

        if (!$uri instanceof UriInterface) {
            $uri = new Uri((string) $uri);
        }

        $this->uri = $uri;
    }

    public function getRequestTarget(): string
    {
        return $this->requestTarget;
    }

    public function withRequestTarget($requestTarget): never
    {
public function createUri(string $uri = ''): UriInterface
    {
        if ($this->responseFactory instanceof UriFactoryInterface) {
            return $this->responseFactory->createUri($uri);
        }

        if (class_exists(Psr17FactoryDiscovery::class)) {
            return Psr17FactoryDiscovery::findUrlFactory()->createUri($uri);
        }

        if (class_exists(Uri::class)) {
            return new Uri($uri);
        }

        throw new \LogicException(sprintf('You cannot use "%s()" as no PSR-17 factories have been found. Try running "composer require php-http/discovery psr/http-factory-implementation:*".', __METHOD__));
    }

    public function reset(): void
    {
        if ($this->client instanceof ResetInterface) {
            $this->client->reset();
        }
    }
}
/** * HTTP middleware that replaces URI for advisory requests. */
  public function __invoke() {
    return function D$handler) {
      return function DRequestInterface $request, array $options) use ($handler): PromiseInterface {
        $test_end_point = \Drupal::state()->get('advisories_test_endpoint');
        if ($test_end_point && str_contains($request->getUri(), '://updates.drupal.org/psa.json')) {
          // Only override $uri if it matches the advisories JSON feed to avoid           // changing any other uses of the 'http_client' service during tests with           // this module installed.           $request = $request->withUri(new Uri($test_end_point));
        }
        return $handler($request$options);
      };
    };
  }

  /** * Sets the test endpoint for the advisories JSON feed. * * @param string $test_endpoint * The test endpoint. * @param bool $delete_stored_response * Whether to delete stored feed response. */
$host = $parts['host'] ?? null;
        $port = $parts['port'] ?? null;

        return [$host$port];
    }

    /** * Get a Uri populated with values from $_SERVER. */
    public static function getUriFromGlobals(): UriInterface
    {
        $uri = new Uri('');

        $uri = $uri->withScheme(!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' ? 'https' : 'http');

        $hasPort = false;
        if (isset($_SERVER['HTTP_HOST'])) {
            [$host$port] = self::extractHostAndPortFromAuthority($_SERVER['HTTP_HOST']);
            if ($host !== null) {
                $uri = $uri->withHost($host);
            }

            if ($port !== null) {
                
$targetPath = $rel->getPath();
                        } else {
                            $targetPath = substr($base->getPath(), 0, $lastSlashPos + 1).$rel->getPath();
                        }
                    }
                }
                $targetPath = self::removeDotSegments($targetPath);
                $targetQuery = $rel->getQuery();
            }
        }

        return new Uri(Uri::composeComponents(
            $base->getScheme(),
            $targetAuthority,
            $targetPath,
            $targetQuery,
            $rel->getFragment()
        ));
    }

    /** * Returns the target URI as a relative reference from the base URI. * * This method is the counterpart to resolve(): * * (string) $target === (string) UriResolver::resolve($base, UriResolver::relativize($base, $target)) * * One use-case is to use the current request URI as base URI and then generate relative links in your documents * to reduce the document size or offer self-contained downloadable document archives. * * $base = new Uri('http://example.com/a/b/'); * echo UriResolver::relativize($base, new Uri('http://example.com/a/b/c')); // prints 'c'. * echo UriResolver::relativize($base, new Uri('http://example.com/a/x/y')); // prints '../x/y'. * echo UriResolver::relativize($base, new Uri('http://example.com/a/b/?q')); // prints '?q'. * echo UriResolver::relativize($base, new Uri('http://example.org/a/b/')); // prints '//example.org/a/b/'. * * This method also accepts a target that is already relative and will try to relativize it further. Only a * relative-path reference will be returned as-is. * * echo UriResolver::relativize($base, new Uri('/a/b/c')); // prints 'c' as well */


        if ($this->responseFactory instanceof UriFactoryInterface) {
            return $this->responseFactory->createUri($uri);
        }

        if (class_exists(Psr17FactoryDiscovery::class)) {
            return Psr17FactoryDiscovery::findUrlFactory()->createUri($uri);
        }

        if (class_exists(Uri::class)) {
            return new Uri($uri);
        }

        throw new \LogicException(sprintf('You cannot use "%s()" as no PSR-17 factories have been found. Try running "composer require php-http/discovery psr/http-factory-implementation:*".', __METHOD__));
    }

    public function __sleep(): array
    {
        throw new \BadMethodCallException('Cannot serialize '.__CLASS__);
    }

    public function __wakeup(): void
    {
Home | Imprint | This part of the site doesn't use cookies.