PearDownloader.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. /**
  20. * {@inheritDoc}
  21. */
  22. public function download(PackageInterface $package, $path, $url, $checksum = null, $useSource = false)
  23. {
  24. $this->downloadTo($package, $url, $path, $checksum);
  25. }
  26. /**
  27. * {@inheritDoc}
  28. */
  29. public function update(PackageInterface $initial, PackageInterface $target, $path, $useSource = false)
  30. {
  31. // TODO rm old dir
  32. $this->downloadTo($package, $url, $path, $checksum);
  33. }
  34. /**
  35. * {@inheritDoc}
  36. */
  37. public function remove(PackageInterface $package, $path, $useSource = false)
  38. {
  39. echo 'rm -rf '.$path; // TODO
  40. }
  41. private function downloadTo($package, $url, $targetPath, $checksum = null)
  42. {
  43. if (!is_dir($targetPath)) {
  44. if (file_exists($targetPath)) {
  45. throw new \UnexpectedValueException($targetPath.' exists and is not a directory.');
  46. }
  47. if (!mkdir($targetPath, 0777, true)) {
  48. throw new \UnexpectedValueException($targetPath.' does not exist and could not be created.');
  49. }
  50. }
  51. $cwd = getcwd();
  52. chdir($targetPath);
  53. $tarName = basename($url);
  54. echo 'Downloading '.$url.' to '.$targetPath.'/'.$tarName.PHP_EOL;
  55. copy($url, './'.$tarName);
  56. if (!file_exists($tarName)) {
  57. throw new \UnexpectedValueException($package->getName().' could not be saved into '.$tarName.', make sure the'
  58. .' directory is writable and you have internet connectivity.');
  59. }
  60. if ($checksum && hash_file('sha1', './'.$tarName) !== $checksum) {
  61. throw new \UnexpectedValueException('The checksum verification failed for the '.$package->getName().' archive (downloaded from '.$url.'). Installation aborted.');
  62. }
  63. echo 'Unpacking archive'.PHP_EOL;
  64. exec('tar -xzf "'.escapeshellarg($tarName).'"');
  65. echo 'Cleaning up'.PHP_EOL;
  66. unlink('./'.$tarName);
  67. @unlink('./package.sig');
  68. @unlink('./package.xml');
  69. if (list($dir) = glob('./'.$package->getName().'-*', GLOB_ONLYDIR)) {
  70. foreach (array_merge(glob($dir.'/.*'), glob($dir.'/*')) as $file) {
  71. if (trim(basename($file), '.')) {
  72. rename($file, './'.basename($file));
  73. }
  74. }
  75. rmdir($dir);
  76. }
  77. chdir($cwd);
  78. }
  79. }