Ejemplo n.º 1
0
 protected function download($method, $url, $data)
 {
     if (!preg_match('#http(s?)://#', $url)) {
         $url = self::GITHUB_URL . $url;
     }
     $url = StringUtil::smartReplace($this->getReplacements(), $url);
     $headers = array('User-Agent: PhpHubApi');
     if (null !== $this->environment->getToken()) {
         $headers[] = "Authorization: token {$this->environment->getToken()->getTokenValue()}";
     }
     $ch = curl_init();
     if (self::POST == $method) {
         if (is_string($data)) {
             curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
             $headers[] = 'Content-Type: text/plain';
         } else {
             curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
             curl_setopt($ch, CURLOPT_POST, 1);
         }
     } else {
         $url .= '?' . http_build_query($data);
     }
     curl_setopt($ch, CURLOPT_URL, $url);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
     $data = curl_exec($ch);
     if (false === $data) {
         throw new Exception("Error while downloading page {$url}: " . curl_error($ch));
     }
     curl_close($ch);
     return $data;
 }
 protected function download($method, $url, $data)
 {
     $this->logRequestHistory($method, $url, $data);
     $url = StringUtil::smartReplace($this->getReplacements(), $url);
     $page = array_key_exists('page', $data) ? $data['page'] : 1;
     $perPage = array_key_exists('per_page', $data) ? $data['per_page'] : 30;
     $sha = array_key_exists('sha', $data) ? $data['sha'] : null;
     // supa advanced routa
     switch (true) {
         case preg_match('#^/repos/SuRaMoN/itertools/commits#', $url):
             $allCommits = StringUtil::jsonDecode(file_get_contents(__DIR__ . '/../testdata/commits/list.json'));
             $commits = array();
             foreach ($allCommits as $commit) {
                 if (($commit->sha == $sha || null === $sha) && count($commits) < $perPage) {
                     $commits[] = $commit;
                     $sha = null;
                 }
             }
             return json_encode($commits);
         case preg_match('#^/repos/SuRaMoN/itertools/pulls#', $url):
             $allPullRequests = StringUtil::jsonDecode(file_get_contents(__DIR__ . '/../testdata/pull-requests/list.json'));
             return json_encode($allPullRequests);
         case preg_match('#^/repos/SuRaMoN/itertools/git/commits/(?P<sha>.*)#', $url, $matches):
             $sha = $matches['sha'];
             $commits = StringUtil::jsonDecode(file_get_contents(__DIR__ . '/../testdata/commits/list.json'));
             $commits = array_filter($commits, function ($c) use($sha) {
                 return $c->sha == $sha;
             });
             return json_encode(reset($commits));
         default:
             throw new Exception("Unknown route: {$url} ({$method})");
     }
 }