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

54 lines
1.4 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;
2011-05-06 17:55:49 +00:00
/**
* @author Jordi Boggiano <j.boggiano@seld.be>
*/
class ZipDownloader extends FileDownloader
2011-05-06 17:55:49 +00:00
{
2012-01-18 15:37:55 +00:00
protected $process;
public function __construct(ProcessExecutor $process = null)
{
$this->process = $process ?: new ProcessExecutor;
}
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))) {
throw new \UnexpectedValueException($file.' is not a valid zip archive, got error code '.$retval);
2011-05-06 17:55:49 +00:00
}
$zipArchive->extractTo($path);
$zipArchive->close();
2011-05-06 17:55:49 +00:00
}
2011-09-17 13:12:45 +00:00
}