fromParts() public static method

Creates a URI from a hash of parse_url components.
public static fromParts ( array $parts ) : Psr\Http\Message\UriInterface
$parts array
return Psr\Http\Message\UriInterface
Exemplo n.º 1
0
 /**
  * {@inheritdoc}
  */
 public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property)
 {
     // Only apply to a full URL.
     if (is_string($value) && strpos($value, '://') > 0) {
         // URL encode everything after the hostname.
         $parsed_url = parse_url($value);
         // Fail on seriously malformed URLs.
         if ($parsed_url === FALSE) {
             throw new MigrateException("Value '{$value}' is not a valid URL");
         }
         // Iterate over specific pieces of the URL rawurlencoding each one.
         $url_parts_to_encode = array('path', 'query', 'fragment');
         foreach ($parsed_url as $parsed_url_key => $parsed_url_value) {
             if (in_array($parsed_url_key, $url_parts_to_encode)) {
                 // urlencode() would convert spaces to + signs.
                 $urlencoded_parsed_url_value = rawurlencode($parsed_url_value);
                 // Restore special characters depending on which part of the URL this is.
                 switch ($parsed_url_key) {
                     case 'query':
                         $urlencoded_parsed_url_value = str_replace('%26', '&', $urlencoded_parsed_url_value);
                         break;
                     case 'path':
                         $urlencoded_parsed_url_value = str_replace('%2F', '/', $urlencoded_parsed_url_value);
                         break;
                 }
                 $parsed_url[$parsed_url_key] = $urlencoded_parsed_url_value;
             }
         }
         $value = (string) Uri::fromParts($parsed_url);
     }
     return $value;
 }
Exemplo n.º 2
0
 /**
  * Generate a base string for a HMAC-SHA1 signature
  * based on the given a url, method, and any parameters.
  *
  * @param Url    $url
  * @param string $method
  * @param array  $parameters
  *
  * @return string
  */
 protected function baseString(Uri $url, $method = 'POST', array $parameters = array())
 {
     $baseString = rawurlencode($method) . '&';
     $schemeHostPath = Uri::fromParts(array('scheme' => $url->getScheme(), 'host' => $url->getHost(), 'path' => $url->getPath()));
     $baseString .= rawurlencode($schemeHostPath) . '&';
     $data = array();
     parse_str($url->getQuery(), $query);
     $data = array_merge($query, $parameters);
     // normalize data key/values
     array_walk_recursive($data, function (&$key, &$value) {
         $key = rawurlencode(rawurldecode($key));
         $value = rawurlencode(rawurldecode($value));
     });
     ksort($data);
     $baseString .= $this->queryStringFromData($data);
     return $baseString;
 }
Exemplo n.º 3
0
 /**
  * @param array  $params
  * @param string $username
  * @param string $password
  * @param array  $driverOptions
  *
  * @throws HttpException
  */
 public function __construct(array $params, $username, $password, array $driverOptions = array())
 {
     if (!class_exists('GuzzleHttp\\Client')) {
         $message = sprintf('The socket connection requires a package "%s" but was not installed.', static::PACKAGE);
         throw new HttpException($message);
     }
     if (empty($params['host']) or empty($params['port']) or empty($params['dbname']) or empty($params['user']) or empty($params['password'])) {
         throw new HttpException('Host, port, dbname, user and password name must be configured.');
     }
     $this->hostname = $params['host'];
     $this->port = $params['port'];
     $this->username = $params['user'];
     $this->password = $params['password'];
     $this->database = $params['dbname'];
     $uri = Uri::fromParts(array('scheme' => 'http', 'host' => $this->hostname, 'port' => $this->port));
     $this->connection = new BaseConnection(array('base_uri' => $uri, 'handler' => HandlerStack::create(), 'cookies' => new CookieJar()));
     $this->connect();
 }
Exemplo n.º 4
0
 /**
  * Build a URI object for the Request.
  *
  * @param \Glassdoor\Action\ActionInterface $action
  *   Action to send to API.
  *
  * @return \GuzzleHttp\Psr7\Uri
  *   Uri object.
  */
 private function buildUri(ActionInterface $action)
 {
     $parts = parse_url($this->config->getBaseUrl());
     $params = $action->getParams();
     $params['v'] = $action->getVersion();
     $params['format'] = $this->config->getResponseFormat();
     $params['t.p'] = $this->config->getPartnerId();
     $params['t.k'] = $this->config->getPartnerKey();
     $params['userip'] = $this->config->getUserIp();
     $params['useragent'] = $this->config->getUserAgent();
     $params['action'] = $action->action();
     // Allow any overrides.
     if (!empty($parts['query'])) {
         parse_str($parts['query'], $query_params);
         array_merge($params, $query_params);
     }
     $parts['query'] = http_build_query($params);
     return Uri::fromParts($parts);
 }
Exemplo n.º 5
0
 protected function parseConnections($options, $defaultHost, $defaultPort)
 {
     if (isset($options['host']) || isset($options['port'])) {
         $options['connections'][] = $options;
     } elseif ($options['connection']) {
         $options['connections'][] = $options['connection'];
     }
     /** @var ParameterBag[] $toParse */
     $toParse = [];
     if (isset($options['connections'])) {
         foreach ((array) $options['connections'] as $alias => $conn) {
             if (is_string($conn)) {
                 $conn = ['host' => $conn];
             }
             $conn += ['scheme' => 'tcp', 'host' => $defaultHost, 'port' => $defaultPort];
             $conn = new ParameterBag($conn);
             if ($conn->has('password')) {
                 $conn->set('pass', $conn->get('password'));
                 $conn->remove('password');
             }
             $conn->set('uri', Uri::fromParts($conn->all()));
             $toParse[] = $conn;
         }
     } elseif (isset($options['save_path'])) {
         foreach (explode(',', $options['save_path']) as $conn) {
             $uri = new Uri($conn);
             $connBag = new ParameterBag();
             $connBag->set('uri', $uri);
             $connBag->add(parse_query($uri->getQuery()));
             $toParse[] = $connBag;
         }
     }
     $connections = [];
     foreach ($toParse as $conn) {
         /** @var Uri $uri */
         $uri = $conn->get('uri');
         $parts = explode(':', $uri->getUserInfo(), 2);
         $password = isset($parts[1]) ? $parts[1] : null;
         $connections[] = ['scheme' => $uri->getScheme(), 'host' => $uri->getHost(), 'port' => $uri->getPort(), 'path' => $uri->getPath(), 'alias' => $conn->get('alias'), 'prefix' => $conn->get('prefix'), 'password' => $password, 'database' => $conn->get('database'), 'persistent' => $conn->get('persistent'), 'weight' => $conn->get('weight'), 'timeout' => $conn->get('timeout')];
     }
     return $connections;
 }
Exemplo n.º 6
0
 protected function parseConnections($options, $defaultHost, $defaultPort, $defaultScheme = 'tcp')
 {
     if (isset($options['host']) || isset($options['port'])) {
         $options['connections'][] = $options;
     } elseif ($options['connection']) {
         $options['connections'][] = $options['connection'];
     }
     /** @var ParameterBag[] $toParse */
     $toParse = [];
     if (isset($options['connections'])) {
         foreach ((array) $options['connections'] as $alias => $conn) {
             if (is_string($conn)) {
                 $conn = ['host' => $conn];
             }
             $conn += ['scheme' => $defaultScheme, 'host' => $defaultHost, 'port' => $defaultPort];
             $conn = new ParameterBag($conn);
             if ($conn->has('password')) {
                 $conn->set('pass', $conn->get('password'));
                 $conn->remove('password');
             }
             $conn->set('uri', Uri::fromParts($conn->all()));
             $toParse[] = $conn;
         }
     } else {
         $connections = isset($options['save_path']) ? (array) explode(',', $options['save_path']) : [];
         if (empty($connections)) {
             $connections[] = $defaultHost . ':' . $defaultPort;
         }
         foreach ($connections as $conn) {
             // Default scheme if not given so parse_url works correctly.
             if (!preg_match('~^\\w+://.+~', $conn)) {
                 $conn = $defaultScheme . '://' . $conn;
             }
             $uri = new Uri($conn);
             $connBag = new ParameterBag();
             $connBag->set('uri', $uri);
             $connBag->add(Psr7\parse_query($uri->getQuery()));
             $toParse[] = $connBag;
         }
     }
     $connections = [];
     foreach ($toParse as $conn) {
         /** @var Uri $uri */
         $uri = $conn->get('uri');
         $parts = explode(':', $uri->getUserInfo(), 2);
         $password = isset($parts[1]) ? $parts[1] : null;
         $connections[] = ['scheme' => $uri->getScheme(), 'host' => $uri->getHost(), 'port' => $uri->getPort(), 'path' => $uri->getPath(), 'alias' => $conn->get('alias'), 'prefix' => $conn->get('prefix'), 'password' => $password, 'database' => $conn->get('database'), 'persistent' => $conn->get('persistent'), 'weight' => $conn->get('weight'), 'timeout' => $conn->get('timeout')];
     }
     return $connections;
 }
Exemplo n.º 7
0
 /**
  * Builds the base url for the guzzle connection.
  *
  * @param \Elastica\Connection $connection
  *
  * @return string
  */
 protected function _getBaseUrl(Connection $connection)
 {
     // If url is set, url is taken. Otherwise port, host and path
     $url = $connection->hasConfig('url') ? $connection->getConfig('url') : '';
     if (!empty($url)) {
         $baseUri = $url;
     } else {
         $baseUri = (string) Uri::fromParts(['scheme' => $this->_scheme, 'host' => $connection->getHost(), 'port' => $connection->getPort(), 'path' => ltrim('/', $connection->getPath())]);
     }
     return rtrim($baseUri, '/');
 }
Exemplo n.º 8
0
 public function testDoesNotAddSlashWhenPathIsEmpty()
 {
     $uri = Uri::fromParts(['scheme' => 'http', 'host' => 'example.com', 'path' => '', 'query' => 'foo']);
     $this->assertEquals('', $uri->getPath());
     $this->assertEquals('http://example.com?foo', (string) $uri);
 }
 private function transformToUploadUrl()
 {
     $parts = parse_url((string) $this->request->getUri());
     if (!isset($parts['path'])) {
         $parts['path'] = '';
     }
     $parts['path'] = '/upload' . $parts['path'];
     $uri = Uri::fromParts($parts);
     $this->request = $this->request->withUri($uri);
 }
Exemplo n.º 10
0
 /**
  * @dataProvider getValidUris
  */
 public function testFromParts($input)
 {
     $uri = Uri::fromParts(parse_url($input));
     $this->assertSame($input, (string) $uri);
 }
Exemplo n.º 11
0
 /**
  * @param string $rawPath
  * @param array $query
  * @param array $options
  *
  * @return Uri
  *
  * @throws \Exception
  */
 protected function resolveUri($rawPath, array $query, array $options)
 {
     $query = array_filter($query);
     $matches = [];
     preg_match_all('/[\\{]{1}([\\w]+)[\\}]{1}/', $rawPath, $matches);
     $requiredOptions = $matches[1];
     foreach ($requiredOptions as $option) {
         if (!array_key_exists($option, $options)) {
             throw new \Exception('Missing option ' . $option);
         }
         $rawPath = str_replace('{' . $option . '}', $options[$option], $rawPath);
     }
     $uri = Uri::fromParts(['scheme' => $this->clientDescription->getSchema(), 'host' => $this->clientDescription->getHost(), 'path' => $rawPath]);
     foreach ($query as $name => $value) {
         $uri = Uri::withQueryValue($uri, $name, $value);
     }
     return $uri;
 }
Exemplo n.º 12
0
 /**
  * @param string $baseUrl
  */
 public function setBaseUrl($baseUrl)
 {
     $this->baseUrl = Uri::fromParts(parse_url($baseUrl));
 }