Skip to content

Commit

Permalink
#54 overwriting open zip archive fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
Ne-Lexa committed May 4, 2020
1 parent 3942bf2 commit dbddcda
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 39 deletions.
32 changes: 20 additions & 12 deletions .php_cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/**
* PHP Code Style Fixer (config created for version 2.16.1 (Yellow Bird)).
* PHP Code Style Fixer (config created for version 2.16.3 (Yellow Bird)).
*
* Use one of the following console commands to just see the
* changes that will be made.
Expand Down Expand Up @@ -119,7 +119,7 @@ $rules = [
*
* Risky!
* Risky as new docblocks might mean more, e.g. a Doctrine entity
* might have a new column in database
* might have a new column in database.
*/
'comment_to_phpdoc' => [
'ignored_tags' => [
Expand Down Expand Up @@ -283,7 +283,7 @@ $rules = [
* - Explicit syntax allows word concatenation inside strings, e.g.
* `"${var}IsAVar"`, implicit doesn't
* - Explicit syntax is easier to detect for IDE/editors and
* therefore has colors/hightlight with higher contrast, which is
* therefore has colors/highlight with higher contrast, which is
* easier to read
* Backtick operator is skipped because it is harder to handle; you
* can use `backtick_to_shell_exec` fixer to normalize backticks to
Expand Down Expand Up @@ -327,7 +327,7 @@ $rules = [
* want to override a method, use the Template method pattern.
*
* Risky!
* Risky when overriding `public` methods of `abstract` classes
* Risky when overriding `public` methods of `abstract` classes.
*/
'final_public_method_for_abstract_class' => false,

Expand Down Expand Up @@ -725,8 +725,8 @@ $rules = [
'no_superfluous_elseif' => true,

/*
* Removes `@param` and `@return` tags that don't provide any useful
* information.
* Removes `@param`, `@return` and `@var` tags that don't provide
* any useful information.
*/
'no_superfluous_phpdoc_tags' => false,

Expand All @@ -751,7 +751,13 @@ $rules = [
*/
'no_unneeded_curly_braces' => true,

// A final class must not have final methods.
/*
* A `final` class must not have `final` methods and `private`
* methods must not be `final`.
*
* Risky!
* Risky when child class overrides a `private` method.
*/
'no_unneeded_final_method' => true,

/*
Expand Down Expand Up @@ -1190,8 +1196,8 @@ $rules = [
'phpdoc_var_annotation_correct_order' => true,

/*
* `@var` and `@type` annotations should not contain the variable
* name.
* `@var` and `@type` annotations of classy properties should not
* contain the name.
*/
'phpdoc_var_without_name' => false,

Expand Down Expand Up @@ -1366,7 +1372,7 @@ $rules = [
* `static`.
*
* Risky!
* Risky when using "->bindTo" on lambdas without referencing to
* Risky when using `->bindTo` on lambdas without referencing to
* `$this`.
*/
'static_lambda' => true,
Expand Down Expand Up @@ -1464,12 +1470,14 @@ $rules = [

if (\PHP_SAPI === 'cli' && !class_exists(\PhpCsFixer\Config::class)) {
$binFixer = __DIR__ . '/vendor/bin/php-cs-fixer';

if (!is_file($binFixer)) {
$binFixer = 'php-cs-fixer';
}
$dryRun = !\in_array('--force', $_SERVER['argv'], true);
$dryRun = !in_array('--force', $_SERVER['argv'], true);

$command = escapeshellarg($binFixer) . ' fix --config ' . escapeshellarg(__FILE__) . ' --diff-format udiff --ansi -vv';

$command = escapeshellarg($binFixer) . ' fix --config ' . escapeshellarg(__FILE__) . ' --diff-format udiff --ansi';
if ($dryRun) {
$command .= ' --dry-run';
}
Expand Down
34 changes: 18 additions & 16 deletions src/ZipFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -1611,6 +1611,17 @@ public function saveAsFile($filename)
}
$this->saveAsStream($handle);

$reopen = false;

if ($this->reader !== null) {
$meta = $this->reader->getStreamMetaData();

if ($meta['wrapper_type'] === 'plainfile' && isset($meta['uri']) && $meta['uri'] === $filename) {
$this->reader->close();
$reopen = true;
}
}

if (!@rename($tempFilename, $filename)) {
if (is_file($tempFilename)) {
unlink($tempFilename);
Expand All @@ -1619,6 +1630,10 @@ public function saveAsFile($filename)
throw new ZipException('Can not move ' . $tempFilename . ' to ' . $filename);
}

if ($reopen) {
return $this->openFile($filename);
}

return $this;
}

Expand Down Expand Up @@ -1822,24 +1837,11 @@ public function rewrite()

$meta = $this->reader->getStreamMetaData();

if ($meta['wrapper_type'] === 'plainfile' && isset($meta['uri'])) {
$this->saveAsFile($meta['uri']);
$this->close();

if (!($handle = @fopen($meta['uri'], 'rb'))) {
throw new ZipException("File {$meta['uri']} can't open.");
}
} else {
$handle = @fopen('php://temp', 'r+b');

if (!$handle) {
throw new ZipException('php://temp cannot open for write.');
}
$this->writeZipToStream($handle);
$this->close();
if ($meta['wrapper_type'] !== 'plainfile' || !isset($meta['uri'])) {
throw new ZipException('Overwrite is only supported for open local files.');
}

return $this->openFromStream($handle);
return $this->saveAsFile($meta['uri']);
}

/**
Expand Down
15 changes: 4 additions & 11 deletions tests/ZipFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1943,23 +1943,16 @@ public function testRewriteFile()
*/
public function testRewriteString()
{
$this->setExpectedException(ZipException::class, 'Overwrite is only supported for open local files');

$zipFile = new ZipFile();
$zipFile['file'] = 'content';
$zipFile['file2'] = 'content2';
$zipFile->saveAsFile($this->outputFilename);
$zipFile->close();

$zipFile->openFromString(file_get_contents($this->outputFilename));
static::assertSame(\count($zipFile), 2);
static::assertTrue(isset($zipFile['file']));
static::assertTrue(isset($zipFile['file2']));
$zipFile['file3'] = 'content3';
$zipFile = $zipFile->rewrite();
static::assertSame(\count($zipFile), 3);
static::assertTrue(isset($zipFile['file']));
static::assertTrue(isset($zipFile['file2']));
static::assertTrue(isset($zipFile['file3']));
$zipFile->close();
$zipFile['file2'] = 'content 2';
$zipFile->rewrite();
}

/**
Expand Down

0 comments on commit dbddcda

Please sign in to comment.