Amp\Artax\Uri::resolve PHP Method

resolve() public method

public resolve ( string $toResolve ) : Uri
$toResolve string
return Uri
    public function resolve($toResolve)
    {
        $r = new Uri($toResolve);
        if ($r->__toString() === '') {
            return clone $this;
        }
        $base = $this;
        $t = new \StdClass();
        $t->scheme = '';
        $t->authority = '';
        $t->path = '';
        $t->query = '';
        $t->fragment = '';
        if ('' !== $r->getScheme()) {
            $t->scheme = $r->getScheme();
            $t->authority = $r->getAuthority();
            $t->path = $this->removeDotSegments($r->getPath());
            $t->query = $r->getQuery();
        } else {
            if ('' !== $r->getAuthority()) {
                $t->authority = $r->getAuthority();
                $t->path = $this->removeDotSegments($r->getPath());
                $t->query = $r->getQuery();
            } else {
                if ('' == $r->getPath()) {
                    $t->path = $base->getPath();
                    if ($r->getQuery()) {
                        $t->query = $r->getQuery();
                    } else {
                        $t->query = $base->getQuery();
                    }
                } else {
                    if ($r->getPath() && substr($r->getPath(), 0, 1) == "/") {
                        $t->path = $this->removeDotSegments($r->getPath());
                    } else {
                        $t->path = $this->mergePaths($base->getPath(), $r->getPath());
                    }
                    $t->query = $r->getQuery();
                }
                $t->authority = $base->getAuthority();
            }
            $t->scheme = $base->getScheme();
        }
        $t->fragment = $r->getFragment();
        $result = $this->reconstitute($t->scheme, $t->authority, $t->path, $t->query, $t->fragment);
        return new Uri($result);
    }

Usage Example

Example #1
0
 private function getOpenGrokSearchResults(string $branch, array $params) : \Generator
 {
     $branch = $branch === self::DEFAULT_BRANCH ? $branch : ($branch = 'PHP-' . $branch);
     $url = self::BASE_URL . '?project=' . $branch . '&n=10000&' . http_build_query($params);
     try {
         $request = (new HttpRequest())->setMethod('GET')->setUri($url);
         $this->cookieJar->store(new Cookie('OpenGrokProject', $branch, null, null, self::BASE_URL));
         /** @var HttpResponse $response */
         $response = (yield $this->httpClient->request($request));
         /** @var \DOMDocument $doc */
         $doc = domdocument_load_html($response->getBody());
     } catch (\Throwable $e) {
         throw new OpenGrokSearchFailureException("Totally failed to get a valid [results page]({$url})", 1);
     }
     if (!($resultsDiv = $doc->getElementById('results'))) {
         throw new OpenGrokSearchFailureException("The [results page]({$url}) is not in the format I expected it to be", 1);
     }
     try {
         $resultsTable = xpath_get_element($resultsDiv, './table');
     } catch (ElementNotFoundException $e) {
         throw new OpenGrokSearchFailureException("There were no [results]({$url}) for that search", 0);
     }
     $dir = null;
     $tests = false;
     $trim = strlen('/xref/' . $branch);
     $results = ['url' => $url, 'count' => 0, 'code' => [], 'tests' => []];
     $baseUrl = new Uri($url);
     foreach ($resultsTable->getElementsByTagName('tr') as $row) {
         /** @var \DOMElement $row */
         if (preg_match('#\\bdir\\b#', $row->getAttribute('class'))) {
             $tests = (bool) preg_match('#/tests/#i', xpath_get_element($row, './td/a')->textContent);
             continue;
         }
         foreach (xpath_get_elements($row, "./td/tt/a[@class='s']") as $resultAnchor) {
             $hrefAttr = $resultAnchor->getAttribute('href');
             $path = substr($hrefAttr, $trim);
             $href = (string) $baseUrl->resolve($hrefAttr);
             $el = xpath_get_element($resultAnchor, "./span[@class='l']");
             $line = $el->textContent;
             $code = '';
             while ($el = $el->nextSibling) {
                 $code .= $el->textContent;
             }
             $results[$tests ? 'tests' : 'code'][] = ['href' => $href, 'path' => $path, 'line' => $line, 'code' => trim(preg_replace('#\\s+#', ' ', $code))];
             $results['count']++;
         }
     }
     return $results;
 }
All Usage Examples Of Amp\Artax\Uri::resolve