Skip to content

Commit 20689d2

Browse files
committed
Merge pull request #97 from alchemy-fr/issue-96
Add optional argument to overwrite target file
2 parents 8cb5d70 + 6f64bed commit 20689d2

13 files changed

+195
-114
lines changed

src/Adapter/AbstractAdapter.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
namespace Alchemy\Zippy\Adapter;
1414

1515
use Alchemy\Zippy\Archive\Archive;
16+
use Alchemy\Zippy\Archive\ArchiveInterface;
1617
use Alchemy\Zippy\Exception\InvalidArgumentException;
1718
use Alchemy\Zippy\Resource\ResourceManager;
1819
use Alchemy\Zippy\Adapter\VersionProbe\VersionProbeInterface;
@@ -99,11 +100,11 @@ public function extract(ResourceInterface $resource, $to = null)
99100
/**
100101
* @inheritdoc
101102
*/
102-
public function extractMembers(ResourceInterface $resource, $members, $to = null)
103+
public function extractMembers(ResourceInterface $resource, $members, $to = null, $overwrite = false)
103104
{
104105
$this->requireSupport();
105106

106-
return $this->doExtractMembers($resource, $members, $to);
107+
return $this->doExtractMembers($resource, $members, $to, $overwrite);
107108
}
108109

109110
/**
@@ -203,9 +204,13 @@ abstract protected function doExtract(ResourceInterface $resource, $to);
203204
/**
204205
* Do the extract members after having check that the current adapter is supported
205206
*
207+
* @param ResourceInterface $resource
208+
* @param string|string[] $members
209+
* @param string $to
210+
* @param bool $overwrite
206211
* @return \SplFileInfo The extracted archive
207212
*/
208-
abstract protected function doExtractMembers(ResourceInterface $resource, $members, $to);
213+
abstract protected function doExtractMembers(ResourceInterface $resource, $members, $to, $overwrite = false);
209214

210215
/**
211216
* Do the list members after having check that the current adapter is supported

src/Adapter/AbstractBinaryAdapter.php

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
use Alchemy\Zippy\Archive\MemberInterface;
1717
use Alchemy\Zippy\Exception\InvalidArgumentException;
1818
use Alchemy\Zippy\Exception\RuntimeException;
19-
use Alchemy\Zippy\Parser\ParserInterface;
2019
use Alchemy\Zippy\Parser\ParserFactory;
21-
use Alchemy\Zippy\ProcessBuilder\ProcessBuilderFactoryInterface;
20+
use Alchemy\Zippy\Parser\ParserInterface;
2221
use Alchemy\Zippy\ProcessBuilder\ProcessBuilderFactory;
22+
use Alchemy\Zippy\ProcessBuilder\ProcessBuilderFactoryInterface;
2323
use Alchemy\Zippy\Resource\ResourceManager;
2424
use Symfony\Component\Process\ExecutableFinder;
2525
use Symfony\Component\Process\ProcessBuilder;
@@ -50,13 +50,17 @@ abstract class AbstractBinaryAdapter extends AbstractAdapter implements BinaryAd
5050
/**
5151
* Constructor
5252
*
53-
* @param ParserInterface $parser An output parser
54-
* @param ResourceManager $manager A resource manager
55-
* @param ProcessBuilderFactoryInterface $inflator A process builder factory for the inflator binary
53+
* @param ParserInterface $parser An output parser
54+
* @param ResourceManager $manager A resource manager
55+
* @param ProcessBuilderFactoryInterface $inflator A process builder factory for the inflator binary
5656
* @param ProcessBuilderFactoryInterface|null $deflator A process builder factory for the deflator binary
5757
*/
58-
public function __construct(ParserInterface $parser, ResourceManager $manager, ProcessBuilderFactoryInterface $inflator, ProcessBuilderFactoryInterface $deflator)
59-
{
58+
public function __construct(
59+
ParserInterface $parser,
60+
ResourceManager $manager,
61+
ProcessBuilderFactoryInterface $inflator,
62+
ProcessBuilderFactoryInterface $deflator
63+
) {
6064
$this->parser = $parser;
6165
$this->manager = $manager;
6266
$this->deflator = $deflator;
@@ -144,10 +148,16 @@ public function getDeflatorVersion()
144148
*
145149
* @throws RuntimeException In case object could not be instanciated
146150
*/
147-
public static function newInstance(ExecutableFinder $finder, ResourceManager $manager, $inflatorBinaryName = null, $deflatorBinaryName = null)
148-
{
149-
$inflator = $inflatorBinaryName instanceof ProcessBuilderFactoryInterface ? $inflatorBinaryName : self::findABinary($inflatorBinaryName, static::getDefaultInflatorBinaryName(), $finder);
150-
$deflator = $deflatorBinaryName instanceof ProcessBuilderFactoryInterface ? $deflatorBinaryName : self::findABinary($deflatorBinaryName, static::getDefaultDeflatorBinaryName(), $finder);
151+
public static function newInstance(
152+
ExecutableFinder $finder,
153+
ResourceManager $manager,
154+
$inflatorBinaryName = null,
155+
$deflatorBinaryName = null
156+
) {
157+
$inflator = $inflatorBinaryName instanceof ProcessBuilderFactoryInterface ? $inflatorBinaryName : self::findABinary($inflatorBinaryName,
158+
static::getDefaultInflatorBinaryName(), $finder);
159+
$deflator = $deflatorBinaryName instanceof ProcessBuilderFactoryInterface ? $deflatorBinaryName : self::findABinary($deflatorBinaryName,
160+
static::getDefaultDeflatorBinaryName(), $finder);
151161

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

172182
private static function findABinary($wish, array $defaults, ExecutableFinder $finder)
173183
{
174-
$possibles = $wish ? (array) $wish : $defaults;
184+
$possibles = $wish ? (array)$wish : $defaults;
175185

176186
$binary = null;
177187

@@ -188,7 +198,7 @@ private static function findABinary($wish, array $defaults, ExecutableFinder $fi
188198
/**
189199
* Adds files to argument list
190200
*
191-
* @param Array $files An array of files
201+
* @param Array $files An array of files
192202
* @param ProcessBuilder $builder A Builder instance
193203
*
194204
* @return Boolean
@@ -200,8 +210,8 @@ protected function addBuilderFileArgument(array $files, ProcessBuilder $builder)
200210
array_walk($files, function ($file) use ($builder, &$iterations) {
201211
$builder->add(
202212
$file instanceof \SplFileInfo ?
203-
$file->getRealpath() :
204-
($file instanceof MemberInterface ? $file->getLocation() : $file)
213+
$file->getRealpath() :
214+
($file instanceof MemberInterface ? $file->getLocation() : $file)
205215
);
206216

207217
$iterations++;

src/Adapter/AbstractTarAdapter.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ protected function doRemove(ResourceInterface $resource, $files)
5656
/**
5757
* @inheritdoc
5858
*/
59-
protected function doExtractMembers(ResourceInterface $resource, $members, $to)
59+
protected function doExtractMembers(ResourceInterface $resource, $members, $to, $overwrite = false)
6060
{
61-
return $this->doTarExtractMembers($this->getLocalOptions(), $resource, $members, $to);
61+
return $this->doTarExtractMembers($this->getLocalOptions(), $resource, $members, $to, $overwrite);
6262
}
6363

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

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

360+
if ($overwrite == false) {
361+
$builder->add('-k');
362+
}
363+
360364
$builder
361365
->add('--extract')
362366
->add(sprintf('--file=%s', $resource->getResource()));

src/Adapter/AdapterInterface.php

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,24 @@
1111

1212
namespace Alchemy\Zippy\Adapter;
1313

14-
use Alchemy\Zippy\Archive\ArchiveInterface;
1514
use Alchemy\Zippy\Adapter\Resource\ResourceInterface;
15+
use Alchemy\Zippy\Archive\ArchiveInterface;
16+
use Alchemy\Zippy\Archive\MemberInterface;
1617
use Alchemy\Zippy\Exception\InvalidArgumentException;
18+
use Alchemy\Zippy\Exception\NotSupportedException;
1719
use Alchemy\Zippy\Exception\RuntimeException;
1820

1921
Interface AdapterInterface
2022
{
2123
/**
2224
* Opens an archive
2325
*
24-
* @param String $path The path to the archive
26+
* @param string $path The path to the archive
2527
*
2628
* @return ArchiveInterface
2729
*
2830
* @throws InvalidArgumentException In case the provided path is not valid
29-
* @throws RuntimeException In case of failure
31+
* @throws RuntimeException In case of failure
3032
*/
3133
public function open($path);
3234

@@ -36,22 +38,22 @@ public function open($path);
3638
* Please note some adapters can not create empty archives.
3739
* They would throw a `NotSupportedException` in case you ask to create an archive without files
3840
*
39-
* @param String $path The path to the archive
40-
* @param String|Array|\Traversable|null $files A filename, an array of files, or a \Traversable instance
41-
* @param Boolean $recursive Whether to recurse or not in the provided directories
41+
* @param string $path The path to the archive
42+
* @param string|string[]|\Traversable|null $files A filename, an array of files, or a \Traversable instance
43+
* @param bool $recursive Whether to recurse or not in the provided directories
4244
*
4345
* @return ArchiveInterface
4446
*
45-
* @throws RuntimeException In case of failure
46-
* @throws NotSupportedException In case the operation in not supported
47+
* @throws RuntimeException In case of failure
48+
* @throws NotSupportedException In case the operation in not supported
4749
* @throws InvalidArgumentException In case no files could be added
4850
*/
4951
public function create($path, $files = null, $recursive = true);
5052

5153
/**
5254
* Tests if the adapter is supported by the current environment
5355
*
54-
* @return Boolean
56+
* @return bool
5557
*/
5658
public function isSupported();
5759

@@ -60,7 +62,7 @@ public function isSupported();
6062
*
6163
* @param ResourceInterface $resource The path to the archive
6264
*
63-
* @return Array
65+
* @return array
6466
*
6567
* @throws RuntimeException In case of failure
6668
*/
@@ -69,26 +71,26 @@ public function listMembers(ResourceInterface $resource);
6971
/**
7072
* Adds a file to the archive
7173
*
72-
* @param ResourceInterface $resource The path to the archive
73-
* @param String|Array|\Traversable $files An array of paths to add, relative to cwd
74-
* @param Boolean $recursive Whether or not to recurse in the provided directories
74+
* @param ResourceInterface $resource The path to the archive
75+
* @param string|array|\Traversable $files An array of paths to add, relative to cwd
76+
* @param bool $recursive Whether or not to recurse in the provided directories
7577
*
76-
* @return Array
78+
* @return array
7779
*
78-
* @throws RuntimeException In case of failure
80+
* @throws RuntimeException In case of failure
7981
* @throws InvalidArgumentException In case no files could be added
8082
*/
8183
public function add(ResourceInterface $resource, $files, $recursive = true);
8284

8385
/**
8486
* Removes a member of the archive
8587
*
86-
* @param ResourceInterface $resource The path to the archive
87-
* @param String|Array|\Traversable $files A filename, an array of files, or a \Traversable instance
88+
* @param ResourceInterface $resource The path to the archive
89+
* @param String|Array|\Traversable $files A filename, an array of files, or a \Traversable instance
8890
*
8991
* @return Array
9092
*
91-
* @throws RuntimeException In case of failure
93+
* @throws RuntimeException In case of failure
9294
* @throws InvalidArgumentException In case no files could be removed
9395
*/
9496
public function remove(ResourceInterface $resource, $files);
@@ -99,11 +101,11 @@ public function remove(ResourceInterface $resource, $files);
99101
* Note that any existing files will be overwritten by the adapter
100102
*
101103
* @param ResourceInterface $resource The path to the archive
102-
* @param String|null $to The path where to extract the archive
104+
* @param string|null $to The path where to extract the archive
103105
*
104106
* @return \SplFileInfo The extracted archive
105107
*
106-
* @throws RuntimeException In case of failure
108+
* @throws RuntimeException In case of failure
107109
* @throws InvalidArgumentException In case the provided path where to extract the archive is not valid
108110
*/
109111
public function extract(ResourceInterface $resource, $to = null);
@@ -112,15 +114,16 @@ public function extract(ResourceInterface $resource, $to = null);
112114
* Extracts specific members of the archive
113115
*
114116
* @param ResourceInterface $resource The path to the archive
115-
* @param Array $members An array of members
116-
* @param String|null $to The path where to extract the members
117+
* @param string|string[] $members A path or array of paths matching the members to extract from the resource.
118+
* @param string|null $to The path where to extract the members
119+
* @param bool $overwrite Whether to overwrite existing files in target directory
117120
*
118121
* @return \SplFileInfo The extracted archive
119122
*
120-
* @throws RuntimeException In case of failure
121-
* @throws InvalidArgumentException In case no members could be removed or provide extract target directory is not valid
123+
* @throws RuntimeException In case of failure
124+
* @throws InvalidArgumentException In case no members could be removed or providedd extract target directory is not valid
122125
*/
123-
public function extractMembers(ResourceInterface $resource, $members, $to = null);
126+
public function extractMembers(ResourceInterface $resource, $members, $to = null, $overwrite = false);
124127

125128
/**
126129
* Returns the adapter name

src/Adapter/ZipAdapter.php

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@
1111

1212
namespace Alchemy\Zippy\Adapter;
1313

14+
use Alchemy\Zippy\Adapter\Resource\ResourceInterface;
15+
use Alchemy\Zippy\Adapter\VersionProbe\ZipVersionProbe;
1416
use Alchemy\Zippy\Archive\Archive;
1517
use Alchemy\Zippy\Archive\Member;
16-
use Alchemy\Zippy\Adapter\Resource\ResourceInterface;
17-
use Alchemy\Zippy\Exception\RuntimeException;
18-
use Alchemy\Zippy\Exception\NotSupportedException;
1918
use Alchemy\Zippy\Exception\InvalidArgumentException;
20-
use Alchemy\Zippy\Resource\Resource;
21-
use Alchemy\Zippy\Resource\ResourceManager;
22-
use Alchemy\Zippy\Adapter\VersionProbe\ZipVersionProbe;
19+
use Alchemy\Zippy\Exception\NotSupportedException;
20+
use Alchemy\Zippy\Exception\RuntimeException;
2321
use Alchemy\Zippy\Parser\ParserInterface;
2422
use Alchemy\Zippy\ProcessBuilder\ProcessBuilderFactoryInterface;
23+
use Alchemy\Zippy\Resource\Resource;
24+
use Alchemy\Zippy\Resource\ResourceManager;
2525
use Symfony\Component\Process\Exception\ExceptionInterface as ProcessException;
2626

2727
/**
@@ -31,9 +31,14 @@
3131
*/
3232
class ZipAdapter extends AbstractBinaryAdapter
3333
{
34-
public function __construct(ParserInterface $parser, ResourceManager $manager, ProcessBuilderFactoryInterface $inflator, ProcessBuilderFactoryInterface $deflator)
35-
{
34+
public function __construct(
35+
ParserInterface $parser,
36+
ResourceManager $manager,
37+
ProcessBuilderFactoryInterface $inflator,
38+
ProcessBuilderFactoryInterface $deflator
39+
) {
3640
parent::__construct($parser, $manager, $inflator, $deflator);
41+
3742
$this->probe = new ZipVersionProbe($inflator, $deflator);
3843
}
3944

@@ -42,7 +47,7 @@ public function __construct(ParserInterface $parser, ResourceManager $manager, P
4247
*/
4348
protected function doCreate($path, $files, $recursive)
4449
{
45-
$files = (array) $files;
50+
$files = (array)$files;
4651

4752
$builder = $this
4853
->inflator
@@ -130,7 +135,7 @@ protected function doListMembers(ResourceInterface $resource)
130135
*/
131136
protected function doAdd(ResourceInterface $resource, $files, $recursive)
132137
{
133-
$files = (array) $files;
138+
$files = (array)$files;
134139

135140
$builder = $this
136141
->inflator
@@ -225,7 +230,7 @@ protected function doGetInflatorVersion()
225230
*/
226231
protected function doRemove(ResourceInterface $resource, $files)
227232
{
228-
$files = (array) $files;
233+
$files = (array)$files;
229234

230235
$builder = $this
231236
->inflator
@@ -319,18 +324,22 @@ protected function doExtract(ResourceInterface $resource, $to)
319324
/**
320325
* @inheritdoc
321326
*/
322-
protected function doExtractMembers(ResourceInterface $resource, $members, $to)
327+
protected function doExtractMembers(ResourceInterface $resource, $members, $to, $overwrite = false)
323328
{
324329
if (null !== $to && !is_dir($to)) {
325330
throw new InvalidArgumentException(sprintf("%s is not a directory", $to));
326331
}
327332

328-
$members = (array) $members;
333+
$members = (array)$members;
329334

330335
$builder = $this
331336
->deflator
332337
->create();
333338

339+
if ((bool) $overwrite) {
340+
$builder->add('-o');
341+
}
342+
334343
$builder
335344
->add($resource->getResource());
336345

0 commit comments

Comments
 (0)