public function execute(Command $command, string $grammarPath) : SimpleXMLElement { $uri = $this->uri->withPath($grammarPath); $response = $this->httpClient->sendRequest($this->buildRequest($command, $uri)); if (200 !== (int) $response->getStatusCode()) { throw InvalidResponseException::fromUnsuccessfulResponse($response); } $previousValue = libxml_use_internal_errors(true); $xml = simplexml_load_string((string) $response->getBody()); libxml_use_internal_errors($previousValue); if (false === $xml) { throw InvalidResponseException::fromXmlError(libxml_get_last_error()); } return $xml; }
public function setBase(UriInterface $uri) { $base = (string) $uri->withPath('')->withQuery('')->withFragment(''); if ($base and $base !== $this->base) { $this->base = $base; } }
public function updateBaseUri(UriInterface $uri) { $base = (string) $uri->withPath('')->withQuery('')->withFragment(''); $oldBase = (string) $this->getClient()->getConfig('base_uri'); if ($base and $base !== $oldBase) { $this->client = new Client(['base_uri' => $base]); } }
/** * @param UriInterface $uri * @return UriInterface * @throws \ErrorException */ protected function forward_aliases(UriInterface $uri) { // strip beginning / $path = substr($uri->getPath(), 1); $aliases = $this->db->conn()->prepare(<<<SQL SELECT `alias_go`, `alias_forward_to` FROM `{$this->db->getPrefix()}aliases` WHERE `alias_active` = 1 AND `alias_go` = ? SQL ); $aliases->execute(array($path)); $aliases = $aliases->fetchAll(\PDO::FETCH_ASSOC); foreach ($aliases as $alias) { if ($path == $alias['alias_go']) { $path = $alias['alias_forward_to']; } } return $uri->withPath('/' . $path); }
/** * Converts the relative URI into a new URI that is resolved against the base URI. * * @param UriInterface $base Base URI * @param UriInterface $rel Relative URI * * @return UriInterface * @link http://tools.ietf.org/html/rfc3986#section-5.2 */ public static function resolve(UriInterface $base, UriInterface $rel) { if ((string) $rel === '') { // we can simply return the same base URI instance for this same-document reference return $base; } if ($rel->getScheme() != '') { return $rel->withPath(self::removeDotSegments($rel->getPath())); } if ($rel->getAuthority() != '') { $targetAuthority = $rel->getAuthority(); $targetPath = self::removeDotSegments($rel->getPath()); $targetQuery = $rel->getQuery(); } else { $targetAuthority = $base->getAuthority(); if ($rel->getPath() === '') { $targetPath = $base->getPath(); $targetQuery = $rel->getQuery() != '' ? $rel->getQuery() : $base->getQuery(); } else { if ($rel->getPath()[0] === '/') { $targetPath = $rel->getPath(); } else { if ($targetAuthority != '' && $base->getPath() === '') { $targetPath = '/' . $rel->getPath(); } else { $lastSlashPos = strrpos($base->getPath(), '/'); if ($lastSlashPos === false) { $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())); }
private static function decodeUnreservedCharacters(UriInterface $uri) { $regex = '/%(?:2D|2E|5F|7E|3[0-9]|[46][1-9A-F]|[57][0-9A])/i'; $callback = function (array $match) { return rawurldecode($match[0]); }; return $uri->withPath(preg_replace_callback($regex, $callback, $uri->getPath()))->withQuery(preg_replace_callback($regex, $callback, $uri->getQuery())); }
/** * Resolve a base URI with a relative URI and return a new URI. * * @param UriInterface $base Base URI * @param UriInterface $rel Relative URI * * @return UriInterface */ public static function resolve(UriInterface $base, UriInterface $rel) { // Return the relative uri as-is if it has a scheme. if ($rel->getScheme()) { return $rel->withPath(static::removeDotSegments($rel->getPath())); } $relParts = ['scheme' => $rel->getScheme(), 'authority' => $rel->getAuthority(), 'path' => $rel->getPath(), 'query' => $rel->getQuery(), 'fragment' => $rel->getFragment()]; $parts = ['scheme' => $base->getScheme(), 'authority' => $base->getAuthority(), 'path' => $base->getPath(), 'query' => $base->getQuery(), 'fragment' => $base->getFragment()]; if (!empty($relParts['authority'])) { $parts['authority'] = $relParts['authority']; $parts['path'] = self::removeDotSegments($relParts['path']); $parts['query'] = $relParts['query']; $parts['fragment'] = $relParts['fragment']; } elseif (!empty($relParts['path'])) { if (substr($relParts['path'], 0, 1) == '/') { $parts['path'] = self::removeDotSegments($relParts['path']); $parts['query'] = $relParts['query']; $parts['fragment'] = $relParts['fragment']; } else { if (!empty($parts['authority']) && empty($parts['path'])) { $mergedPath = '/'; } else { $mergedPath = substr($parts['path'], 0, strrpos($parts['path'], '/') + 1); } $parts['path'] = self::removeDotSegments($mergedPath . $relParts['path']); $parts['query'] = $relParts['query']; $parts['fragment'] = $relParts['fragment']; } } elseif (!empty($relParts['query'])) { $parts['query'] = $relParts['query']; } elseif ($relParts['fragment']) { $parts['fragment'] = $relParts['fragment']; } return new Uri(static::createUriString($parts['scheme'], $parts['authority'], $parts['path'], $parts['query'], $parts['fragment'])); }
/** * Create a complete API Uri from the Base Uri, path and query parameters. * * Example: * Given a base uri that equal http://domain.tld * Given the following parameters /pets/{id}, ['id' => 1], ['foo' => 'bar'] * Then the Uri will equal to http://domain.tld/pets/1?foo=bar * * @param string $pathTemplate A template path * @param array $pathParameters Path parameters * @param array $queryParameters Query parameters * * @return UriInterface */ private function buildRequestUri($pathTemplate, array $pathParameters, array $queryParameters) { $path = $this->uriTemplate->expand($pathTemplate, $pathParameters); $query = http_build_query($queryParameters); return $this->baseUri->withPath($path)->withQuery($query); }
/** * Create an UriInterface with the request URI information from `$server`. It uses `$server['REQUEST_URI']` for * this information. * * @param UriInterface $uri * @param array $server * * @return UriInterface */ private function withPath(UriInterface $uri, array $server) : UriInterface { if (isset($server['REQUEST_URI'])) { return $uri->withPath(\current(\explode('?', $server['REQUEST_URI'], 2))); } return $uri; }