move load method to top
[phorkie.git] / src / phorkie / ForkRemote.php
index 524255aca5564184eae44fa65a98c658afc98154..7f6129d9a524cefe134e116a2ac6ffd4014b2dbf 100644 (file)
@@ -3,6 +3,11 @@ namespace phorkie;
 
 class ForkRemote
 {
+    /**
+     * Contains error message when parse() failed
+     */
+    public $error;
+
     protected $url;
 
     /**
@@ -17,12 +22,27 @@ class ForkRemote
 
     public function __construct($url)
     {
-        $this->url = $url;
+        $this->url = trim($url);
     }
 
     public function parse()
     {
-        $scheme = parse_url($this->url, PHP_URL_SCHEME);
+        if ($this->url == '') {
+            $this->error = 'Empty fork URL';
+            return false;
+        }
+
+        $arUrl  = parse_url($this->url);
+        $scheme = isset($arUrl['scheme']) ? $arUrl['scheme'] : '';
+
+        if ($scheme == 'https' && isset($arUrl['host'])
+            && $arUrl['host'] == 'gist.github.com'
+        ) {
+            $this->arGitUrls[][] = 'git://gist.github.com/'
+                . ltrim($arUrl['path'], '/') . '.git';
+            return true;
+        }
+
         switch ($scheme) {
         case 'git':
             //clearly a git url
@@ -75,7 +95,12 @@ class ForkRemote
             }
         }
 
-        return $count > 0;
+        if ($count > 0) {
+            return true;
+        }
+
+        $this->error = 'No git:// clone URL found';
+        return false;
     }
 
     /**
@@ -105,6 +130,22 @@ class ForkRemote
         return $this->arGitUrls;
     }
 
+    /**
+     * Get the URL from which the git URL was derived, often
+     * the HTTP URL.
+     *
+     * @return string
+     */
+    public function getUrl()
+    {
+        return $this->url;
+    }
+
+    public function setUrl($url)
+    {
+        $this->url = $url;
+    }
+
     public function isSupported($url)
     {
         return parse_url($url, PHP_URL_SCHEME) == 'git';