X-Git-Url: https://git.cweiske.de/phorkie.git/blobdiff_plain/63575a005e8e2386abb24e97791e18d61e6350fe..43b23197ffc3e1d08a1e08b09dbb31f06692d7ff:/src/phorkie/ForkRemote.php diff --git a/src/phorkie/ForkRemote.php b/src/phorkie/ForkRemote.php index f3639b2..f8e319a 100644 --- a/src/phorkie/ForkRemote.php +++ b/src/phorkie/ForkRemote.php @@ -3,40 +3,79 @@ namespace phorkie; class ForkRemote { + /** + * Contains error message when parse() failed + */ + public $error; + protected $url; + /** + * Array with keys (URL title) and values (arrays of urls) + * Only supported URLs are included. + * + * @var array + */ + protected $arGitUrls; + + + public function __construct($url) { - $this->url = $url; + $this->url = trim($url); } public function parse() { - $scheme = parse_url($this->url, PHP_URL_SCHEME); - switch ($scheme) { - case 'git': - //clearly a git url - $this->gitUrl = $this->url; - return true; - - case 'ssh': - //FIXME: maybe loosen this when we know how to skip the - //"do you trust this server" question of ssh - $this->error = 'ssh:// URLs are not supported'; - return false; - - case 'http': - case 'https': - return $this->extractUrlsFromHtml($this->url); + $hp = new HtmlParser(); + $ret = $hp->extractGitUrls($this->url); + $this->arGitUrls = $hp->getGitUrls(); + $this->error = $hp->error; + + return $ret; + } + + /** + * Iterate through all git urls and return one if there is only + * one supported one. + * + * @return mixed Boolean false or array with keys "url" and "title" + */ + public function getUniqueGitUrl() + { + $nFound = 0; + foreach ($this->arGitUrls as $title => $arUrls) { + foreach ($arUrls as $url) { + $nFound++; + $uniqueUrl = array('url' => $url, 'title' => $title); + } } - $this->error = 'Unknown URLs scheme: ' . $scheme; + if ($nFound == 1) { + return $uniqueUrl; + } return false; } - protected function extractUrlsFromHtml($url) + public function getGitUrls() { + 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; + } +} ?>