Skip to content

Commit 502efd3

Browse files
maennchendonatj
andauthored
Refactor Library (#224)
* Refactor Library * Update composer.json Co-authored-by: Jesse Donat <donatj@gmail.com> * Update src/ZipStream.php Co-authored-by: Jesse Donat <donatj@gmail.com> * Update src/File.php Co-authored-by: Jesse Donat <donatj@gmail.com> * Refactor file to avoid deflate state * Fix Linter / Formatter for dontaj/next-avoid-file-state * Allow PHP 8.1 * Make Psr7 Stream optional * Provide complete tests for Windows (#236) Co-authored-by: Jesse Donat <donatj@gmail.com>
1 parent 3fa72e4 commit 502efd3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+3123
-2268
lines changed

.github/workflows/part_docs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
id: setup-php
2525
uses: shivammathur/setup-php@v2
2626
with:
27-
php-version: "8.2"
27+
php-version: "8.1"
2828
tools: phive
2929
- name: Cache Tools
3030
uses: actions/cache@v3

.github/workflows/part_test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
strategy:
1515
fail-fast: false
1616
matrix:
17-
php: ["8.0", "8.1", "8.2"]
17+
php: ["8.1", "8.2"]
1818
os: [ubuntu-latest]
1919
experimental: [false]
2020
include:

.php-cs-fixer.dist.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
return $config->setRules([
2727
'@PER' => true,
2828
'@PER:risky' => true,
29-
'@PHP81Migration' => true,
29+
'@PHP82Migration' => true,
3030
'@PHPUnit84Migration:risky' => true,
3131
'array_syntax' => ['syntax' => 'short'],
3232
'class_attributes_separation' => true,
@@ -68,4 +68,4 @@
6868
],
6969
])
7070
->setFinder($finder)
71-
->setRiskyAllowed(true);
71+
->setRiskyAllowed(true);

README.md

Lines changed: 61 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@ version.
1414

1515
## Overview
1616

17-
A fast and simple streaming zip file downloader for PHP. Using this library will save you from having to write the Zip to disk. You can directly send it to the user, which is much faster. It can work with S3 buckets or any PSR7 Stream.
17+
A fast and simple streaming zip file downloader for PHP. Using this library will
18+
save you from having to write the Zip to disk. You can directly send it to the
19+
user, which is much faster. It can work with S3 buckets or any PSR7 Stream.
1820

1921
Please see the [LICENSE](LICENSE) file for licensing and warranty information.
2022

2123
## Installation
2224

23-
Simply add a dependency on maennchen/zipstream-php to your project's composer.json file if you use Composer to manage the dependencies of your project. Use following command to add the package to your project's dependencies:
25+
Simply add a dependency on maennchen/zipstream-php to your project's
26+
`composer.json` file if you use Composer to manage the dependencies of your
27+
project. Use following command to add the package to your project's dependencies:
2428

2529
```bash
2630
composer require maennchen/zipstream-php
@@ -29,45 +33,81 @@ composer require maennchen/zipstream-php
2933
## Usage
3034

3135
For detailed instructions, please check the
32-
[Documentation](https://maennchen.dev/ZipStream-PHP/).
33-
34-
Here's a simple example:
36+
[Documentation](https://maennchen.github.io/ZipStream-PHP/).
3537

3638
```php
3739
// Autoload the dependencies
3840
require 'vendor/autoload.php';
3941

40-
// enable output of HTTP headers
41-
$options = new ZipStream\Option\Archive();
42-
$options->setSendHttpHeaders(true);
43-
4442
// create a new zipstream object
45-
$zip = new ZipStream\ZipStream('example.zip', $options);
43+
$zip = new ZipStream\ZipStream(
44+
outputName: 'example.zip',
45+
46+
// enable output of HTTP headers
47+
sendHttpHeaders: true,
48+
);
4649

4750
// create a file named 'hello.txt'
48-
$zip->addFile('hello.txt', 'This is the contents of hello.txt');
51+
$zip->addFile(
52+
fileName: 'hello.txt',
53+
data: 'This is the contents of hello.txt',
54+
);
4955

5056
// add a file named 'some_image.jpg' from a local file 'path/to/image.jpg'
51-
$zip->addFileFromPath('some_image.jpg', 'path/to/image.jpg');
57+
$zip->addFileFromPath(
58+
fileName: 'some_image.jpg',
59+
path: 'path/to/image.jpg',
60+
);
5261

5362
// finish the zip stream
5463
$zip->finish();
5564
```
5665

66+
## Upgrade to version 3.0.0
67+
68+
### General
69+
70+
- Minimum PHP Version: `8.1`
71+
- Only 64bit Architecture is supported.
72+
- The class `ZipStream\Option\Method` has been replaced with the enum `ZipStream\CompressionMethod`.
73+
- Most clases have been flagged as `@internal` and should not be used from the outside.
74+
If you're using internal resources to extend this library, please open an issue so that a clean
75+
interface can be added & published.
76+
The externally available classes & enums are:
77+
- `ZipStream\CompressionMethod`
78+
- `ZipStream\Exception*`
79+
- `ZipStream\ZipStream`
80+
81+
### Archive Options
82+
83+
- The class `ZipStream\Option\Archive` has been replaced in favor of named arguments in the
84+
`ZipStream\ZipStream` constuctor.
85+
- The archive options `largeFileSize` & `largeFileMethod` has been removed. If you want different
86+
`compressionMethods` based on the file size, you'll have to implement this yourself.
87+
- The archive option `httpHeaderCallback` changed the type from `callable` to `Closure`.
88+
- The archive option `zeroHeader` has been replaced with the option `defaultEnableZeroHeader`
89+
and can be overridden for every file.
90+
- The archive option `statFiles` was removed since the library no longer checks filesizes this way.
91+
- The archive option `deflateLevel` has been replaced with the option `defaultDeflateLevel`
92+
and can be overridden for every file.
93+
- The first argument (`name`) of the `ZipStream\ZipStream` constuctor has been replaced with
94+
the named argument `outputName`.
95+
96+
### File Options
97+
98+
- The class `ZipStream\Option\File` has been replaced in favor of named arguments in the
99+
`ZipStream\ZipStream->addFile*` functions.
100+
- The file option `method` has been renamed to `compressionMethod`.
101+
- The file option `time` has been renamed to `lastModificationDateTime`.
102+
- The file option `size` has been renamed to `maxSize`.
103+
57104
## Upgrade to version 2.0.0
58105

59-
- Only the self opened streams will be closed (#139)
60-
If you were relying on ZipStream to close streams that the library didn't open,
61-
you'll need to close them yourself now.
106+
https://github.com/maennchen/ZipStream-PHP/tree/2.0.0#upgrade-to-version-200
62107

63108
## Upgrade to version 1.0.0
64109

65-
- All options parameters to all function have been moved from an `array` to structured option objects. See [the wiki](https://github.com/maennchen/ZipStream-PHP/wiki/Available-options) for examples.
66-
- The whole library has been refactored. The minimal PHP requirement has been raised to PHP 7.1.
67-
68-
## Usage with Symfony and S3
69-
70-
You can find example code on [the wiki](https://github.com/maennchen/ZipStream-PHP/wiki/Symfony-example).
110+
https://github.com/maennchen/ZipStream-PHP/tree/2.0.0#upgrade-to-version-100
71111

72112
## Contributing
73113

composer.json

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,29 @@
2222
}
2323
],
2424
"require": {
25-
"php": "^8.0",
25+
"php-64bit": "^8.1",
2626
"ext-mbstring": "*",
27-
"psr/http-message": "^1.0",
28-
"myclabs/php-enum": "^1.5"
27+
"ext-zlib": "*"
2928
},
3029
"require-dev": {
31-
"phpunit/phpunit": "^8.5.8 || ^9.4.2",
32-
"guzzlehttp/guzzle": "^6.5.3 || ^7.2.0",
30+
"phpunit/phpunit": "^9.5",
31+
"guzzlehttp/guzzle": "^7.5",
3332
"ext-zip": "*",
3433
"mikey179/vfsstream": "^1.6",
35-
"vimeo/psalm": "^5.0",
36-
"php-coveralls/php-coveralls": "^2.4",
37-
"friendsofphp/php-cs-fixer": "^3.9"
34+
"php-coveralls/php-coveralls": "^2.5",
35+
"friendsofphp/php-cs-fixer": "^3.13",
36+
"vimeo/psalm": "^5.0"
37+
},
38+
"suggest": {
39+
"psr/http-message": "^1.0",
40+
"guzzlehttp/psr7": "^2.4"
3841
},
3942
"scripts": {
4043
"format": "PHP_CS_FIXER_IGNORE_ENV=true php-cs-fixer fix",
4144
"test": "composer run test:unit && composer run test:formatted && composer run test:lint",
4245
"test:unit": "phpunit --coverage-clover=coverage.clover.xml --coverage-html cov",
46+
"test:unit:slow": "composer run test:unit -- --group slow",
47+
"test:unit:fast": "composer run test:unit -- --exclude-group slow",
4348
"test:formatted": "composer run format -- --dry-run --stop-on-violation --using-cache=no",
4449
"test:lint": "psalm --stats --show-info --find-unused-psalm-suppress",
4550
"coverage:report": "php-coveralls --coverage_clover=coverage.clover.xml --json_path=coveralls-upload.json --insecure",
@@ -51,6 +56,9 @@
5156
"ZipStream\\": "src/"
5257
}
5358
},
59+
"autoload-dev": {
60+
"psr-4": { "ZipStream\\Test\\": "test/" }
61+
},
5462
"archive": {
5563
"exclude": [
5664
"/composer.lock",

guides/ContentLength.rst

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,32 @@ The following workaround adds an approximated header:
88

99
.. code-block:: php
1010
11-
class Zip
12-
{
13-
/** @var string */
14-
private $name;
11+
use ZipStream\CompressionMethod;
12+
use ZipStream\ZipStream;
1513
14+
class Zip
15+
{
1616
private $files = [];
1717
18-
public function __construct($name)
19-
{
20-
$this->name = $name;
21-
}
18+
public function __construct(
19+
private readonly string $name
20+
) { }
2221
23-
public function addFile($name, $data)
24-
{
22+
public function addFile(
23+
string $name,
24+
string $data,
25+
): void {
2526
$this->files[] = ['type' => 'addFile', 'name' => $name, 'data' => $data];
2627
}
2728
28-
public function addFileFromPath($name, $path)
29-
{
29+
public function addFileFromPath(
30+
string $name,
31+
string $path,
32+
): void {
3033
$this->files[] = ['type' => 'addFileFromPath', 'name' => $name, 'path' => $path];
3134
}
3235
33-
public function getEstimate()
34-
{
36+
public function getEstimate(): int {
3537
$estimate = 22;
3638
foreach ($this->files as $file) {
3739
$estimate += 76 + 2 * strlen($file['name']);
@@ -48,24 +50,28 @@ The following workaround adds an approximated header:
4850
public function finish()
4951
{
5052
header('Content-Length: ' . $this->getEstimate());
51-
$options = new \ZipStream\Option\Archive();
52-
$options->setSendHttpHeaders(true);
53-
$options->setEnableZip64(false);
54-
$options->setDeflateLevel(-1);
55-
$zip = new \ZipStream\ZipStream($this->name, $options);
53+
$zip = new ZipStream(
54+
outputName: $this->name,
55+
SendHttpHeaders: true,
56+
enableZip64: false,
57+
defaultCompressionMethod: CompressionMethod::STORE,
58+
);
5659
57-
$fileOptions = new \ZipStream\Option\File();
58-
$fileOptions->setMethod(\ZipStream\Option\Method::STORE());
5960
foreach ($this->files as $file) {
60-
if ($file['type'] === 'addFile') {
61-
$zip->addFile($file['name'], $file['data'], $fileOptions);
62-
}
63-
if ($file['type'] === 'addFileFromPath') {
64-
$zip->addFileFromPath($file['name'], $file['path'], $fileOptions);
65-
}
61+
if ($file['type'] === 'addFile') {
62+
$zip->addFile(
63+
fileName: $file['name'],
64+
data: $file['data'],
65+
);
66+
}
67+
if ($file['type'] === 'addFileFromPath') {
68+
$zip->addFileFromPath(
69+
fileName: $file['name'],
70+
path: $file['path'],
71+
);
72+
}
6673
}
6774
$zip->finish();
68-
exit;
6975
}
7076
}
7177
@@ -76,4 +82,4 @@ It only works with the following constraints:
7682

7783
Thanks to
7884
`partiellkorrekt <https://github.com/maennchen/ZipStream-PHP/issues/89#issuecomment-1047949274>`_
79-
for this workaround.
85+
for this workaround.

guides/FlySystem.rst

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@ default one, and pass it to Flysystem ``putStream`` method.
2323
$zipStream->addFile('test.txt', 'text');
2424
$zipStream->finish();
2525
26-
// Store File (see Flysystem documentation, and all its framework integration)
27-
$adapter = new Local(__DIR__.'/path/to/folder'); // Can be any adapter (AWS, Google, Ftp, etc.)
26+
// Store File
27+
// (see Flysystem documentation, and all its framework integration)
28+
// Can be any adapter (AWS, Google, Ftp, etc.)
29+
$adapter = new Local(__DIR__.'/path/to/folder');
2830
$filesystem = new Filesystem($adapter);
2931
3032
$filesystem->putStream('test.zip', $tempStream)
3133
3234
// Close Stream
33-
fclose($tempStream);
35+
fclose($tempStream);

0 commit comments

Comments
 (0)