You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 lines
4.1 KiB
PHTML

<?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;
use Composer\Json\JsonFile;
use Composer\IO\IOInterface;
/**
* @author Per Bernhardt <plb@webfactory.de>
*/
class HgBitbucketDriver extends VcsDriver
{
protected $owner;
protected $repository;
protected $tags;
protected $branches;
protected $rootIdentifier;
protected $infoCache = array();
public function __construct($url, IOInterface $io)
{
preg_match('#^https://bitbucket\.org/([^/]+)/([^/]+)/?$#', $url, $match);
$this->owner = $match[1];
$this->repository = $match[2];
parent::__construct($url, $io);
}
/**
* {@inheritDoc}
*/
public function initialize()
{
}
/**
* {@inheritDoc}
*/
public function getRootIdentifier()
{
if (null === $this->rootIdentifier) {
$repoData = JsonFile::parseJson($this->getContents($this->getScheme() . '://api.bitbucket.org/1.0/repositories/'.$this->owner.'/'.$this->repository.'/tags'));
$this->rootIdentifier = $repoData['tip']['raw_node'];
}
return $this->rootIdentifier;
}
/**
* {@inheritDoc}
*/
public function getUrl()
{
return $this->url;
}
/**
* {@inheritDoc}
*/
public function getSource($identifier)
{
$label = array_search($identifier, $this->getTags()) ?: $identifier;
return array('type' => 'hg', 'url' => $this->getUrl(), 'reference' => $label);
}
/**
* {@inheritDoc}
*/
public function getDist($identifier)
{
$label = array_search($identifier, $this->getTags()) ?: $identifier;
$url = $this->getScheme() . '://bitbucket.org/'.$this->owner.'/'.$this->repository.'/get/'.$label.'.zip';
return array('type' => 'zip', 'url' => $url, 'reference' => $label, 'shasum' => '');
}
/**
* {@inheritDoc}
*/
public function getComposerInformation($identifier)
{
if (!isset($this->infoCache[$identifier])) {
$composer = $this->getContents($this->getScheme() . '://bitbucket.org/'.$this->owner.'/'.$this->repository.'/raw/'.$identifier.'/composer.json');
if (!$composer) {
return;
}
$composer = JsonFile::parseJson($composer);
if (!isset($composer['time'])) {
$changeset = JsonFile::parseJson($this->getContents($this->getScheme() . '://api.bitbucket.org/1.0/repositories/'.$this->owner.'/'.$this->repository.'/changesets/'.$identifier));
$composer['time'] = $changeset['timestamp'];
}
$this->infoCache[$identifier] = $composer;
}
return $this->infoCache[$identifier];
}
/**
* {@inheritDoc}
*/
public function getTags()
{
if (null === $this->tags) {
$tagsData = JsonFile::parseJson($this->getContents($this->getScheme() . '://api.bitbucket.org/1.0/repositories/'.$this->owner.'/'.$this->repository.'/tags'));
$this->tags = array();
foreach ($tagsData as $tag => $data) {
$this->tags[$tag] = $data['raw_node'];
}
}
return $this->tags;
}
/**
* {@inheritDoc}
*/
public function getBranches()
{
if (null === $this->branches) {
$branchData = JsonFile::parseJson($this->getContents($this->getScheme() . '://api.bitbucket.org/1.0/repositories/'.$this->owner.'/'.$this->repository.'/branches'));
$this->branches = array();
foreach ($branchData as $branch => $data) {
$this->branches[$branch] = $data['raw_node'];
}
}
return $this->branches;
}
/**
* {@inheritDoc}
*/
public static function supports($url, $deep = false)
{
return extension_loaded('openssl') && preg_match('#^https://bitbucket\.org/([^/]+)/([^/]+)/?$#', $url);
}
}