1
0
Fork 0

Rewrite PearRepository to implement PEAR Client/Server protocol itself, taking longer then pear remote-list, but at least showing ALL available versions, making arbitrary dependencies possible.

pull/5/head
Benjamin Eberlei 2011-06-08 22:06:57 +02:00 committed by Jordi Boggiano
parent 8afc2ca694
commit fa613cad19
3 changed files with 78 additions and 42 deletions

View File

@ -20,7 +20,7 @@ use Composer\Package\PackageInterface;
*/ */
abstract class AbstractDownloader abstract class AbstractDownloader
{ {
protected function downloadFile ($url, $path) protected function downloadFile($url, $path)
{ {
$file = fopen($url, "rb"); $file = fopen($url, "rb");
if ($file) { if ($file) {
@ -29,13 +29,9 @@ abstract class AbstractDownloader
while (!feof($file)) { while (!feof($file)) {
fwrite($newf, fread($file, 1024 * 8), 1024 * 8); fwrite($newf, fread($file, 1024 * 8), 1024 * 8);
} }
fclose($newf);
} }
}
if ($file) {
fclose($file); fclose($file);
} }
if ($newf) {
fclose($newf);
}
} }
} }

View File

@ -25,52 +25,92 @@ class PearRepository extends ArrayRepository
private $name; private $name;
private $url; private $url;
public function __construct($url, $name) public function __construct($url)
{ {
$this->url = $url; $this->url = $url;
$this->name = $name;
if (!filter_var($this->url, FILTER_VALIDATE_URL)) { if (!filter_var($this->url, FILTER_VALIDATE_URL)) {
throw new \UnexpectedValueException("Invalid url given for PEAR repository " . $name); throw new \UnexpectedValueException("Invalid url given for PEAR repository " . $name);
} }
} }
/**
* @param string $url
* @return DOMDocument
*/
private function requestXml($url)
{
$content = file_get_contents($url);
$dom = new \DOMDocument('1.0', 'UTF-8');
$dom->loadXML($content);
return $dom;
}
protected function initialize() protected function initialize()
{ {
parent::initialize(); parent::initialize();
exec("pear remote-list -c ".escapeshellarg($this->name), $output, $return); set_error_handler(function($severity, $message, $file, $line) {
throw new ErrorException($message, $severity, $severity, $file, $line);
});
try {
$this->fetchFromServer();
} catch(ErrorException $e) {
if ($return != 0) {
throw new \BadMethodCallException("Could not execute pear channel-list, an error occured.");
} }
restore_error_handler();
}
$headersDone = false; protected function fetchFromServer()
foreach ($output AS $line) { {
$parts = explode(" ", preg_replace('(\s{2,})', ' ', trim($line))); $categoryXML = $this->requestXml($this->url . "/rest/c/categories.xml");
if (count($parts) != 2) { $categories = $categoryXML->getElementsByTagName("c");
continue;
}
list($packageName, $pearVersion) = $parts;
if (!$headersDone) { foreach ($categories as $category) {
if ($packageName == "PACKAGE" && $pearVersion == "VERSION") { $categoryLink = $category->getAttribute("xlink:href");
$headersDone = true; $categoryLink = str_replace("info.xml", "packages.xml", $categoryLink);
$packagesXML = $this->requestXml($this->url . $categoryLink);
$packages = $packagesXML->getElementsByTagName('p');
foreach ($packages as $package) {
$packageName = $package->nodeValue;
$packageLink = $package->getAttribute('xlink:href');
$releaseLink = $this->url . str_replace("/rest/p/", "/rest/r/", $packageLink);
$allReleasesLink = $releaseLink . "/allreleases2.xml";
$releasesXML = $this->requestXml($allReleasesLink);
$releases = $releasesXML->getElementsByTagName('r');
foreach ($releases as $release) {
/* @var $release DOMElement */
$pearVersion = $release->getElementsByTagName('v')->item(0)->nodeValue;
$version = BasePackage::parseVersion($pearVersion);
$package = new MemoryPackage($packageName, $version['version'], $version['type']);
$package->setSourceType('pear');
$package->setSourceUrl($this->url.'/get/'.$packageName.'-'.$pearVersion.".tgz");
$depsLink = $releaseLink . "/deps.".$pearVersion.".txt";
$deps = file_get_contents($depsLink);
if (preg_match('((O:([0-9])+:"([^"]+)"))', $deps, $matches)) {
if (strlen($matches[3]) == $matches[2]) {
throw new \InvalidArgumentException("Invalid dependency data, it contains serialized objects.");
}
}
$deps = unserialize($deps);
if (isset($deps['required']['package'])) {
foreach ($deps['required']['package'] as $dependency) {
$requires[$dependency['name']] = $dependency['min'];
}
$package->setRequires($requires);
}
$this->addPackage($package);
} }
continue;
} }
if ($pearVersion == "-n/a-") {
continue; // Preferred stability is set to a level that this package can't fullfil.
}
$version = BasePackage::parseVersion($pearVersion);
$package = new MemoryPackage($packageName, $version['version'], $version['type']);
$package->setSourceType('pear');
$package->setSourceUrl($this->url.'/get/'.$packageName.'-'.$pearVersion.".tgz");
$this->addPackage($package);
} }
} }
} }