Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Statamic 5 and Laravel 11 #17

Merged
merged 35 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
15f3f1e
Use Pint
aerni May 14, 2024
0ef11b8
Partial composer dependency update
aerni May 14, 2024
bae0117
Fix styling
aerni May 14, 2024
6b71dce
Upgrade stechstudio/laravel-zipstream
aerni Jul 5, 2024
eee34e0
Apply class renames
aerni Jul 5, 2024
fa99166
[Breaking] Rename Zip class to Zipper
aerni Jul 5, 2024
d8b82eb
Bind store in service provider
aerni Jul 5, 2024
445a37e
Add type
aerni Jul 5, 2024
e5ad53b
No need for this anymore
aerni Jul 5, 2024
6a22cde
No need to require this dependency
aerni Jul 5, 2024
70aebf7
Avoid returning URL to empty zip
aerni Jul 5, 2024
82248b5
Simplify and rename method
aerni Jul 5, 2024
1a21411
[Breaking] Add Zipper Facade
aerni Jul 5, 2024
c838fb9
Fix styling
aerni Jul 5, 2024
cdd6528
Update README.md
aerni Jul 5, 2024
440f414
Fix workflow
aerni Jul 5, 2024
535ff88
Fix workflow
aerni Jul 5, 2024
e78a780
Require PHP 8.2
aerni Jul 5, 2024
8a24bf2
Fix workflow
aerni Jul 5, 2024
751ffea
Fix tests
aerni Jul 5, 2024
a1cbb3f
Use Statamic’s addon test case
aerni Jul 5, 2024
6b109ab
Fix tests
aerni Jul 7, 2024
3c939c7
Fix styling
aerni Jul 7, 2024
9363c57
Fix tests
aerni Jul 7, 2024
3913a82
Don’t allow zipping of non-assets
aerni Jul 8, 2024
1683986
Simplify tests
aerni Jul 8, 2024
21573e8
Fix styling
aerni Jul 8, 2024
1ba4c8c
Use stable packages only
aerni Aug 13, 2024
772f567
Fix styling
aerni Aug 13, 2024
e3b1096
Update README.md
aerni Aug 13, 2024
b7de02a
The files are required. Use constructor again.
aerni Aug 13, 2024
28e070f
Fix styling
aerni Aug 13, 2024
d6e23cb
Revert class name change
aerni Aug 13, 2024
13b2a8e
Fix styling
aerni Aug 13, 2024
eb0dc7a
Fix missing rename
aerni Aug 13, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@ jobs:

steps:
- name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v3
with:
ref: ${{ github.head_ref }}

- name: Run PHP CS Fixer
uses: docker://oskarstark/php-cs-fixer-ga
with:
args: --config=.php_cs.dist.php --allow-risky=yes
- name: Fix styling issues
uses: aglipanci/[email protected]

- name: Commit changes
uses: stefanzweifel/git-auto-commit-action@v4
Expand Down
32 changes: 20 additions & 12 deletions .github/workflows/run-tests.yaml
Original file line number Diff line number Diff line change
@@ -1,37 +1,45 @@
name: Run Tests
name: run-tests

on: [push, pull_request, workflow_dispatch]
on: ['push', 'pull_request']

jobs:
phpunit:
runs-on: ubuntu-latest
test:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: true
matrix:
php: [8.0]
laravel: [9.*]
os: [ubuntu-latest]
php: [8.2, 8.3]
laravel: [10.*, 11.*]
stability: [prefer-lowest, prefer-stable]
include:
- laravel: 9.*
testbench: ^7.5
- laravel: 10.*
testbench: 8.*
- laravel: 11.*
testbench: 9.*

name: PHP ${{ matrix.php }} – Laravel ${{ matrix.laravel }} - ${{ matrix.stability }}
name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }}

steps:
- name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo
coverage: none

- name: Setup problem matchers
run: |
echo "::add-matcher::${{ runner.tool_cache }}/php.json"
echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"

- name: Install dependencies
run: |
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update
composer update --${{ matrix.stability }} --prefer-dist --no-interaction

- name: Run tests
- name: Execute tests
run: vendor/bin/phpunit
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
.idea
.env
.phpunit.result.cache
.phpunit.cache
.php_cs.cache
.php-cs-fixer.cache
coverage
Expand Down
35 changes: 0 additions & 35 deletions .php_cs.dist.php

This file was deleted.

88 changes: 15 additions & 73 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,67 +16,9 @@ Publish the config of the package (optional):
php please vendor:publish --tag=zipper-config
```

The following config will be published to `config/zipper.php`:

```php
return [

/*
|--------------------------------------------------------------------------
| Save To Disk
|--------------------------------------------------------------------------
|
| Set this to 'true' to save the created zips to disk.
| The saved file will be used the next time a user requests a zip with the same payload.
|
*/

'save' => false,

/*
|--------------------------------------------------------------------------
| Filesystem Disk
|--------------------------------------------------------------------------
|
| Choose the disk you want to use when saving a zip.
|
*/

'disk' => 'public',

/*
|--------------------------------------------------------------------------
| Link Expiry
|--------------------------------------------------------------------------
|
| Set the time in minutes after which a link should expire.
|
*/

'expiry' => null,

/*
|--------------------------------------------------------------------------
| Cleanup Scope
|--------------------------------------------------------------------------
|
| The scope to use when cleaning up your zip references with the scheduled command.
|
| Options:
| "expired": Only delete expired reference files
| "all": Delete all reference files excluding unexpired files
| "force": Delete all reference files including unexpired files
|
*/

'cleanup' => 'expired',

];
```

## Basic Usage

To create a zip of your assets, you have to call the `zip` tag followed by the `variable` containing your assets. The tag returns the URL to the route that handles creating the zip. The zip will be streamed without being saved to disk. You may opt in to save the file to disk to be used on subsequent requests.
To create a zip of your assets, you must call the `zip` tag followed by the `variable` containing your assets. The tag returns the URL to the route that handles creating the zip. The zip will be streamed without being saved to disk, but you may opt-in to save the file to disk for later use.

Somewhere in your content files:

Expand All @@ -86,43 +28,43 @@ images:
- snes.jpg
```

Somehwere in your views:
Somewhere in your views:

```antlers
{{ zip:images }}
```

### Filename

You may optionally pass a filename using the `filename` parameter. The filename defaults to the timestamp when the Zip object was created. The example below binds the name of the zip to the title of the page.
You may optionally pass a filename using the `filename` parameter. The filename defaults to the current timestamp when the Zip object is created. The example below binds the zip name to the page title.

```antlers
{{ zip:images :filename="title" }}
```

### Link Expiry

If you want to expire your links after a certain time, you can either set the expiry globally in the config, or use the `expiry` parameter on the tag. The expiry is to be set in minutes. Note, that the expiry on the tag will overide the expiry in the config.
If you want to expire your links after a certain time, you can either set the expiry globally in the config or use the `expiry` parameter on the tag. The expiry is to be set in minutes. Note that the expiry on the tag will override the expiry in the config.

```antlers
{{ zip:images expiry="60" }}
```

## Cleanup Old References

Zipper saves an encrypted instance of the Zip class every time it returns a URL. These reference files are stored in `storage/zipper/{id}`. Whenever a user downloads a zip, Zipper will retrieve and decrypt the requested Zip instance.
Zipper saves an encrypted instance of the Zip class every time it returns a URL. This class is later retrieved and decrypted when a user downloads a zip. These reference files are stored in `storage/zipper/{id}`.

With time, the amound of saved reference files will grow. To get this under control, Zipper provides a scheduled command that will daily delete old reference files. Just make sure that your Scheduler is running.
With time, the number of saved reference files will grow. To control this, Zipper provides a scheduled command that will delete old reference files daily. Just make sure that your Scheduler is running.

### Cleanup Scopes

There are a couple of cleanup scopes you can define in the config:
There are a couple of cleanup scopes to choose from in the config.:

| Option | Description |
|-----------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `expired` | Only delete expired references files. This only affects references of zips that used the `expiry` option |
| `all` | Delete all reference files excluding unexpired files. This will delete references of zips that didn't use the expiry option as well as expired zips. It will not delete unexpired zips. |
| `force` | Delete all reference files including unexpired files. This will completely wipe all references. |
| Option | Description |
|-----------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `expired` | Only delete expired references files. This only affects references of zips that used the `expiry` option |
| `all` | Delete all reference files, excluding unexpired files. This will delete references of expired zips and zips that didn't use the expiry option. It will not delete unexpired zips. |
| `force` | Delete all reference files, including unexpired files. This will completely wipe all references. |

### Clean Command

Expand All @@ -136,12 +78,12 @@ php please zipper:clean --scope=force

## Advanced Usage

You may also use this addon programmatically as shown below.
You may also use this addon programmatically, as shown below.

```php
use Aerni\Zipper\Zip;

// Prepare an array of Statamic assets, paths or URLs.
// Prepare an array of Statamic assets, paths, or URLs.
$files = [
Statamic\Assets\Asset,
'/home/ploi/site.com/storage/app/assets/file_1.jpg',
Expand All @@ -152,7 +94,7 @@ $files = [
$zip = Zip::make($files);

// Set an optional filename. This defaults to the timestamp when the object was created.
$zip->filename('obi-wan-kenobi')
$zip->filename('obi-wan-kenobi');

// Set an optional expiry time in minutes. This defaults to the expiry set in the config.
$zip->expiry(60);
Expand Down
18 changes: 8 additions & 10 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,15 @@
}
],
"require": {
"php": "^8.0",
"aws/aws-sdk-php": "^3.224",
"laravel/framework": "^9.0 || ^10.0",
"statamic/cms": "^4.0",
"stechstudio/laravel-zipstream": "^4.8"
"php": "^8.2",
"laravel/framework": "^10.0 || ^11.0",
"statamic/cms": "^5.0",
"stechstudio/laravel-zipstream": "^5.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.8",
"nunomaduro/collision": "^6.1",
"orchestra/testbench": "^7.5",
"phpunit/phpunit": "^9.5"
"nunomaduro/collision": "^7.0 || ^8.0",
"orchestra/testbench": "^8.0 || ^9.0",
"phpunit/phpunit": "^10.0"
},
"autoload": {
"psr-4": {
Expand Down Expand Up @@ -65,6 +63,6 @@
]
}
},
"minimum-stability": "dev",
"minimum-stability": "stable",
"prefer-stable": true
}
43 changes: 19 additions & 24 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
verbose="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
printerClass="NunoMaduro\Collision\Adapters\Phpunit\Printer">
<testsuites>
<testsuite name="Test Suite">
<directory>./tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
<php>
<env name="APP_KEY" value="base64:xstw340kWV7AmdlgzjEAqc8pAQjcRJZ1UeHR5/LCAA4="/>
</php>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" bootstrap="vendor/autoload.php" colors="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" processIsolation="false" stopOnFailure="false" beStrictAboutTestsThatDoNotTestAnything="false" cacheDirectory=".phpunit.cache">
<testsuites>
<testsuite name="Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<php>
<env name="APP_ENV" value="testing"/>
<env name="APP_KEY" value="base64:ybcI9MKuhLnESRSuWDfnJQuohOXMBaynfbTC5Y5i1FE="/>
<env name="BCRYPT_ROUNDS" value="4"/>
<env name="CACHE_DRIVER" value="array"/>
<!-- <env name="DB_CONNECTION" value="sqlite"/> -->
<!-- <env name="DB_DATABASE" value=":memory:"/> -->
<env name="MAIL_MAILER" value="array"/>
<env name="QUEUE_CONNECTION" value="sync"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="TELESCOPE_ENABLED" value="false"/>
</php>
<source/>
</phpunit>
2 changes: 1 addition & 1 deletion routes/actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
use Aerni\Zipper\Http\Middleware\VerifyRequest;
use Illuminate\Support\Facades\Route;

Route::get('{id}', [ZipperController::class, '__invoke'])
Route::get('{id}', ZipperController::class)
->middleware(VerifyRequest::class)
->name('zipper.create');
4 changes: 2 additions & 2 deletions src/Http/Controllers/ZipperController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

use Aerni\Zipper\Facades\ZipperStore;
use Statamic\Http\Controllers\Controller;
use STS\ZipStream\ZipStream;
use STS\ZipStream\Builder;
use Symfony\Component\HttpFoundation\StreamedResponse;

class ZipperController extends Controller
{
public function __invoke(string $id): ZipStream|StreamedResponse
public function __invoke(string $id): Builder|StreamedResponse
{
return ZipperStore::get($id)->get();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Middleware/VerifyRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function handle($request, Closure $next)
}

if (! ZipperStore::exists($request->route()->parameter('id'))) {
throw new NotFoundHttpException();
throw new NotFoundHttpException;
}

return $next($request);
Expand Down
Loading
Loading