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;
|
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')) {
|
2011-12-16 21:22:08 +00:00
|
|
|
// try to use unzip on *nix
|
|
|
|
if (!defined('PHP_WINDOWS_VERSION_BUILD')) {
|
2012-01-18 15:37:55 +00:00
|
|
|
$result = $this->process->execute('unzip '.escapeshellarg($file).' -d '.escapeshellarg($path));
|
|
|
|
if (0 == $result) {
|
2011-12-16 21:22:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-14 22:05:08 +00:00
|
|
|
throw new \RuntimeException('You need the zip extension enabled to use the ZipDownloader');
|
2011-05-06 17:55:49 +00:00
|
|
|
}
|
|
|
|
|
2011-07-06 19:06:52 +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 12:19:41 +00:00
|
|
|
$this->handleZipError($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
|
|
|
|
|
|
|
protected function handleZipError($retval, $file)
|
|
|
|
{
|
|
|
|
switch ($retval) {
|
|
|
|
case \ZIPARCHIVE::ER_EXISTS:
|
|
|
|
throw new \UnexpectedValueException(sprintf("File '%s' already exists.", $file));
|
|
|
|
case \ZIPARCHIVE::ER_INCONS:
|
|
|
|
throw new \UnexpectedValueException(sprintf("Zip archive '%s' is inconsistent.", $file));
|
|
|
|
case \ZIPARCHIVE::ER_INVAL:
|
|
|
|
throw new \UnexpectedValueException(sprintf("Invalid argument. (%s)", $file));
|
|
|
|
case \ZIPARCHIVE::ER_MEMORY:
|
|
|
|
throw new \UnexpectedValueException(sprintf("Malloc failure. (%s)", $file));
|
|
|
|
case \ZIPARCHIVE::ER_NOENT:
|
|
|
|
throw new \UnexpectedValueException(sprintf("No such file: '%s'", $file));
|
|
|
|
case \ZIPARCHIVE::ER_NOZIP:
|
|
|
|
throw new \UnexpectedValueException(sprintf("'%s' is not a zip archive.", $file));
|
|
|
|
case \ZIPARCHIVE::ER_OPEN:
|
|
|
|
throw new \UnexpectedValueException(sprintf("Can't open file: %s", $file));
|
|
|
|
case \ZIPARCHIVE::ER_READ:
|
|
|
|
throw new \UnexpectedValueException(sprintf("Read error. (%s)", $file));
|
|
|
|
case \ZIPARCHIVE::ER_SEEK:
|
|
|
|
throw new \UnexpectedValueException(sprintf("Seek error. (%s)", $file));
|
|
|
|
default:
|
|
|
|
throw new \UnexpectedValueException(
|
|
|
|
sprintf("'%s' is not a valid zip archive, got error code: %s", $file, $retval)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2011-09-17 13:12:45 +00:00
|
|
|
}
|