/**
  * @param string $specification
  * @return string
  */
 private function createUrlFromUri($specification)
 {
     preg_match('%^(?P<path>[^?#]*)(?:(?:\\?(?P<query>[^#]*))?(?:\\#(?P<fragment>.*))?)$%', (string) $specification, $matches);
     $path = $matches['path'];
     $query = isset($matches['query']) ? $matches['query'] : '';
     $fragment = isset($matches['fragment']) ? $matches['fragment'] : '';
     $uri = $this->uri->withQuery('')->withFragment('');
     // Relative path
     if (!empty($path) && '/' !== $path[0]) {
         $path = rtrim($this->uri->getPath(), '/') . '/' . $path;
     }
     // Path present; set on URI
     if (!empty($path)) {
         $uri = $uri->withPath($path);
     }
     // Query present; set on URI
     if (!empty($query)) {
         $uri = $uri->withQuery($query);
     }
     // Fragment present; set on URI
     if (!empty($fragment)) {
         $uri = $uri->withFragment($fragment);
     }
     return (string) $uri;
 }
 /**
  * @param AccessToken $accessToken
  * @param UriInterface $destination
  * @return Response
  */
 public function handle(AccessToken $accessToken, UriInterface $destination)
 {
     $claims = $this->userService->getUserClaims($accessToken)->toArray();
     $jwt = $this->encoderService->encode($claims);
     $q = $destination->getQuery();
     $q .= ($q ? '&' : '') . 'jwt=' . $jwt;
     return new RedirectResponse((string) $destination->withQuery($q));
 }
 /**
  * Merges additional query parameters with current query parameters (possibly overwriting (some of) them)
  *
  * @param UriInterface $uri
  * @param array $queryParameters Query parameters to add / overwrite
  * @return UriInterface
  */
 public static function addQueryParameters(UriInterface $uri, array $queryParameters)
 {
     $originalQuery = $uri->getQuery();
     $originalQueryParameters = [];
     parse_str($originalQuery, $originalQueryParameters);
     $newQueryParameters = array_replace_recursive($originalQueryParameters, $queryParameters);
     $newQuery = http_build_query($newQueryParameters);
     $newUri = $uri->withQuery($newQuery);
     return $newUri;
 }
Ejemplo n.º 4
0
 /**
  * Create a new URI with a specific query string value.
  *
  * Any existing query string values that exactly match the provided key are
  * removed and replaced with the given key value pair.
  *
  * Note: this function will convert "=" to "%3D" and "&" to "%26".
  *
  * @param UriInterface $uri URI to use as a base.
  * @param string $key   Key to set.
  * @param string $value Value to set.
  *
  * @return UriInterface
  */
 public static function withQueryValue(UriInterface $uri, $key, $value)
 {
     $current = $uri->getQuery();
     $key = strtr($key, self::$replaceQuery);
     if (!$current) {
         $result = [];
     } else {
         $result = [];
         foreach (explode('&', $current) as $part) {
             if (explode('=', $part)[0] !== $key) {
                 $result[] = $part;
             }
         }
     }
     if ($value !== null) {
         $result[] = $key . '=' . strtr($value, self::$replaceQuery);
     } else {
         $result[] = $key;
     }
     return $uri->withQuery(implode('&', $result));
 }
Ejemplo n.º 5
0
 /**
  * Create a new URI with a specific query string value removed.
  *
  * Any existing query string values that exactly match the provided key are removed.
  *
  * Note: this function will convert "=" to "%3D" and "&" to "%26".
  *
  * @param UriInterface $uri URI to use as a base.
  * @param string       $key Query string key value pair to remove.
  *
  * @throws \InvalidArgumentException
  *
  * @return UriInterface
  */
 public static function withoutQueryValue(UriInterface $uri, $key)
 {
     if ($uri->getQuery() === null) {
         return $uri;
     }
     $result = [];
     foreach (explode('&', $uri->getQuery()) as $part) {
         if (explode('=', $part)[0] !== $key) {
             $result[] = $part;
         }
     }
     return $uri->withQuery(implode('&', $result));
 }
Ejemplo n.º 6
0
 public static function withAddedQueryValues(UriInterface $uri, array $queryValues) : UriInterface
 {
     $current = $uri->getQuery();
     if (false === empty($current)) {
         parse_str($current, $values);
         foreach ($values as $key => $value) {
             if (false === isset($queryValues[$key])) {
                 $queryValues[$key] = $value;
             }
         }
     }
     $result = [];
     foreach ($queryValues as $key => $value) {
         $result[] = $key . '=' . $value;
     }
     return $uri->withQuery(implode('&', $result));
 }
 protected function getEnumFacet($facetName, $facetConfig, UriInterface $searchUri, $queryParams)
 {
     $attributeName = $facetConfig['attribute'];
     $cache = $this->get('app.cache');
     $cacheKey = $facetName . '-facet-' . $this->locale;
     $typeData = $this->get('app.repository.productType')->getTypes();
     if (!$cache->has($cacheKey)) {
         $facetValues = [];
         /**
          * @var ProductTypeCollection $typeData
          */
         foreach ($typeData as $productType) {
             /**
              * @var AttributeDefinition $attribute
              */
             $attribute = $productType->getAttributes()->getByName($attributeName);
             if (is_null($attribute)) {
                 continue;
             }
             /**
              * @var LocalizedEnumType $attributeType
              */
             $attributeType = $attribute->getType();
             $values = $attributeType->getValues();
             foreach ($values as $value) {
                 if (isset($facetValues[$value->getKey()])) {
                     continue;
                 }
                 $facetEntry = new ViewData();
                 $facetEntry->value = $value->getKey();
                 $facetEntry->label = (string) $value->getLabel();
                 $facetValues[$value->getKey()] = $facetEntry;
             }
         }
         $cache->store($cacheKey, serialize($facetValues));
     } else {
         $facetValues = unserialize($cache->fetch($cacheKey));
     }
     $facetData = new ViewData();
     $facetData->displayList = $facetConfig['display'] == 'list';
     $facetData->selectFacet = true;
     $facetData->facet = new ViewData();
     if ($facetConfig['multi'] == true) {
         $facetData->facet->multiSelect = $facetConfig['multi'];
     }
     $facetData->facet->available = true;
     $facetData->facet->label = $this->trans('filters.' . $facetName, [], 'catalog');
     $facetData->facet->key = $facetName;
     $limitedOptions = new ViewDataCollection();
     $selectedValues = array_diff_key($queryParams, [$facetName => true]);
     $facetData->facet->clearUri = $searchUri->withQuery(\GuzzleHttp\Psr7\build_query($selectedValues));
     foreach ($this->facets->getByName($facetName)->getTerms() as $term) {
         $key = $term->getTerm();
         $facetEntry = $facetValues[$term->getTerm()];
         $facetSelection = isset($queryParams[$facetName]) ? $queryParams[$facetName] : [];
         if (!is_array($facetSelection)) {
             $facetSelection = [$facetSelection];
         }
         if (in_array($key, $facetSelection)) {
             $facetEntry->selected = true;
             $uriValues = array_merge($selectedValues, [$facetName => array_diff($facetSelection, [$key])]);
         } else {
             $uriValues = array_merge($selectedValues, [$facetName => array_merge($facetSelection, [$key])]);
         }
         $uri = $searchUri->withQuery(\GuzzleHttp\Psr7\build_query($uriValues));
         $facetEntry->uri = $uri;
         $facetEntry->count = $term->getCount();
         $limitedOptions->add($facetEntry);
     }
     $facetData->facet->limitedOptions = $limitedOptions;
     return $facetData;
 }
Ejemplo n.º 8
0
 /**
  * Creates a new URI with a specific query string value.
  *
  * Any existing query string values that exactly match the provided key are
  * removed and replaced with the given key value pair.
  *
  * A value of null will set the query string key without a value, e.g. "key"
  * instead of "key=value".
  *
  * @param UriInterface $uri   URI to use as a base.
  * @param string       $key   Key to set.
  * @param string|null  $value Value to set
  *
  * @return UriInterface
  */
 public static function withQueryValue(UriInterface $uri, $key, $value)
 {
     $current = $uri->getQuery();
     if ($current === '') {
         $result = [];
     } else {
         $decodedKey = rawurldecode($key);
         $result = array_filter(explode('&', $current), function ($part) use($decodedKey) {
             return rawurldecode(explode('=', $part)[0]) !== $decodedKey;
         });
     }
     // Query string separators ("=", "&") within the key or value need to be encoded
     // (while preventing double-encoding) before setting the query string. All other
     // chars that need percent-encoding will be encoded by withQuery().
     $key = strtr($key, self::$replaceQuery);
     if ($value !== null) {
         $result[] = $key . '=' . strtr($value, self::$replaceQuery);
     } else {
         $result[] = $key;
     }
     return $uri->withQuery(implode('&', $result));
 }
/**
 * 
 * Adds a query string parameter key/value pair to a uri object.
 * 
 * Given a uri object $uri1 representing http://someserver.com/controller/action?param1=val1 
 * s3MVC_addQueryStrParamToUri($uri1, 'param2', 'val2') will return a new uri object representing
 * http://someserver.com/controller/action?param1=val1&param2=val2
 * 
 * @param \Psr\Http\Message\UriInterface $uri
 * @param string $param_name
 * @param string $param_value
 * 
 * @return \Psr\Http\Message\UriInterface
 */
function s3MVC_addQueryStrParamToUri(\Psr\Http\Message\UriInterface $uri, $param_name, $param_value)
{
    $query_params = [];
    parse_str($uri->getQuery(), $query_params);
    // Extract existing query string params to an array
    $query_params[$param_name] = $param_value;
    // Add new param the query string params array
    return $uri->withQuery(http_build_query($query_params));
    // return a uri object with updated query params
}
Ejemplo n.º 10
0
 /**
  * Forms an absolute link using the URI object from current reuqest.
  *
  * @param string $query Query string to use.
  *
  * @return string Absolute URI with given query.
  */
 protected function formLink($query)
 {
     $uri = $this->uri->withQuery($query);
     return $uri->__toString();
 }
Ejemplo n.º 11
0
 /**
  * {@inheritdoc}
  */
 public function createUri($pageNumber)
 {
     return $this->uri->withQuery(http_build_query($this->getQuery() + [$this->pageParameter => $pageNumber]));
 }
 /**
  * Create an UriInterface with the path information from `$server`. It uses the first part of
  * `$server['REQUEST_URI']` for this information.
  *
  * @param UriInterface $uri
  * @param array        $server
  *
  * @return UriInterface
  */
 private function withQueryString(UriInterface $uri, array $server) : UriInterface
 {
     if (isset($server['QUERY_STRING'])) {
         return $uri->withQuery($server['QUERY_STRING']);
     } else {
         if (isset($server['REQUEST_URI'])) {
             $requestUri = \explode('?', $server['REQUEST_URI'], 2);
             if (\count($requestUri) > 1) {
                 return $uri->withQuery($requestUri[1]);
             }
         }
     }
     return $uri;
 }
Ejemplo n.º 13
0
 /**
  * @param UriInterface $url
  * @param Session      $session
  *
  * @return Promise
  *
  * @resolves bool True if the module was just disconnected, false if it was already disconnected.
  *
  * @rejects  RequestException
  * @rejects  OxygenPageNotFoundException
  */
 public function disconnectOxygenAsync(UriInterface $url, Session $session)
 {
     return $this->client->getAsync($url->withQuery(\GuzzleHttp\Psr7\build_query(['q' => 'admin/config/oxygen/disconnect'])), [RequestOptions::COOKIES => $session->getCookieJar(), RequestOptions::AUTH => $session->getAuthData()])->then(function (ResponseInterface $response) use($url, $session) {
         $crawler = new Crawler((string) $response->getBody(), (string) $url);
         try {
             $form = $crawler->filter('form#oxygen-admin-disconnect')->form();
             if ($form->get('oxygen_connected')->getValue() === 'yes') {
                 return $this->client->requestAsync($form->getMethod(), $form->getUri(), [RequestOptions::COOKIES => $session->getCookieJar(), RequestOptions::AUTH => $session->getAuthData(), RequestOptions::HEADERS => ['referer' => $form->getUri()], RequestOptions::FORM_PARAMS => $form->getPhpValues()]);
             }
             return false;
         } catch (\Exception $e) {
             throw new OxygenPageNotFoundException();
         }
     })->then(function ($result) {
         if (!$result instanceof ResponseInterface) {
             // Module was already disconnected.
             return false;
         }
         // Module was successfully disconnected.
         return true;
     });
 }