1
0
Fork 0
composer/src/Composer/Downloader/ZipDownloader.php

164 lines
5.3 KiB
PHP
Raw Normal View History

2011-05-06 17:55:49 +00:00
<?php
/*
* This file is part of Composer.
*
* (c) Nils Adermann <naderman@naderman.de>
* Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Composer\Downloader;
use Composer\Config;
2012-11-10 22:17:36 +00:00
use Composer\Cache;
use Composer\EventDispatcher\EventDispatcher;
use Composer\Package\PackageInterface;
use Composer\Util\IniHelper;
use Composer\Util\Platform;
2012-01-18 15:37:55 +00:00
use Composer\Util\ProcessExecutor;
use Composer\Util\RemoteFilesystem;
use Composer\IO\IOInterface;
use Symfony\Component\Process\ExecutableFinder;
2012-03-29 13:08:47 +00:00
use ZipArchive;
2011-05-06 17:55:49 +00:00
/**
* @author Jordi Boggiano <j.boggiano@seld.be>
*/
class ZipDownloader extends ArchiveDownloader
2011-05-06 17:55:49 +00:00
{
2012-01-18 15:37:55 +00:00
protected $process;
protected static $hasSystemUnzip;
2012-01-18 15:37:55 +00:00
public function __construct(IOInterface $io, Config $config, EventDispatcher $eventDispatcher = null, Cache $cache = null, ProcessExecutor $process = null, RemoteFilesystem $rfs = null)
2012-01-18 15:37:55 +00:00
{
$this->process = $process ?: new ProcessExecutor($io);
parent::__construct($io, $config, $eventDispatcher, $cache, $rfs);
2012-01-18 15:37:55 +00:00
}
/**
* {@inheritDoc}
*/
public function download(PackageInterface $package, $path, $output = true)
{
if (null === self::$hasSystemUnzip) {
$finder = new ExecutableFinder;
self::$hasSystemUnzip = (bool) $finder->find('unzip');
}
if (!class_exists('ZipArchive') && !self::$hasSystemUnzip) {
// php.ini path is added to the error message to help users find the correct file
$iniMessage = IniHelper::getMessage();
$error = "The zip extension and unzip command are both missing, skipping.\n" . $iniMessage;
throw new \RuntimeException($error);
}
return parent::download($package, $path, $output);
}
2017-02-13 13:00:48 +00:00
/**
* extract $file to $path with "unzip" command
2017-02-13 12:43:36 +00:00
*
* @param string $file File to extract
* @param string $path Path where to extract file
2017-02-13 13:00:48 +00:00
* @return bool True if succeed
2017-02-13 12:43:36 +00:00
*/
2017-02-13 13:00:48 +00:00
protected function extractWithUnzip($file, $path)
2011-05-06 17:55:49 +00:00
{
$processError = null;
2017-02-13 13:00:48 +00:00
$command = 'unzip -qq '.ProcessExecutor::escape($file).' -d '.ProcessExecutor::escape($path);
if (!Platform::isWindows()) {
$command .= ' && chmod -R u+w ' . ProcessExecutor::escape($path);
}
2017-02-13 13:00:48 +00:00
try {
if (0 === $this->process->execute($command, $ignoredOutput)) {
return TRUE;
}
2017-02-13 13:00:48 +00:00
$processError = 'Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput();
} catch (\Exception $e) {
$processError = 'Failed to execute ' . $command . "\n\n" . $e->getMessage();
2011-05-06 17:55:49 +00:00
}
2017-02-13 13:00:48 +00:00
throw new \RuntimeException($processError);
}
/**
* extract $file to $path with ZipArchive
*
* @param string $file File to extract
* @param string $path Path where to extract file
* @return bool True if succeed
*/
protected function extractWithZipArchive($file, $path)
{
2012-03-29 13:08:47 +00:00
$zipArchive = new ZipArchive();
2011-05-06 17:55:49 +00:00
if (true !== ($retval = $zipArchive->open($file))) {
2017-02-13 13:00:48 +00:00
throw new \UnexpectedValueException(rtrim($this->getErrorMessage($retval, $file)."\n"), $retval);
2011-05-06 17:55:49 +00:00
}
if (true !== $zipArchive->extractTo($path)) {
2017-02-13 13:00:48 +00:00
throw new \RuntimeException(rtrim("There was an error extracting the ZIP file, it is either corrupted or using an invalid format.\n"));
}
$zipArchive->close();
2017-02-13 13:00:48 +00:00
return TRUE;
}
/**
* extract $file to $path
*
* @param string $file File to extract
* @param string $path Path where to extract file
*/
protected function extract($file, $path)
{
if (self::$hasSystemUnzip && !(class_exists('ZipArchive') && Platform::isWindows())) {
if ( $this->extractWithUnzip($file, $path) ) {
return;
}
}
$this->extractWithZipArchive($file, $path);
2011-05-06 17:55:49 +00:00
}
2012-03-29 12:22:26 +00:00
/**
2012-03-29 13:08:47 +00:00
* Give a meaningful error message to the user.
2012-03-29 12:22:26 +00:00
*
2012-05-22 10:07:08 +00:00
* @param int $retval
* @param string $file
2012-03-29 13:08:47 +00:00
* @return string
2012-03-29 12:22:26 +00:00
*/
2012-03-29 13:08:47 +00:00
protected function getErrorMessage($retval, $file)
{
switch ($retval) {
2012-03-29 13:08:47 +00:00
case ZipArchive::ER_EXISTS:
return sprintf("File '%s' already exists.", $file);
case ZipArchive::ER_INCONS:
return sprintf("Zip archive '%s' is inconsistent.", $file);
case ZipArchive::ER_INVAL:
return sprintf("Invalid argument (%s)", $file);
case ZipArchive::ER_MEMORY:
return sprintf("Malloc failure (%s)", $file);
case ZipArchive::ER_NOENT:
return sprintf("No such zip file: '%s'", $file);
case ZipArchive::ER_NOZIP:
return sprintf("'%s' is not a zip archive.", $file);
case ZipArchive::ER_OPEN:
return sprintf("Can't open zip file: %s", $file);
case ZipArchive::ER_READ:
return sprintf("Zip read error (%s)", $file);
case ZipArchive::ER_SEEK:
return sprintf("Zip seek error (%s)", $file);
default:
return sprintf("'%s' is not a valid zip archive, got error code: %s", $file, $retval);
}
}
2011-09-17 13:12:45 +00:00
}