From f272f66324cca32c3fbc78f9214cba1ecd42d677 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Tue, 28 Jun 2011 20:42:02 +0200 Subject: [PATCH] Fixes and improvements to the PEAR implementation --- src/Composer/Composer.php | 7 ++- src/Composer/Downloader/PearDownloader.php | 53 +++++++++++++------- src/Composer/Downloader/ZipDownloader.php | 2 +- src/Composer/Repository/PearRepository.php | 58 ++++++++++++---------- 4 files changed, 75 insertions(+), 45 deletions(-) diff --git a/src/Composer/Composer.php b/src/Composer/Composer.php index dc662f92e..1338f9e2e 100644 --- a/src/Composer/Composer.php +++ b/src/Composer/Composer.php @@ -15,6 +15,7 @@ namespace Composer; use Composer\Repository\ComposerRepository; use Composer\Repository\PlatformRepository; use Composer\Repository\GitRepository; +use Composer\Repository\PearRepository; /** * @author Jordi Boggiano @@ -94,8 +95,12 @@ class Composer case 'composer': return new ComposerRepository($spec['url']); + case 'pear': - return new Repository\PearRepository($spec['url'], isset($spec['name']) ? $spec['name'] : $name); + return new PearRepository($spec['url'], $name); + + default: + throw new \UnexpectedValueException('Unknown repository type: '.$type.', could not create repository '.$name); } } } \ No newline at end of file diff --git a/src/Composer/Downloader/PearDownloader.php b/src/Composer/Downloader/PearDownloader.php index 8f3645e6a..a52811a67 100644 --- a/src/Composer/Downloader/PearDownloader.php +++ b/src/Composer/Downloader/PearDownloader.php @@ -16,32 +16,51 @@ use Composer\Package\PackageInterface; /** * @author Benjamin Eberlei + * @author Jordi Boggiano */ -class PearDownloader extends AbstractDownloader +class PearDownloader { public function download(PackageInterface $package, $path) { - $path = $path . "/" . $package->getName(); - if (!is_dir($path)) { - if (file_exists($path)) { - throw new \UnexpectedValueException($path.' exists and is not a directory.'); + $targetPath = $path . "/" . $package->getName(); + if (!is_dir($targetPath)) { + if (file_exists($targetPath)) { + throw new \UnexpectedValueException($targetPath.' exists and is not a directory.'); } - if (!mkdir($path, 0777, true)) { - throw new \UnexpectedValueException($path.' does not exist and could not be created.'); + if (!mkdir($targetPath, 0777, true)) { + throw new \UnexpectedValueException($targetPath.' does not exist and could not be created.'); } } - $tmpName = tempnam(sys_get_temp_dir(), ''); - copy($package->getSourceUrl(), $tmpName); - - if (!file_exists($tmpName)) { - throw new \UnexpectedValueException($package->getName().' could not be saved into '.$tmpName.', make sure the' - .' directory is writable and you have internet connectivity.'); - } - $cwd = getcwd(); - chdir($path); - system('tar xzf '.escapeshellarg($tmpName)); + chdir($targetPath); + + $source = $package->getSourceUrl(); + $tarName = basename($source); + + echo 'Downloading '.$source.' to '.$targetPath.'/'.$tarName.PHP_EOL; + copy($package->getSourceUrl(), './'.$tarName); + + if (!file_exists($tarName)) { + throw new \UnexpectedValueException($package->getName().' could not be saved into '.$tarName.', make sure the' + .' directory is writable and you have internet connectivity.'); + } + + echo 'Unpacking archive'.PHP_EOL; + exec('tar -xzf "'.escapeshellarg($tarName).'"'); + + echo 'Cleaning up'.PHP_EOL; + unlink('./'.$tarName); + @unlink('./package.sig'); + @unlink('./package.xml'); + if (list($dir) = glob('./'.$package->getName().'-*', GLOB_ONLYDIR)) { + foreach (array_merge(glob($dir.'/.*'), glob($dir.'/*')) as $file) { + if (trim(basename($file), '.')) { + rename($file, './'.basename($file)); + } + } + rmdir($dir); + } chdir($cwd); } } \ No newline at end of file diff --git a/src/Composer/Downloader/ZipDownloader.php b/src/Composer/Downloader/ZipDownloader.php index 7333cb75b..9fef0f9e8 100644 --- a/src/Composer/Downloader/ZipDownloader.php +++ b/src/Composer/Downloader/ZipDownloader.php @@ -17,7 +17,7 @@ use Composer\Package\PackageInterface; /** * @author Jordi Boggiano */ -class ZipDownloader extends AbstractDownloader +class ZipDownloader { public function download(PackageInterface $package, $path) { diff --git a/src/Composer/Repository/PearRepository.php b/src/Composer/Repository/PearRepository.php index 18832759c..7a66de69d 100644 --- a/src/Composer/Repository/PearRepository.php +++ b/src/Composer/Repository/PearRepository.php @@ -19,46 +19,30 @@ use Composer\Package\LinkConstraint\VersionConstraint; /** * @author Benjamin Eberlei + * @author Jordi Boggiano */ class PearRepository extends ArrayRepository { private $name; private $url; - public function __construct($url) + public function __construct($url, $name = '') { - $this->url = $url; - - if (!filter_var($this->url, FILTER_VALIDATE_URL)) { - throw new \UnexpectedValueException("Invalid url given for PEAR repository " . $name); + if (!filter_var($url, FILTER_VALIDATE_URL)) { + throw new \UnexpectedValueException('Invalid url given for PEAR repository "'.$name.'": '.$url); } + + $this->url = $url; } - /** - * @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() { parent::initialize(); set_error_handler(function($severity, $message, $file, $line) { - throw new ErrorException($message, $severity, $severity, $file, $line); + throw new \ErrorException($message, $severity, $severity, $file, $line); }); - try { - $this->fetchFromServer(); - } catch(ErrorException $e) { - - } + $this->fetchFromServer(); restore_error_handler(); } @@ -80,7 +64,7 @@ class PearRepository extends ArrayRepository $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) { @@ -102,8 +86,14 @@ class PearRepository extends ArrayRepository } $deps = unserialize($deps); if (isset($deps['required']['package'])) { + $requires = array(); foreach ($deps['required']['package'] as $dependency) { - $requires[$dependency['name']] = $dependency['min']; + if (isset($dependency['min'])) { + $constraint = new VersionConstraint('>=', $dependency['min']); + } else { + $constraint = new VersionConstraint('>=', '0.0.0'); + } + $requires[] = new Link($packageName, $dependency['name'], $constraint, 'requires'); } $package->setRequires($requires); } @@ -113,4 +103,20 @@ class PearRepository extends ArrayRepository } } } + + /** + * @param string $url + * @return DOMDocument + */ + private function requestXml($url) + { + $content = file_get_contents($url); + if (!$content) { + throw new \UnexpectedValueException('The PEAR channel at '.$url.' did not respond.'); + } + $dom = new \DOMDocument('1.0', 'UTF-8'); + $dom->loadXML($content); + + return $dom; + } }