1
0
Fork 0

Add unzip support on windows and fail earlier if unzipping is impossible, fixes #4943

pull/4962/head
Jordi Boggiano 2016-02-25 13:05:26 +00:00
parent a0bff7af1d
commit b945fc4d83
1 changed files with 22 additions and 6 deletions

View File

@ -15,10 +15,12 @@ namespace Composer\Downloader;
use Composer\Config; use Composer\Config;
use Composer\Cache; use Composer\Cache;
use Composer\EventDispatcher\EventDispatcher; use Composer\EventDispatcher\EventDispatcher;
use Composer\Package\PackageInterface;
use Composer\Util\Platform; use Composer\Util\Platform;
use Composer\Util\ProcessExecutor; use Composer\Util\ProcessExecutor;
use Composer\Util\RemoteFilesystem; use Composer\Util\RemoteFilesystem;
use Composer\IO\IOInterface; use Composer\IO\IOInterface;
use Symfony\Component\Process\ExecutableFinder;
use ZipArchive; use ZipArchive;
/** /**
@ -27,6 +29,7 @@ use ZipArchive;
class ZipDownloader extends ArchiveDownloader class ZipDownloader extends ArchiveDownloader
{ {
protected $process; protected $process;
protected static $hasSystemZip;
public function __construct(IOInterface $io, Config $config, EventDispatcher $eventDispatcher = null, Cache $cache = null, ProcessExecutor $process = null, RemoteFilesystem $rfs = null) public function __construct(IOInterface $io, Config $config, EventDispatcher $eventDispatcher = null, Cache $cache = null, ProcessExecutor $process = null, RemoteFilesystem $rfs = null)
{ {
@ -34,12 +37,29 @@ class ZipDownloader extends ArchiveDownloader
parent::__construct($io, $config, $eventDispatcher, $cache, $rfs); parent::__construct($io, $config, $eventDispatcher, $cache, $rfs);
} }
/**
* {@inheritDoc}
*/
public function download(PackageInterface $package, $path)
{
if (null === self::$hasSystemZip) {
$finder = new ExecutableFinder;
self::$hasSystemZip = (bool) $finder->find('unzip');
}
if (!class_exists('ZipArchive') && !self::$hasSystemZip) {
throw new \RuntimeException('The zip extension and unzip command are both missing, skipping');
}
return parent::download($package, $path);
}
protected function extract($file, $path) protected function extract($file, $path)
{ {
$processError = null; $processError = null;
// try to use unzip on *nix // try to use unzip on *nix
if (!Platform::isWindows()) { if (self::$hasSystemZip) {
$command = 'unzip '.ProcessExecutor::escape($file).' -d '.ProcessExecutor::escape($path) . ' && chmod -R u+w ' . ProcessExecutor::escape($path); $command = 'unzip '.ProcessExecutor::escape($file).' -d '.ProcessExecutor::escape($path) . ' && chmod -R u+w ' . ProcessExecutor::escape($path);
try { try {
if (0 === $this->process->execute($command, $ignoredOutput)) { if (0 === $this->process->execute($command, $ignoredOutput)) {
@ -63,11 +83,7 @@ class ZipDownloader extends ArchiveDownloader
} }
$error = "Could not decompress the archive, enable the PHP zip extension or install unzip.\n" $error = "Could not decompress the archive, enable the PHP zip extension or install unzip.\n"
. $iniMessage . "\n" . $processError; . $iniMessage . ($processError ? "\n" . $processError : '');
if (!Platform::isWindows()) {
$error = "Could not decompress the archive, enable the PHP zip extension.\n" . $iniMessage;
}
throw new \RuntimeException($error); throw new \RuntimeException($error);
} }