RarDownloader.php 2.9 KB

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