RarDownloader.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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\Config;
  13. use Composer\Cache;
  14. use Composer\EventDispatcher\EventDispatcher;
  15. use Composer\Util\Platform;
  16. use Composer\Util\ProcessExecutor;
  17. use Composer\Util\RemoteFilesystem;
  18. use Composer\IO\IOInterface;
  19. use RarArchive;
  20. /**
  21. * RAR archive downloader.
  22. *
  23. * Based on previous work by Jordi Boggiano ({@see ZipDownloader}).
  24. *
  25. * @author Derrick Nelson <drrcknlsn@gmail.com>
  26. */
  27. class RarDownloader extends ArchiveDownloader
  28. {
  29. protected $process;
  30. public function __construct(IOInterface $io, Config $config, EventDispatcher $eventDispatcher = null, Cache $cache = null, ProcessExecutor $process = null, RemoteFilesystem $rfs = null)
  31. {
  32. $this->process = $process ?: new ProcessExecutor($io);
  33. parent::__construct($io, $config, $eventDispatcher, $cache, $rfs);
  34. }
  35. protected function extract($file, $path)
  36. {
  37. $processError = null;
  38. // Try to use unrar on *nix
  39. if (!Platform::isWindows()) {
  40. $command = 'unrar x ' . ProcessExecutor::escape($file) . ' ' . ProcessExecutor::escape($path) . ' && chmod -R u+w ' . ProcessExecutor::escape($path);
  41. if (0 === $this->process->execute($command, $ignoredOutput)) {
  42. return;
  43. }
  44. $processError = 'Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput();
  45. }
  46. if (!class_exists('RarArchive')) {
  47. // php.ini path is added to the error message to help users find the correct file
  48. $iniPath = php_ini_loaded_file();
  49. if ($iniPath) {
  50. $iniMessage = 'The php.ini used by your command-line PHP is: ' . $iniPath;
  51. } else {
  52. $iniMessage = 'A php.ini file does not exist. You will have to create one.';
  53. }
  54. $error = "Could not decompress the archive, enable the PHP rar extension or install unrar.\n"
  55. . $iniMessage . "\n" . $processError;
  56. if (!Platform::isWindows()) {
  57. $error = "Could not decompress the archive, enable the PHP rar extension.\n" . $iniMessage;
  58. }
  59. throw new \RuntimeException($error);
  60. }
  61. $rarArchive = RarArchive::open($file);
  62. if (false === $rarArchive) {
  63. throw new \UnexpectedValueException('Could not open RAR archive: ' . $file);
  64. }
  65. $entries = $rarArchive->getEntries();
  66. if (false === $entries) {
  67. throw new \RuntimeException('Could not retrieve RAR archive entries');
  68. }
  69. foreach ($entries as $entry) {
  70. if (false === $entry->extract($path)) {
  71. throw new \RuntimeException('Could not extract entry');
  72. }
  73. }
  74. $rarArchive->close();
  75. }
  76. }