Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/Adapter/AbstractAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace Alchemy\Zippy\Adapter;

use Alchemy\Zippy\Archive\Archive;
use Alchemy\Zippy\Archive\ArchiveInterface;
use Alchemy\Zippy\Exception\InvalidArgumentException;
use Alchemy\Zippy\Resource\ResourceManager;
use Alchemy\Zippy\Adapter\VersionProbe\VersionProbeInterface;
Expand Down Expand Up @@ -99,11 +100,11 @@ public function extract(ResourceInterface $resource, $to = null)
/**
* @inheritdoc
*/
public function extractMembers(ResourceInterface $resource, $members, $to = null)
public function extractMembers(ResourceInterface $resource, $members, $to = null, $overwrite = false)
{
$this->requireSupport();

return $this->doExtractMembers($resource, $members, $to);
return $this->doExtractMembers($resource, $members, $to, $overwrite);
}

/**
Expand Down Expand Up @@ -203,9 +204,13 @@ abstract protected function doExtract(ResourceInterface $resource, $to);
/**
* Do the extract members after having check that the current adapter is supported
*
* @param ResourceInterface $resource
* @param string|string[] $members
* @param string $to
* @param bool $overwrite
* @return \SplFileInfo The extracted archive
*/
abstract protected function doExtractMembers(ResourceInterface $resource, $members, $to);
abstract protected function doExtractMembers(ResourceInterface $resource, $members, $to, $overwrite = false);

/**
* Do the list members after having check that the current adapter is supported
Expand Down
40 changes: 25 additions & 15 deletions src/Adapter/AbstractBinaryAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
use Alchemy\Zippy\Archive\MemberInterface;
use Alchemy\Zippy\Exception\InvalidArgumentException;
use Alchemy\Zippy\Exception\RuntimeException;
use Alchemy\Zippy\Parser\ParserInterface;
use Alchemy\Zippy\Parser\ParserFactory;
use Alchemy\Zippy\ProcessBuilder\ProcessBuilderFactoryInterface;
use Alchemy\Zippy\Parser\ParserInterface;
use Alchemy\Zippy\ProcessBuilder\ProcessBuilderFactory;
use Alchemy\Zippy\ProcessBuilder\ProcessBuilderFactoryInterface;
use Alchemy\Zippy\Resource\ResourceManager;
use Symfony\Component\Process\ExecutableFinder;
use Symfony\Component\Process\ProcessBuilder;
Expand Down Expand Up @@ -50,13 +50,17 @@ abstract class AbstractBinaryAdapter extends AbstractAdapter implements BinaryAd
/**
* Constructor
*
* @param ParserInterface $parser An output parser
* @param ResourceManager $manager A resource manager
* @param ProcessBuilderFactoryInterface $inflator A process builder factory for the inflator binary
* @param ParserInterface $parser An output parser
* @param ResourceManager $manager A resource manager
* @param ProcessBuilderFactoryInterface $inflator A process builder factory for the inflator binary
* @param ProcessBuilderFactoryInterface|null $deflator A process builder factory for the deflator binary
*/
public function __construct(ParserInterface $parser, ResourceManager $manager, ProcessBuilderFactoryInterface $inflator, ProcessBuilderFactoryInterface $deflator)
{
public function __construct(
ParserInterface $parser,
ResourceManager $manager,
ProcessBuilderFactoryInterface $inflator,
ProcessBuilderFactoryInterface $deflator
) {
$this->parser = $parser;
$this->manager = $manager;
$this->deflator = $deflator;
Expand Down Expand Up @@ -144,10 +148,16 @@ public function getDeflatorVersion()
*
* @throws RuntimeException In case object could not be instanciated
*/
public static function newInstance(ExecutableFinder $finder, ResourceManager $manager, $inflatorBinaryName = null, $deflatorBinaryName = null)
{
$inflator = $inflatorBinaryName instanceof ProcessBuilderFactoryInterface ? $inflatorBinaryName : self::findABinary($inflatorBinaryName, static::getDefaultInflatorBinaryName(), $finder);
$deflator = $deflatorBinaryName instanceof ProcessBuilderFactoryInterface ? $deflatorBinaryName : self::findABinary($deflatorBinaryName, static::getDefaultDeflatorBinaryName(), $finder);
public static function newInstance(
ExecutableFinder $finder,
ResourceManager $manager,
$inflatorBinaryName = null,
$deflatorBinaryName = null
) {
$inflator = $inflatorBinaryName instanceof ProcessBuilderFactoryInterface ? $inflatorBinaryName : self::findABinary($inflatorBinaryName,
static::getDefaultInflatorBinaryName(), $finder);
$deflator = $deflatorBinaryName instanceof ProcessBuilderFactoryInterface ? $deflatorBinaryName : self::findABinary($deflatorBinaryName,
static::getDefaultDeflatorBinaryName(), $finder);

try {
$outputParser = ParserFactory::create(static::getName());
Expand All @@ -171,7 +181,7 @@ public static function newInstance(ExecutableFinder $finder, ResourceManager $ma

private static function findABinary($wish, array $defaults, ExecutableFinder $finder)
{
$possibles = $wish ? (array) $wish : $defaults;
$possibles = $wish ? (array)$wish : $defaults;

$binary = null;

Expand All @@ -188,7 +198,7 @@ private static function findABinary($wish, array $defaults, ExecutableFinder $fi
/**
* Adds files to argument list
*
* @param Array $files An array of files
* @param Array $files An array of files
* @param ProcessBuilder $builder A Builder instance
*
* @return Boolean
Expand All @@ -200,8 +210,8 @@ protected function addBuilderFileArgument(array $files, ProcessBuilder $builder)
array_walk($files, function ($file) use ($builder, &$iterations) {
$builder->add(
$file instanceof \SplFileInfo ?
$file->getRealpath() :
($file instanceof MemberInterface ? $file->getLocation() : $file)
$file->getRealpath() :
($file instanceof MemberInterface ? $file->getLocation() : $file)
);

$iterations++;
Expand Down
10 changes: 7 additions & 3 deletions src/Adapter/AbstractTarAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ protected function doRemove(ResourceInterface $resource, $files)
/**
* @inheritdoc
*/
protected function doExtractMembers(ResourceInterface $resource, $members, $to)
protected function doExtractMembers(ResourceInterface $resource, $members, $to, $overwrite = false)
{
return $this->doTarExtractMembers($this->getLocalOptions(), $resource, $members, $to);
return $this->doTarExtractMembers($this->getLocalOptions(), $resource, $members, $to, $overwrite);
}

/**
Expand Down Expand Up @@ -345,7 +345,7 @@ protected function doTarExtract($options, ResourceInterface $resource, $to = nul
return new \SplFileInfo($to ? : $resource->getResource());
}

protected function doTarExtractMembers($options, ResourceInterface $resource, $members, $to = null)
protected function doTarExtractMembers($options, ResourceInterface $resource, $members, $to = null, $overwrite = false)
{
if (null !== $to && !is_dir($to)) {
throw new InvalidArgumentException(sprintf("%s is not a directory", $to));
Expand All @@ -357,6 +357,10 @@ protected function doTarExtractMembers($options, ResourceInterface $resource, $m
->inflator
->create();

if ($overwrite == false) {
$builder->add('-k');
}

$builder
->add('--extract')
->add(sprintf('--file=%s', $resource->getResource()));
Expand Down
53 changes: 28 additions & 25 deletions src/Adapter/AdapterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,24 @@

namespace Alchemy\Zippy\Adapter;

use Alchemy\Zippy\Archive\ArchiveInterface;
use Alchemy\Zippy\Adapter\Resource\ResourceInterface;
use Alchemy\Zippy\Archive\ArchiveInterface;
use Alchemy\Zippy\Archive\MemberInterface;
use Alchemy\Zippy\Exception\InvalidArgumentException;
use Alchemy\Zippy\Exception\NotSupportedException;
use Alchemy\Zippy\Exception\RuntimeException;

Interface AdapterInterface
{
/**
* Opens an archive
*
* @param String $path The path to the archive
* @param string $path The path to the archive
*
* @return ArchiveInterface
*
* @throws InvalidArgumentException In case the provided path is not valid
* @throws RuntimeException In case of failure
* @throws RuntimeException In case of failure
*/
public function open($path);

Expand All @@ -36,22 +38,22 @@ public function open($path);
* Please note some adapters can not create empty archives.
* They would throw a `NotSupportedException` in case you ask to create an archive without files
*
* @param String $path The path to the archive
* @param String|Array|\Traversable|null $files A filename, an array of files, or a \Traversable instance
* @param Boolean $recursive Whether to recurse or not in the provided directories
* @param string $path The path to the archive
* @param string|string[]|\Traversable|null $files A filename, an array of files, or a \Traversable instance
* @param bool $recursive Whether to recurse or not in the provided directories
*
* @return ArchiveInterface
*
* @throws RuntimeException In case of failure
* @throws NotSupportedException In case the operation in not supported
* @throws RuntimeException In case of failure
* @throws NotSupportedException In case the operation in not supported
* @throws InvalidArgumentException In case no files could be added
*/
public function create($path, $files = null, $recursive = true);

/**
* Tests if the adapter is supported by the current environment
*
* @return Boolean
* @return bool
*/
public function isSupported();

Expand All @@ -60,7 +62,7 @@ public function isSupported();
*
* @param ResourceInterface $resource The path to the archive
*
* @return Array
* @return array
*
* @throws RuntimeException In case of failure
*/
Expand All @@ -69,26 +71,26 @@ public function listMembers(ResourceInterface $resource);
/**
* Adds a file to the archive
*
* @param ResourceInterface $resource The path to the archive
* @param String|Array|\Traversable $files An array of paths to add, relative to cwd
* @param Boolean $recursive Whether or not to recurse in the provided directories
* @param ResourceInterface $resource The path to the archive
* @param string|array|\Traversable $files An array of paths to add, relative to cwd
* @param bool $recursive Whether or not to recurse in the provided directories
*
* @return Array
* @return array
*
* @throws RuntimeException In case of failure
* @throws RuntimeException In case of failure
* @throws InvalidArgumentException In case no files could be added
*/
public function add(ResourceInterface $resource, $files, $recursive = true);

/**
* Removes a member of the archive
*
* @param ResourceInterface $resource The path to the archive
* @param String|Array|\Traversable $files A filename, an array of files, or a \Traversable instance
* @param ResourceInterface $resource The path to the archive
* @param String|Array|\Traversable $files A filename, an array of files, or a \Traversable instance
*
* @return Array
*
* @throws RuntimeException In case of failure
* @throws RuntimeException In case of failure
* @throws InvalidArgumentException In case no files could be removed
*/
public function remove(ResourceInterface $resource, $files);
Expand All @@ -99,11 +101,11 @@ public function remove(ResourceInterface $resource, $files);
* Note that any existing files will be overwritten by the adapter
*
* @param ResourceInterface $resource The path to the archive
* @param String|null $to The path where to extract the archive
* @param string|null $to The path where to extract the archive
*
* @return \SplFileInfo The extracted archive
*
* @throws RuntimeException In case of failure
* @throws RuntimeException In case of failure
* @throws InvalidArgumentException In case the provided path where to extract the archive is not valid
*/
public function extract(ResourceInterface $resource, $to = null);
Expand All @@ -112,15 +114,16 @@ public function extract(ResourceInterface $resource, $to = null);
* Extracts specific members of the archive
*
* @param ResourceInterface $resource The path to the archive
* @param Array $members An array of members
* @param String|null $to The path where to extract the members
* @param string|string[] $members A path or array of paths matching the members to extract from the resource.
* @param string|null $to The path where to extract the members
* @param bool $overwrite Whether to overwrite existing files in target directory
*
* @return \SplFileInfo The extracted archive
*
* @throws RuntimeException In case of failure
* @throws InvalidArgumentException In case no members could be removed or provide extract target directory is not valid
* @throws RuntimeException In case of failure
* @throws InvalidArgumentException In case no members could be removed or providedd extract target directory is not valid
*/
public function extractMembers(ResourceInterface $resource, $members, $to = null);
public function extractMembers(ResourceInterface $resource, $members, $to = null, $overwrite = false);

/**
* Returns the adapter name
Expand Down
35 changes: 22 additions & 13 deletions src/Adapter/ZipAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@

namespace Alchemy\Zippy\Adapter;

use Alchemy\Zippy\Adapter\Resource\ResourceInterface;
use Alchemy\Zippy\Adapter\VersionProbe\ZipVersionProbe;
use Alchemy\Zippy\Archive\Archive;
use Alchemy\Zippy\Archive\Member;
use Alchemy\Zippy\Adapter\Resource\ResourceInterface;
use Alchemy\Zippy\Exception\RuntimeException;
use Alchemy\Zippy\Exception\NotSupportedException;
use Alchemy\Zippy\Exception\InvalidArgumentException;
use Alchemy\Zippy\Resource\Resource;
use Alchemy\Zippy\Resource\ResourceManager;
use Alchemy\Zippy\Adapter\VersionProbe\ZipVersionProbe;
use Alchemy\Zippy\Exception\NotSupportedException;
use Alchemy\Zippy\Exception\RuntimeException;
use Alchemy\Zippy\Parser\ParserInterface;
use Alchemy\Zippy\ProcessBuilder\ProcessBuilderFactoryInterface;
use Alchemy\Zippy\Resource\Resource;
use Alchemy\Zippy\Resource\ResourceManager;
use Symfony\Component\Process\Exception\ExceptionInterface as ProcessException;

/**
Expand All @@ -31,9 +31,14 @@
*/
class ZipAdapter extends AbstractBinaryAdapter
{
public function __construct(ParserInterface $parser, ResourceManager $manager, ProcessBuilderFactoryInterface $inflator, ProcessBuilderFactoryInterface $deflator)
{
public function __construct(
ParserInterface $parser,
ResourceManager $manager,
ProcessBuilderFactoryInterface $inflator,
ProcessBuilderFactoryInterface $deflator
) {
parent::__construct($parser, $manager, $inflator, $deflator);

$this->probe = new ZipVersionProbe($inflator, $deflator);
}

Expand All @@ -42,7 +47,7 @@ public function __construct(ParserInterface $parser, ResourceManager $manager, P
*/
protected function doCreate($path, $files, $recursive)
{
$files = (array) $files;
$files = (array)$files;

$builder = $this
->inflator
Expand Down Expand Up @@ -130,7 +135,7 @@ protected function doListMembers(ResourceInterface $resource)
*/
protected function doAdd(ResourceInterface $resource, $files, $recursive)
{
$files = (array) $files;
$files = (array)$files;

$builder = $this
->inflator
Expand Down Expand Up @@ -225,7 +230,7 @@ protected function doGetInflatorVersion()
*/
protected function doRemove(ResourceInterface $resource, $files)
{
$files = (array) $files;
$files = (array)$files;

$builder = $this
->inflator
Expand Down Expand Up @@ -319,18 +324,22 @@ protected function doExtract(ResourceInterface $resource, $to)
/**
* @inheritdoc
*/
protected function doExtractMembers(ResourceInterface $resource, $members, $to)
protected function doExtractMembers(ResourceInterface $resource, $members, $to, $overwrite = false)
{
if (null !== $to && !is_dir($to)) {
throw new InvalidArgumentException(sprintf("%s is not a directory", $to));
}

$members = (array) $members;
$members = (array)$members;

$builder = $this
->deflator
->create();

if ((bool) $overwrite) {
$builder->add('-o');
}

$builder
->add($resource->getResource());

Expand Down
Loading