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

92 lines
3.0 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\Package\PackageInterface;
2012-01-18 15:37:55 +00:00
use Composer\Util\ProcessExecutor;
use Composer\IO\IOInterface;
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;
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;
parent::__construct($io);
2012-01-18 15:37:55 +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;
}
}
throw new \RuntimeException('You need the zip extension enabled to use the ZipDownloader');
2011-05-06 17:55:49 +00:00
}
$zipArchive = new \ZipArchive();
2011-05-06 17:55:49 +00:00
if (true !== ($retval = $zipArchive->open($file))) {
$this->handleZipError($retval, $file);
2011-05-06 17:55:49 +00:00
}
$zipArchive->extractTo($path);
$zipArchive->close();
2011-05-06 17:55:49 +00:00
}
2012-03-29 12:22:26 +00:00
/**
* Handle the error and give a meaningful error message to the user.
*
* @param int $retval
* @param string $file
*
* @throws \UnexpectedValueException
*/
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
}