فهرست منبع

Add a VcsDriver abstract class.
All XxxDriver extends this abstract class now.

François Pluchino 13 سال پیش
والد
کامیت
fa793649fa

+ 12 - 11
src/Composer/Repository/Vcs/GitBitbucketDriver.php

@@ -17,9 +17,9 @@ use Composer\Json\JsonFile;
 /**
  * @author Per Bernhardt <plb@webfactory.de>
  */
-class GitBitbucketDriver implements VcsDriverInterface
+class GitBitbucketDriver extends VcsDriver implements VcsDriverInterface
 {
-    protected $url;
+    //protected $url;
     protected $owner;
     protected $repository;
     protected $tags;
@@ -29,10 +29,11 @@ class GitBitbucketDriver implements VcsDriverInterface
 
     public function __construct($url)
     {
-        $this->url = $url;
-        preg_match('#^https://bitbucket\.org/([^/]+)/(.+?)\.git$#', $url, $match);
+        preg_match('#^(?:https?|http)://bitbucket\.org/([^/]+)/(.+?)\.git$#', $url, $match);
         $this->owner = $match[1];
         $this->repository = $match[2];
+
+        parent::__construct($url);
     }
 
     /**
@@ -48,7 +49,7 @@ class GitBitbucketDriver implements VcsDriverInterface
     public function getRootIdentifier()
     {
         if (null === $this->rootIdentifier) {
-            $repoData = json_decode(file_get_contents('https://api.bitbucket.org/1.0/repositories/'.$this->owner.'/'.$this->repository), true);
+            $repoData = json_decode(file_get_contents($this->getHttpSupport() . '://api.bitbucket.org/1.0/repositories/'.$this->owner.'/'.$this->repository), true);
             $this->rootIdentifier = !empty($repoData['main_branch']) ? $repoData['main_branch'] : 'master';
         }
 
@@ -79,7 +80,7 @@ class GitBitbucketDriver implements VcsDriverInterface
     public function getDist($identifier)
     {
         $label = array_search($identifier, $this->getTags()) ?: $identifier;
-        $url = 'https://bitbucket.org/'.$this->owner.'/'.$this->repository.'/get/'.$label.'.zip';
+        $url = $this->getHttpSupport() . '://bitbucket.org/'.$this->owner.'/'.$this->repository.'/get/'.$label.'.zip';
 
         return array('type' => 'zip', 'url' => $url, 'reference' => $label, 'shasum' => '');
     }
@@ -90,7 +91,7 @@ class GitBitbucketDriver implements VcsDriverInterface
     public function getComposerInformation($identifier)
     {
         if (!isset($this->infoCache[$identifier])) {
-            $composer = @file_get_contents('https://bitbucket.org/'.$this->owner.'/'.$this->repository.'/raw/'.$identifier.'/composer.json');
+            $composer = @file_get_contents($this->getHttpSupport() . '://bitbucket.org/'.$this->owner.'/'.$this->repository.'/raw/'.$identifier.'/composer.json');
             if (!$composer) {
                 throw new \UnexpectedValueException('Failed to retrieve composer information for identifier '.$identifier.' in '.$this->getUrl());
             }
@@ -98,7 +99,7 @@ class GitBitbucketDriver implements VcsDriverInterface
             $composer = JsonFile::parseJson($composer);
 
             if (!isset($composer['time'])) {
-                $changeset = json_decode(file_get_contents('https://api.bitbucket.org/1.0/repositories/'.$this->owner.'/'.$this->repository.'/changesets/'.$identifier), true);
+                $changeset = json_decode(file_get_contents($this->getHttpSupport() . '://api.bitbucket.org/1.0/repositories/'.$this->owner.'/'.$this->repository.'/changesets/'.$identifier), true);
                 $composer['time'] = $changeset['timestamp'];
             }
             $this->infoCache[$identifier] = $composer;
@@ -113,7 +114,7 @@ class GitBitbucketDriver implements VcsDriverInterface
     public function getTags()
     {
         if (null === $this->tags) {
-            $tagsData = json_decode(file_get_contents('https://api.bitbucket.org/1.0/repositories/'.$this->owner.'/'.$this->repository.'/tags'), true);
+            $tagsData = json_decode(file_get_contents($this->getHttpSupport() . '://api.bitbucket.org/1.0/repositories/'.$this->owner.'/'.$this->repository.'/tags'), true);
             $this->tags = array();
             foreach ($tagsData as $tag => $data) {
                 $this->tags[$tag] = $data['raw_node'];
@@ -129,7 +130,7 @@ class GitBitbucketDriver implements VcsDriverInterface
     public function getBranches()
     {
         if (null === $this->branches) {
-            $branchData = json_decode(file_get_contents('https://api.bitbucket.org/1.0/repositories/'.$this->owner.'/'.$this->repository.'/branches'), true);
+            $branchData = json_decode(file_get_contents($this->getHttpSupport() . '://api.bitbucket.org/1.0/repositories/'.$this->owner.'/'.$this->repository.'/branches'), true);
             $this->branches = array();
             foreach ($branchData as $branch => $data) {
                 $this->branches[$branch] = $data['raw_node'];
@@ -158,6 +159,6 @@ class GitBitbucketDriver implements VcsDriverInterface
      */
     public static function supports($url, $deep = false)
     {
-        return preg_match('#^https://bitbucket\.org/([^/]+)/(.+?)\.git$#', $url, $match);
+        return preg_match('#^(?:https?|http)://bitbucket\.org/([^/]+)/(.+?)\.git$#', $url, $match);
     }
 }

+ 11 - 9
src/Composer/Repository/Vcs/GitHubDriver.php

@@ -7,7 +7,7 @@ use Composer\Json\JsonFile;
 /**
  * @author Jordi Boggiano <j.boggiano@seld.be>
  */
-class GitHubDriver implements VcsDriverInterface
+class GitHubDriver extends VcsDriver implements VcsDriverInterface
 {
     protected $owner;
     protected $repository;
@@ -18,9 +18,11 @@ class GitHubDriver implements VcsDriverInterface
 
     public function __construct($url)
     {
-        preg_match('#^(?:https?|git)://github\.com/([^/]+)/(.+?)(?:\.git)?$#', $url, $match);
+        preg_match('#^(?:https?|http|git)://github\.com/([^/]+)/(.+?)(?:\.git)?$#', $url, $match);
         $this->owner = $match[1];
         $this->repository = $match[2];
+
+        parent::__construct($url);
     }
 
     /**
@@ -36,7 +38,7 @@ class GitHubDriver implements VcsDriverInterface
     public function getRootIdentifier()
     {
         if (null === $this->rootIdentifier) {
-            $repoData = json_decode(file_get_contents('https://api.github.com/repos/'.$this->owner.'/'.$this->repository), true);
+            $repoData = json_decode(file_get_contents($this->getHttpSupport() . '://api.github.com/repos/'.$this->owner.'/'.$this->repository), true);
             $this->rootIdentifier = $repoData['master_branch'] ?: 'master';
         }
 
@@ -48,7 +50,7 @@ class GitHubDriver implements VcsDriverInterface
      */
     public function getUrl()
     {
-        return 'http://github.com/'.$this->owner.'/'.$this->repository.'.git';
+        return $this->url;
     }
 
     /**
@@ -67,7 +69,7 @@ class GitHubDriver implements VcsDriverInterface
     public function getDist($identifier)
     {
         $label = array_search($identifier, $this->getTags()) ?: $identifier;
-        $url = 'http://github.com/'.$this->owner.'/'.$this->repository.'/zipball/'.$label;
+        $url = $this->getHttpSupport() . '://github.com/'.$this->owner.'/'.$this->repository.'/zipball/'.$label;
 
         return array('type' => 'zip', 'url' => $url, 'reference' => $label, 'shasum' => '');
     }
@@ -78,7 +80,7 @@ class GitHubDriver implements VcsDriverInterface
     public function getComposerInformation($identifier)
     {
         if (!isset($this->infoCache[$identifier])) {
-            $composer = @file_get_contents('https://raw.github.com/'.$this->owner.'/'.$this->repository.'/'.$identifier.'/composer.json');
+            $composer = @file_get_contents($this->getHttpSupport() . '://raw.github.com/'.$this->owner.'/'.$this->repository.'/'.$identifier.'/composer.json');
             if (!$composer) {
                 throw new \UnexpectedValueException('Failed to retrieve composer information for identifier '.$identifier.' in '.$this->getUrl());
             }
@@ -86,7 +88,7 @@ class GitHubDriver implements VcsDriverInterface
             $composer = JsonFile::parseJson($composer);
 
             if (!isset($composer['time'])) {
-                $commit = json_decode(file_get_contents('https://api.github.com/repos/'.$this->owner.'/'.$this->repository.'/commits/'.$identifier), true);
+                $commit = json_decode(file_get_contents($this->getHttpSupport() . '://api.github.com/repos/'.$this->owner.'/'.$this->repository.'/commits/'.$identifier), true);
                 $composer['time'] = $commit['commit']['committer']['date'];
             }
             $this->infoCache[$identifier] = $composer;
@@ -101,7 +103,7 @@ class GitHubDriver implements VcsDriverInterface
     public function getTags()
     {
         if (null === $this->tags) {
-            $tagsData = json_decode(file_get_contents('https://api.github.com/repos/'.$this->owner.'/'.$this->repository.'/tags'), true);
+            $tagsData = json_decode(file_get_contents($this->getHttpSupport() . '://api.github.com/repos/'.$this->owner.'/'.$this->repository.'/tags'), true);
             $this->tags = array();
             foreach ($tagsData as $tag) {
                 $this->tags[$tag['name']] = $tag['commit']['sha'];
@@ -117,7 +119,7 @@ class GitHubDriver implements VcsDriverInterface
     public function getBranches()
     {
         if (null === $this->branches) {
-            $branchData = json_decode(file_get_contents('https://api.github.com/repos/'.$this->owner.'/'.$this->repository.'/branches'), true);
+            $branchData = json_decode(file_get_contents($this->getHttpSupport() . '://api.github.com/repos/'.$this->owner.'/'.$this->repository.'/branches'), true);
             $this->branches = array();
             foreach ($branchData as $branch) {
                 $this->branches[$branch['name']] = $branch['commit']['sha'];

+ 11 - 11
src/Composer/Repository/Vcs/HgBitbucketDriver.php

@@ -17,9 +17,8 @@ use Composer\Json\JsonFile;
 /**
  * @author Per Bernhardt <plb@webfactory.de>
  */
-class HgBitbucketDriver implements VcsDriverInterface
+class HgBitbucketDriver extends VcsDriver implements VcsDriverInterface
 {
-    protected $url;
     protected $owner;
     protected $repository;
     protected $tags;
@@ -29,10 +28,11 @@ class HgBitbucketDriver implements VcsDriverInterface
 
     public function __construct($url)
     {
-        $this->url = $url;
-        preg_match('#^https://bitbucket\.org/([^/]+)/([^/]+)/?$#', $url, $match);
+        preg_match('#^(?:https?|http)://bitbucket\.org/([^/]+)/([^/]+)/?$#', $url, $match);
         $this->owner = $match[1];
         $this->repository = $match[2];
+
+        parent::__construct($url);
     }
 
     /**
@@ -48,7 +48,7 @@ class HgBitbucketDriver implements VcsDriverInterface
     public function getRootIdentifier()
     {
         if (null === $this->rootIdentifier) {
-            $repoData = json_decode(file_get_contents('https://api.bitbucket.org/1.0/repositories/'.$this->owner.'/'.$this->repository.'/tags'), true);
+            $repoData = json_decode(file_get_contents($this->getHttpSupport() . '://api.bitbucket.org/1.0/repositories/'.$this->owner.'/'.$this->repository.'/tags'), true);
             $this->rootIdentifier = $repoData['tip']['raw_node'];
         }
 
@@ -79,7 +79,7 @@ class HgBitbucketDriver implements VcsDriverInterface
     public function getDist($identifier)
     {
         $label = array_search($identifier, $this->getTags()) ?: $identifier;
-        $url = 'https://bitbucket.org/'.$this->owner.'/'.$this->repository.'/get/'.$label.'.zip';
+        $url = $this->getHttpSupport() . '://bitbucket.org/'.$this->owner.'/'.$this->repository.'/get/'.$label.'.zip';
 
         return array('type' => 'zip', 'url' => $url, 'reference' => $label, 'shasum' => '');
     }
@@ -90,7 +90,7 @@ class HgBitbucketDriver implements VcsDriverInterface
     public function getComposerInformation($identifier)
     {
         if (!isset($this->infoCache[$identifier])) {
-            $composer = @file_get_contents('https://bitbucket.org/'.$this->owner.'/'.$this->repository.'/raw/'.$identifier.'/composer.json');
+            $composer = @file_get_contents($this->getHttpSupport() . '://bitbucket.org/'.$this->owner.'/'.$this->repository.'/raw/'.$identifier.'/composer.json');
             if (!$composer) {
                 throw new \UnexpectedValueException('Failed to retrieve composer information for identifier '.$identifier.' in '.$this->getUrl());
             }
@@ -98,7 +98,7 @@ class HgBitbucketDriver implements VcsDriverInterface
             $composer = JsonFile::parseJson($composer);
 
             if (!isset($composer['time'])) {
-                $changeset = json_decode(file_get_contents('https://api.bitbucket.org/1.0/repositories/'.$this->owner.'/'.$this->repository.'/changesets/'.$identifier), true);
+                $changeset = json_decode(file_get_contents($this->getHttpSupport() . '://api.bitbucket.org/1.0/repositories/'.$this->owner.'/'.$this->repository.'/changesets/'.$identifier), true);
                 $composer['time'] = $changeset['timestamp'];
             }
             $this->infoCache[$identifier] = $composer;
@@ -113,7 +113,7 @@ class HgBitbucketDriver implements VcsDriverInterface
     public function getTags()
     {
         if (null === $this->tags) {
-            $tagsData = json_decode(file_get_contents('https://api.bitbucket.org/1.0/repositories/'.$this->owner.'/'.$this->repository.'/tags'), true);
+            $tagsData = json_decode(file_get_contents($this->getHttpSupport() . '://api.bitbucket.org/1.0/repositories/'.$this->owner.'/'.$this->repository.'/tags'), true);
             $this->tags = array();
             foreach ($tagsData as $tag => $data) {
                 $this->tags[$tag] = $data['raw_node'];
@@ -129,7 +129,7 @@ class HgBitbucketDriver implements VcsDriverInterface
     public function getBranches()
     {
         if (null === $this->branches) {
-            $branchData = json_decode(file_get_contents('https://api.bitbucket.org/1.0/repositories/'.$this->owner.'/'.$this->repository.'/branches'), true);
+            $branchData = json_decode(file_get_contents($this->getHttpSupport() . '://api.bitbucket.org/1.0/repositories/'.$this->owner.'/'.$this->repository.'/branches'), true);
             $this->branches = array();
             foreach ($branchData as $branch => $data) {
                 $this->branches[$branch] = $data['raw_node'];
@@ -158,6 +158,6 @@ class HgBitbucketDriver implements VcsDriverInterface
      */
     public static function supports($url, $deep = false)
     {
-        return preg_match('#^https://bitbucket\.org/([^/]+)/([^/]+)/?$#', $url, $match);
+        return preg_match('#^(?:https?|http)://bitbucket\.org/([^/]+)/([^/]+)/?$#', $url, $match);
     }
 }

+ 5 - 5
src/Composer/Repository/Vcs/HgDriver.php

@@ -17,9 +17,8 @@ use Composer\Json\JsonFile;
 /**
  * @author Per Bernhardt <plb@webfactory.de>
  */
-class HgDriver implements VcsDriverInterface
+class HgDriver extends VcsDriver implements VcsDriverInterface
 {
-    protected $url;
     protected $tags;
     protected $branches;
     protected $rootIdentifier;
@@ -27,8 +26,9 @@ class HgDriver implements VcsDriverInterface
 
     public function __construct($url)
     {
-        $this->url = $url;
         $this->tmpDir = sys_get_temp_dir() . '/composer-' . preg_replace('{[^a-z0-9]}i', '-', $url) . '/';
+
+        parent::__construct($url);
     }
 
     /**
@@ -58,7 +58,7 @@ class HgDriver implements VcsDriverInterface
             exec(sprintf('cd %s && hg tip --template "{node}"', $tmpDir), $output);
             $this->rootIdentifier = $output[0];
         }
-        
+
         return $this->rootIdentifier;
     }
 
@@ -122,7 +122,7 @@ class HgDriver implements VcsDriverInterface
     {
         if (null === $this->tags) {
             $tags = array();
-            
+
             exec(sprintf('cd %s && hg tags', escapeshellarg($this->tmpDir)), $output);
             foreach ($output as $tag) {
                 if (preg_match('(^([^\s]+)\s+\d+:(.*)$)', $tag, $match))

+ 3 - 3
src/Composer/Repository/Vcs/SvnDriver.php

@@ -7,9 +7,8 @@ use Composer\Json\JsonFile;
 /**
  * @author Jordi Boggiano <j.boggiano@seld.be>
  */
-class SvnDriver implements VcsDriverInterface
+class SvnDriver extends VcsDriver implements VcsDriverInterface
 {
-    protected $url;
     protected $baseUrl;
     protected $tags;
     protected $branches;
@@ -17,7 +16,8 @@ class SvnDriver implements VcsDriverInterface
 
     public function __construct($url)
     {
-        $this->url = $this->baseUrl = rtrim($url, '/');
+        parent::__construct($this->baseUrl = rtrim($url, '/'));
+
         if (false !== ($pos = strrpos($url, '/trunk'))) {
             $this->baseUrl = substr($url, 0, $pos);
         }

+ 46 - 0
src/Composer/Repository/Vcs/VcsDriver.php

@@ -0,0 +1,46 @@
+<?php
+
+/*
+ * This file is part of Composer.
+ *
+ * (c) Nils Adermann <naderman@naderman.de>
+ *     Jordi Boggiano <j.boggiano@seld.be>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Composer\Repository\Vcs;
+
+/**
+ * A driver implementation
+ *
+ * @author François Pluchino <francois.pluchino@opendisplay.com>
+ */
+abstract class VcsDriver
+{
+    protected $url;
+
+    /**
+     * Constructor
+     *
+     * @param string $url The URL
+     */
+    public function __construct($url)
+    {
+        $this->url = $url;
+    }
+
+    /**
+     * Get the https or http protocol.
+     *
+     * @return string The correct type of protocol
+     */
+    protected function getHttpSupport()
+    {
+        if (extension_loaded('openssl')) {
+            return 'https';
+        }
+        return 'http';
+    }
+}