PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Composer/Downloader/ArchiveDownloader.php

https://github.com/skug/composer
PHP | 106 lines | 54 code | 14 blank | 38 comment | 9 complexity | d6d783eda2b43e93837da188aa3c5b38 MD5 | raw file
  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. * Base downloader for archives
  15. *
  16. * @author Kirill chEbba Chebunin <iam@chebba.org>
  17. * @author Jordi Boggiano <j.boggiano@seld.be>
  18. * @author Franรงois Pluchino <francois.pluchino@opendisplay.com>
  19. */
  20. abstract class ArchiveDownloader extends FileDownloader
  21. {
  22. /**
  23. * {@inheritDoc}
  24. */
  25. public function download(PackageInterface $package, $path)
  26. {
  27. parent::download($package, $path);
  28. $fileName = $this->getFileName($package, $path);
  29. if ($this->io->isVerbose()) {
  30. $this->io->write(' Unpacking archive');
  31. }
  32. try {
  33. $this->extract($fileName, $path);
  34. if ($this->io->isVerbose()) {
  35. $this->io->write(' Cleaning up');
  36. }
  37. unlink($fileName);
  38. // If we have only a one dir inside it suppose to be a package itself
  39. $contentDir = glob($path . '/*');
  40. if (1 === count($contentDir)) {
  41. $contentDir = $contentDir[0];
  42. // Rename the content directory to avoid error when moving up
  43. // a child folder with the same name
  44. $temporaryName = md5(time().rand());
  45. rename($contentDir, $temporaryName);
  46. $contentDir = $temporaryName;
  47. foreach (array_merge(glob($contentDir . '/.*'), glob($contentDir . '/*')) as $file) {
  48. if (trim(basename($file), '.')) {
  49. rename($file, $path . '/' . basename($file));
  50. }
  51. }
  52. rmdir($contentDir);
  53. }
  54. } catch (\Exception $e) {
  55. // clean up
  56. $this->filesystem->removeDirectory($path);
  57. throw $e;
  58. }
  59. $this->io->write('');
  60. }
  61. /**
  62. * {@inheritdoc}
  63. */
  64. protected function getFileName(PackageInterface $package, $path)
  65. {
  66. return rtrim($path.'/'.md5($path.spl_object_hash($package)).'.'.pathinfo($package->getDistUrl(), PATHINFO_EXTENSION), '.');
  67. }
  68. /**
  69. * {@inheritdoc}
  70. */
  71. protected function processUrl($url)
  72. {
  73. if (!extension_loaded('openssl') && (0 === strpos($url, 'https:') || 0 === strpos($url, 'http://github.com'))) {
  74. // bypass https for github if openssl is disabled
  75. if (preg_match('{^https?://(github.com/[^/]+/[^/]+/(zip|tar)ball/[^/]+)$}i', $url, $match)) {
  76. $url = 'http://nodeload.'.$match[1];
  77. } else {
  78. throw new \RuntimeException('You must enable the openssl extension to download files via https');
  79. }
  80. }
  81. return $url;
  82. }
  83. /**
  84. * Extract file to directory
  85. *
  86. * @param string $file Extracted file
  87. * @param string $path Directory
  88. *
  89. * @throws \UnexpectedValueException If can not extract downloaded file to path
  90. */
  91. abstract protected function extract($file, $path);
  92. }