ZipDownloader.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 Jordi Boggiano <j.boggiano@seld.be>
  15. */
  16. class ZipDownloader implements DownloaderInterface
  17. {
  18. /**
  19. * {@inheritDoc}
  20. */
  21. public function download(PackageInterface $package, $path, $url, $checksum = null, $useSource = false)
  22. {
  23. $this->downloadTo($url, $path, $checksum);
  24. }
  25. /**
  26. * {@inheritDoc}
  27. */
  28. public function update(PackageInterface $initial, PackageInterface $target, $path, $useSource = false)
  29. {
  30. // TODO rm old dir
  31. $this->downloadTo($url, $path, $checksum);
  32. }
  33. /**
  34. * {@inheritDoc}
  35. */
  36. public function remove(PackageInterface $package, $path, $useSource = false)
  37. {
  38. echo 'rm -rf '.$path; // TODO
  39. }
  40. private function downloadTo($url, $targetPath, $checksum = null)
  41. {
  42. if (!class_exists('ZipArchive')) {
  43. throw new \UnexpectedValueException('You need the zip extension enabled to use the ZipDownloader');
  44. }
  45. if (!is_dir($targetPath)) {
  46. if (file_exists($targetPath)) {
  47. throw new \UnexpectedValueException($targetPath.' exists and is not a directory.');
  48. }
  49. if (!mkdir($targetPath, 0777, true)) {
  50. throw new \UnexpectedValueException($targetPath.' does not exist and could not be created.');
  51. }
  52. }
  53. $zipName = $targetPath.'/'.basename($url, '.zip').'.zip';
  54. echo 'Downloading '.$url.' to '.$zipName.PHP_EOL;
  55. copy($url, $zipName);
  56. if (!file_exists($zipName)) {
  57. throw new \UnexpectedValueException($targetPath.' could not be saved into '.$zipName.', make sure the'
  58. .' directory is writable and you have internet connectivity.');
  59. }
  60. if ($checksum && hash_file('sha1', $zipName) !== $checksum) {
  61. throw new \UnexpectedValueException('The checksum verification failed for the '.basename($path).' archive (downloaded from '.$url.'). Installation aborted.');
  62. }
  63. $zipArchive = new \ZipArchive();
  64. echo 'Unpacking archive'.PHP_EOL;
  65. if (true === ($retval = $zipArchive->open($zipName))) {
  66. $zipArchive->extractTo($targetPath);
  67. $zipArchive->close();
  68. echo 'Cleaning up'.PHP_EOL;
  69. unlink($zipName);
  70. if (false !== strpos($url, '//github.com/')) {
  71. $contentDir = glob($targetPath.'/*');
  72. if (1 === count($contentDir)) {
  73. $contentDir = $contentDir[0];
  74. foreach (array_merge(glob($contentDir.'/.*'), glob($contentDir.'/*')) as $file) {
  75. if (trim(basename($file), '.')) {
  76. rename($file, $targetPath.'/'.basename($file));
  77. }
  78. }
  79. rmdir($contentDir);
  80. }
  81. }
  82. } else {
  83. throw new \UnexpectedValueException($zipName.' is not a valid zip archive, got error code '.$retval);
  84. }
  85. }
  86. }