Added suport for PEAR2 repositories.

main
Henrique Moody 13 years ago
parent 8123de2fea
commit 14adc0213e

@ -31,7 +31,7 @@ class PearRepository extends ArrayRepository
throw new \UnexpectedValueException('Invalid url given for PEAR repository: '.$config['url']); throw new \UnexpectedValueException('Invalid url given for PEAR repository: '.$config['url']);
} }
$this->url = $config['url']; $this->url = preg_replace('#^(.+)/+$#U', '$1', $config['url']);
} }
protected function initialize() protected function initialize()
@ -51,24 +51,62 @@ class PearRepository extends ArrayRepository
$categories = $categoryXML->getElementsByTagName("c"); $categories = $categoryXML->getElementsByTagName("c");
foreach ($categories as $category) { foreach ($categories as $category) {
$categoryLink = $category->getAttribute("xlink:href"); $link = $category->getAttribute("xlink:href");
$categoryLink = str_replace("info.xml", "packages.xml", $categoryLink); try {
if ('/' !== substr($categoryLink, 0, 1)) { $packagesLink = str_replace("info.xml", "packagesinfo.xml", $link);
$categoryLink = '/' . $categoryLink; $this->fetchPear2Repositories($this->url . '/' . $packagesLink);
} catch (\ErrorException $e) {
if (false === strpos($e->getMessage(), '404')) {
throw $e;
}
$categoryLink = str_replace("info.xml", "packages.xml", $link);
$this->fetchPearRepositories($this->url . '/' . $categoryLink);
}
}
}
/**
* @param string $categoryLink
* @throws ErrorException
* @throws InvalidArgumentException
*/
private function fetchPearRepositories($categoryLink)
{
$packagesXML = $this->requestXml($categoryLink);
$packages = $packagesXML->getElementsByTagName('p');
$loader = new ArrayLoader();
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";
try {
$releasesXML = $this->requestXml($allReleasesLink);
} catch (\ErrorException $e) {
if (strpos($e->getMessage(), '404')) {
continue;
}
throw $e;
} }
$packagesXML = $this->requestXml($this->url . $categoryLink);
$packages = $packagesXML->getElementsByTagName('p'); $releases = $releasesXML->getElementsByTagName('r');
$loader = new ArrayLoader();
foreach ($packages as $package) { foreach ($releases as $release) {
$packageName = $package->nodeValue; /* @var $release DOMElement */
$pearVersion = $release->getElementsByTagName('v')->item(0)->nodeValue;
$packageLink = $package->getAttribute('xlink:href'); $packageData = array(
$releaseLink = $this->url . str_replace("/rest/p/", "/rest/r/", $packageLink); 'name' => $packageName,
$allReleasesLink = $releaseLink . "/allreleases2.xml"; 'type' => 'library',
'dist' => array('type' => 'pear', 'url' => $this->url.'/get/'.$packageName.'-'.$pearVersion.".tgz"),
'version' => $pearVersion,
);
try { try {
$releasesXML = $this->requestXml($allReleasesLink); $deps = file_get_contents($releaseLink . "/deps.".$pearVersion.".txt");
} catch (\ErrorException $e) { } catch (\ErrorException $e) {
if (strpos($e->getMessage(), '404')) { if (strpos($e->getMessage(), '404')) {
continue; continue;
@ -76,54 +114,135 @@ class PearRepository extends ArrayRepository
throw $e; throw $e;
} }
$releases = $releasesXML->getElementsByTagName('r'); $packageData += $this->parseDependences($deps);
foreach ($releases as $release) { try {
/* @var $release DOMElement */ $this->addPackage($loader->load($packageData));
$pearVersion = $release->getElementsByTagName('v')->item(0)->nodeValue; } catch (\UnexpectedValueException $e) {
continue;
}
}
}
}
$packageData = array( /**
'name' => $packageName, * @todo Improve dependences of pear packages.
'type' => 'library', * @param array $options
'dist' => array('type' => 'pear', 'url' => $this->url.'/get/'.$packageName.'-'.$pearVersion.".tgz"), * @return array
'version' => $pearVersion, */
); private function parseDependencesOptions(array $depsOptions)
{
$data = array();
foreach ($depsOptions as $name => $options) {
if ('php' == $name) {
$key = $name;
if (isset($options['min'])) {
$value = '>=' . $options['min'];
} else {
$value = '>=0.0.0';
}
$data[$key] = $value;
try { } elseif ('package' == $name) {
$deps = file_get_contents($releaseLink . "/deps.".$pearVersion.".txt"); foreach ($options as $key => $value) {
} catch (\ErrorException $e) { $key = $value['name'];
if (strpos($e->getMessage(), '404')) { if (isset($value['min'])) {
continue; $value = '>=' . $value['min'];
} } else {
throw $e; $value = '>=0.0.0';
} }
$data[$key] = $value;
}
} elseif ('extension' == $name) {
foreach ($options as $key => $value) {
$key = 'ext-' . $value['name'];
$value = '*';
$data[$key] = $value;
}
}
}
$data = array_filter($data);
return $data;
}
if (preg_match('((O:([0-9])+:"([^"]+)"))', $deps, $matches)) { /**
if (strlen($matches[3]) == $matches[2]) { * @param string $deps
throw new \InvalidArgumentException("Invalid dependency data, it contains serialized objects."); * @return array
} * @throws InvalidArgumentException
} */
$deps = unserialize($deps); private function parseDependences($deps)
if (isset($deps['required']['package'])) { {
if (preg_match('((O:([0-9])+:"([^"]+)"))', $deps, $matches)) {
if (isset($deps['required']['package']['name'])) { if (strlen($matches[3]) == $matches[2]) {
$deps['required']['package'] = array($deps['required']['package']); throw new \InvalidArgumentException("Invalid dependency data, it contains serialized objects.");
} }
}
foreach ($deps['required']['package'] as $dependency) { $deps = (array) @unserialize($deps);
if (isset($dependency['min'])) { unset($deps['required']['pearinstaller']);
$packageData['require'][$dependency['name']] = '>='.$dependency['min'];
} else {
$packageData['require'][$dependency['name']] = '>=0.0.0';
}
}
}
try { $depsData = array();
$this->addPackage($loader->load($packageData)); if (isset($deps['required'])) {
} catch (\UnexpectedValueException $e) { $depsData['require'] = $this->parseDependencesOptions($deps['required']);
continue; } else {
} $depsData['require'] = array('php' => '>=5.3.0');
}
if (isset($depsData['optional'])) {
$depsData['recommend'] = $this->parseDependencesOptions($depsData['optional']);
}
return $depsData;
}
/**
* @param string $packagesLink
* @return void
* @throws InvalidArgumentException
*/
private function fetchPear2Repositories($packagesLink)
{
$loader = new ArrayLoader();
$packagesXml = $this->requestXml($packagesLink);
$informations = $packagesXml->getElementsByTagName('pi');
foreach ($informations as $information) {
$package = $information->getElementsByTagName('p')->item(0);
$packageName = $package->getElementsByTagName('n')->item(0)->nodeValue;
$packageData = array(
'name' => $packageName,
'type' => 'library'
);
$packageKeys = array('l' => 'license', 'd' => 'description');
foreach ($packageKeys as $pear => $composer) {
if ($package->getElementsByTagName($pear)->length > 0
&& ($pear = $package->getElementsByTagName($pear)->item(0)->nodeValue)) {
$packageData[$composer] = $pear;
}
}
$depsData = $information->getElementsByTagName('deps')->item(0);
$depsData = $depsData->getElementsByTagName('d')->item(0);
$depsData = $this->parseDependences($depsData->nodeValue);
$revisions = $information->getElementsByTagName('a')->item(0);
$revisions = $revisions->getElementsByTagName('r');
$packageUrl = $this->url . '/get/' . $packageName;
foreach ($revisions as $revision) {
$version = $revision->getElementsByTagName('v')->item(0)->nodeValue;
$revisionData = array(
'dist' => array(
'type' => 'pear',
'url' => $packageUrl . '-' . $version . '.tgz'
),
'version' => $version
);
try {
$this->addPackage(
$loader->load($packageData + $revisionData + $depsData)
);
} catch (\UnexpectedValueException $e) {
continue;
} }
} }
} }
@ -144,4 +263,4 @@ class PearRepository extends ArrayRepository
return $dom; return $dom;
} }
} }
Loading…
Cancel
Save