PearDownloader.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Downloader;
  12. use Composer\Package\PackageInterface;
  13. /**
  14. * @author Benjamin Eberlei <kontakt@beberlei.de>
  15. * @author Jordi Boggiano <j.boggiano@seld.be>
  16. */
  17. class PearDownloader implements DownloaderInterface
  18. {
  19. public function download(PackageInterface $package, $path, $url, $checksum = null)
  20. {
  21. $targetPath = $path . "/" . $package->getName();
  22. if (!is_dir($targetPath)) {
  23. if (file_exists($targetPath)) {
  24. throw new \UnexpectedValueException($targetPath.' exists and is not a directory.');
  25. }
  26. if (!mkdir($targetPath, 0777, true)) {
  27. throw new \UnexpectedValueException($targetPath.' does not exist and could not be created.');
  28. }
  29. }
  30. $cwd = getcwd();
  31. chdir($targetPath);
  32. $tarName = basename($url);
  33. echo 'Downloading '.$url.' to '.$targetPath.'/'.$tarName.PHP_EOL;
  34. copy($url, './'.$tarName);
  35. if (!file_exists($tarName)) {
  36. throw new \UnexpectedValueException($package->getName().' could not be saved into '.$tarName.', make sure the'
  37. .' directory is writable and you have internet connectivity.');
  38. }
  39. if ($checksum && hash_file('sha1', './'.$tarName) !== $checksum) {
  40. throw new \UnexpectedValueException('The checksum verification failed for the '.$package->getName().' archive (downloaded from '.$url.'). Installation aborted.');
  41. }
  42. echo 'Unpacking archive'.PHP_EOL;
  43. exec('tar -xzf "'.escapeshellarg($tarName).'"');
  44. echo 'Cleaning up'.PHP_EOL;
  45. unlink('./'.$tarName);
  46. @unlink('./package.sig');
  47. @unlink('./package.xml');
  48. if (list($dir) = glob('./'.$package->getName().'-*', GLOB_ONLYDIR)) {
  49. foreach (array_merge(glob($dir.'/.*'), glob($dir.'/*')) as $file) {
  50. if (trim(basename($file), '.')) {
  51. rename($file, './'.basename($file));
  52. }
  53. }
  54. rmdir($dir);
  55. }
  56. chdir($cwd);
  57. }
  58. }