浏览代码

Urlencode Gitlab project names

Url encode all non alphanumeric characters in project name for GitLabDriver.

If the project name has "." characters in it, which is supported in Gitlab, the Gitlab API will 404 when requesting the branches or tags of the repository. This commit urlencodes all non alphanumeric characters in the project name in requests to the Gitlab API.
Calin Marian 8 年之前
父节点
当前提交
862a13a17e
共有 1 个文件被更改,包括 18 次插入1 次删除
  1. 18 1
      src/Composer/Repository/Vcs/GitLabDriver.php

+ 18 - 1
src/Composer/Repository/Vcs/GitLabDriver.php

@@ -228,7 +228,24 @@ class GitLabDriver extends VcsDriver
      */
     public function getApiUrl()
     {
-        return $this->scheme.'://'.$this->originUrl.'/api/v3/projects/'.$this->owner.'%2F'.$this->repository;
+        return $this->scheme.'://'.$this->originUrl.'/api/v3/projects/'.$this->urlEncodeAll($this->owner).'%2F'.$this->urlEncodeAll($this->repository);
+    }
+
+    /**
+     * Urlencode all non alphanumeric characters.
+     * 
+     * @param string $string
+     * @return string
+     */
+    protected function urlEncodeAll($string)
+    {
+        $encoded = '';
+        for ($i = 0; isset($string[$i]); $i++) {
+            $character = $string[$i];
+            if (!ctype_alnum($character)) $character = '%' . sprintf('%02X', ord($character));
+            $encoded .= $character;
+        }
+        return $encoded;
     }
 
     /**