예제 #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})");
     }
 }
예제 #3
0
 protected function getInnerArray()
 {
     if (null === $this->innerArray) {
         $jsonObject = StringUtil::jsonDecode($this->connector->get($this->url, $this->params));
         $this->innerArray = new ArrayObject($this->converter ? array_map($this->converter, $jsonObject) : $jsonObject);
     }
     return $this->innerArray;
 }
예제 #4
0
 public function page($entriesPerPage = self::DEFAULT_ENTRIES_PER_PAGE, $pageNum = 1)
 {
     $params = $this->params;
     $params['per_page'] = $entriesPerPage;
     $params['page'] = $pageNum;
     $jsonObject = StringUtil::jsonDecode($this->connector->get($this->url, $params));
     return null !== $this->converter ? array_map($this->converter, $jsonObject) : $jsonObject;
 }
예제 #5
0
 public function create($title, Ref $head, Ref $base, $body = '')
 {
     $status = StringUtil::jsonDecode($this->connector->post('/repos/:owner/:repo/pulls', json_encode(array('title' => $title, 'body' => $body, 'head' => $head->getRefName(), 'base' => $base->getRefName()))));
     if (isset($status->number)) {
         return $this->jsonToEntity($status);
     }
     PhpHubExceptionFactory::throwNewGithubException($status);
 }
예제 #6
0
 public function newPartialCommitFromSha(Sha $sha)
 {
     $connector = $this->connector;
     $self = $this;
     $initializer = function () use($sha, $connector, $self) {
         $jsonObject = StringUtil::jsonDecode($connector->get("/repos/:owner/:repo/git/commits/{$sha}"));
         return $self->constructArgumentsFromJsonObject($jsonObject);
     };
     return $this->proxyFactory->newPartialProxy($this->commitReflector, array($sha, $initializer), array('constructProperties' => array('\\PhpHubApi\\Entity\\Sha $sha'), 'dontOverwrite' => array('getSha', 'equals', 'getRefName', '__toString')));
 }
예제 #7
0
 public function page($entriesPerPage = self::DEFAULT_ENTRIES_PER_PAGE, Sha $currentHash = null)
 {
     $params = $this->params;
     $params['per_page'] = $entriesPerPage;
     if (null !== $currentHash) {
         $params['sha'] = (string) $currentHash;
     }
     $jsonObject = StringUtil::jsonDecode($this->connector->get($this->url, $params));
     return null !== $this->converter ? array_map($this->converter, $jsonObject) : $jsonObject;
 }
예제 #8
0
 public function createBranch(Ref $from, $name)
 {
     if ($from instanceof Commit) {
         $from = $from->getSha();
     }
     $status = StringUtil::jsonDecode($this->connector->post('/repos/:owner/:repo/git/refs', json_encode(array('ref' => "refs/heads/{$name}", 'sha' => (string) $from))));
     if (isset($status->ref)) {
         return;
     }
     PhpHubExceptionFactory::throwNewGithubException($status);
 }