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\Package\PackageInterface;
|
2012-01-18 15:37:55 +00:00
|
|
|
use Composer\Util\ProcessExecutor;
|
2012-01-18 16:11:26 +00:00
|
|
|
use Composer\IO\IOInterface;
|
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>
|
|
|
|
*/
|
2012-02-17 22:10:02 +00:00
|
|
|
class ZipDownloader extends ArchiveDownloader
|
2011-05-06 17:55:49 +00:00
|
|
|
{
|
2012-01-18 15:37:55 +00:00
|
|
|
protected $process;
|
|
|
|
|
2012-01-18 16:36:57 +00:00
|
|
|
public function __construct(IOInterface $io, ProcessExecutor $process = null)
|
2012-01-18 15:37:55 +00:00
|
|
|
{
|
|
|
|
$this->process = $process ?: new ProcessExecutor;
|
2012-01-18 16:11:26 +00:00
|
|
|
parent::__construct($io);
|
2012-01-18 15:37:55 +00:00
|
|
|
}
|
|
|
|
|
2011-09-28 22:48:17 +00:00
|
|
|
protected function extract($file, $path)
|
2011-05-06 17:55:49 +00:00
|
|
|
{
|
|
|
|
if (!class_exists('ZipArchive')) {
|
2012-04-10 12:59:20 +00:00
|
|
|
$error = 'You need the zip extension enabled to use the ZipDownloader';
|
|
|
|
|
2011-12-16 21:22:08 +00:00
|
|
|
// try to use unzip on *nix
|
|
|
|
if (!defined('PHP_WINDOWS_VERSION_BUILD')) {
|
2012-04-10 12:59:20 +00:00
|
|
|
$command = 'unzip '.escapeshellarg($file).' -d '.escapeshellarg($path);
|
|
|
|
if (0 === $this->process->execute($command, $ignoredOutput)) {
|
2011-12-16 21:22:08 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-04-10 12:59:20 +00:00
|
|
|
|
|
|
|
$error = 'Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput();
|
2011-12-16 21:22:08 +00:00
|
|
|
}
|
|
|
|
|
2012-04-10 12:59:20 +00:00
|
|
|
throw new \RuntimeException($error);
|
2011-05-06 17:55:49 +00:00
|
|
|
}
|
|
|
|
|
2012-03-29 13:08:47 +00:00
|
|
|
$zipArchive = new ZipArchive();
|
2011-05-06 17:55:49 +00:00
|
|
|
|
2011-09-28 22:48:17 +00:00
|
|
|
if (true !== ($retval = $zipArchive->open($file))) {
|
2012-03-29 13:08:47 +00:00
|
|
|
throw new \UnexpectedValueException($this->getErrorMessage($retval, $file));
|
2011-05-06 17:55:49 +00:00
|
|
|
}
|
2011-09-28 22:48:17 +00:00
|
|
|
|
|
|
|
$zipArchive->extractTo($path);
|
|
|
|
$zipArchive->close();
|
2011-05-06 17:55:49 +00:00
|
|
|
}
|
2012-03-29 12:19:41 +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
|
|
|
*
|
|
|
|
* @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)
|
2012-03-29 12:19:41 +00:00
|
|
|
{
|
|
|
|
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);
|
2012-03-29 12:19:41 +00:00
|
|
|
}
|
|
|
|
}
|
2011-09-17 13:12:45 +00:00
|
|
|
}
|