Skip to content

Commit ce35455

Browse files
authored
Merge pull request #32 from ADmad/glide-2-official
Upgrade to Glide 2 (Flysystem 2).
2 parents 3de5b2e + 0811a91 commit ce35455

File tree

6 files changed

+41
-41
lines changed

6 files changed

+41
-41
lines changed

.github/workflows/ci.yml

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,18 @@ jobs:
1515
composer-opts: '--prefer-lowest'
1616

1717
steps:
18-
- uses: actions/checkout@v1
19-
with:
20-
fetch-depth: 1
18+
- uses: actions/checkout@v2
2119

2220
- name: Setup PHP
2321
uses: shivammathur/setup-php@v2
2422
with:
2523
php-version: ${{ matrix.php-version }}
26-
extension-csv: mbstring, intl
24+
extensions: mbstring, intl
2725
coverage: pcov
2826

2927
- name: Composer install
3028
run: |
31-
if [[ ${{ matrix.php-version }} == '8.0' ]]; then
32-
composer update --ignore-platform-reqs
33-
else
34-
composer update ${{ matrix.composer-opts }}
35-
fi
29+
composer update ${{ matrix.composer-opts }}
3630
3731
- name: Run PHPUnit
3832
run: |
@@ -51,20 +45,18 @@ jobs:
5145
runs-on: ubuntu-18.04
5246

5347
steps:
54-
- uses: actions/checkout@v1
55-
with:
56-
fetch-depth: 1
48+
- uses: actions/checkout@v2
5749

5850
- name: Setup PHP
5951
uses: shivammathur/setup-php@v2
6052
with:
6153
php-version: '7.4'
62-
extension-csv: mbstring, intl
54+
extensions: mbstring, intl
6355
coverage: none
64-
tools: cs2pr, psalm:^4.1
56+
tools: cs2pr, psalm:^4.8
6557

6658
- name: Composer Install
67-
run: composer require cakephp/cakephp-codesniffer:^4.1
59+
run: composer require cakephp/cakephp-codesniffer:^4.5
6860

6961
- name: Run phpcs
7062
run: vendor/bin/phpcs --report=checkstyle -q --standard=vendor/cakephp/cakephp-codesniffer/CakePHP src/ tests/ | cs2pr

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ In your `config/routes.php` setup the `GlideMiddleware` for required scope which
3131
intercepts requests and serves images generated by Glide. For e.g.:
3232

3333
```php
34-
Router::scope('/images', function ($routes) {
34+
$routes->scope('/images', function ($routes) {
3535
$routes->registerMiddleware('glide', new \ADmad\Glide\Middleware\GlideMiddleware([
3636
// Run this middleware only for URLs starting with specified string. Default null.
3737
// Setting this option is required only if you want to setup the middleware
@@ -55,7 +55,7 @@ Router::scope('/images', function ($routes) {
5555
'base_url' => '/images/',
5656

5757
// Response class for serving images. If unset (default) an instance of
58-
// \ADmad\Glide\Responses\PsrResponseFactory() will be used.
58+
// \ADmad\Glide\Response\PsrResponseFactory() will be used.
5959
// http://glide.thephpleague.com/1.0/config/responses/
6060
'response' => null,
6161
],
@@ -121,7 +121,7 @@ images. You can load the helper in your `AppView::initialize()` as shown in
121121
example below:
122122

123123
```php
124-
public function initialize()
124+
public function initialize(): void
125125
{
126126
// All option values should match the corresponding options for `GlideFilter`.
127127
$this->loadHelper('ADmad/Glide.Glide', [

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
},
3030
"require": {
3131
"cakephp/cakephp": "^4.0.2",
32-
"league/glide": "^1.6"
32+
"league/glide": "^2.0"
3333
},
3434
"require-dev": {
3535
"phpunit/phpunit": "^8.5 || ^9.3"

src/Middleware/GlideMiddleware.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55

66
use ADmad\Glide\Exception\ResponseException;
77
use ADmad\Glide\Exception\SignatureException;
8-
use ADmad\Glide\Responses\PsrResponseFactory;
8+
use ADmad\Glide\Response\PsrResponseFactory;
9+
use Cake\Core\Configure;
910
use Cake\Core\InstanceConfigTrait;
1011
use Cake\Event\EventDispatcherInterface;
1112
use Cake\Event\EventDispatcherTrait;
@@ -180,7 +181,7 @@ protected function _checkModified(ServerRequestInterface $request, Server $serve
180181
try {
181182
/** @var int|string|false $modifiedTime */
182183
$modifiedTime = $server->getSource()
183-
->getTimestamp($server->getSourcePath($this->_path));
184+
->lastModified($server->getSourcePath($this->_path));
184185
} catch (Exception $exception) {
185186
return $this->_handleException($request, $exception);
186187
}
@@ -254,13 +255,10 @@ protected function _passThrough(ServerRequestInterface $request, Server $server)
254255
$path = $server->getSourcePath($this->_path);
255256

256257
$resource = $source->readStream($path);
257-
if ($resource === false) {
258-
throw new ResponseException();
259-
}
260258
$stream = new Stream($resource);
261259

262-
$contentType = $source->getMimetype($path);
263-
$contentLength = $source->getSize($path);
260+
$contentType = $source->mimeType($path);
261+
$contentLength = $source->fileSize($path);
264262

265263
/** @psalm-suppress PossiblyFalseArgument */
266264
return (new Response())->withBody($stream)
@@ -345,6 +343,10 @@ protected function _handleException(ServerRequestInterface $request, $exception)
345343
return $result;
346344
}
347345

346+
if (Configure::read('debug')) {
347+
throw $exception;
348+
}
349+
348350
throw new ResponseException(null, null, $exception);
349351
}
350352
}
Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,33 @@
11
<?php
22
declare(strict_types=1);
33

4-
namespace ADmad\Glide\Responses;
4+
namespace ADmad\Glide\Response;
55

66
use ADmad\Glide\Exception\ResponseException;
77
use Cake\Http\Response;
88
use Laminas\Diactoros\Stream;
9-
use League\Flysystem\FilesystemInterface;
10-
use League\Glide\Filesystem\FilesystemException;
9+
use League\Flysystem\FilesystemOperator;
1110
use League\Glide\Responses\ResponseFactoryInterface;
1211

1312
class PsrResponseFactory implements ResponseFactoryInterface
1413
{
1514
/**
1615
* Create response.
1716
*
18-
* @param \League\Flysystem\FilesystemInterface $cache Cache file system.
17+
* @param \League\Flysystem\FilesystemOperator $cache Cache file system.
1918
* @param string $path Cached file path.
2019
* @return \Psr\Http\Message\ResponseInterface Response object.
2120
*/
22-
public function create(FilesystemInterface $cache, $path)
21+
public function create(FilesystemOperator $cache, $path)
2322
{
2423
$resource = $cache->readStream($path);
2524
if ($resource === false) {
2625
throw new ResponseException();
2726
}
2827
$stream = new Stream($resource);
2928

30-
$contentType = $cache->getMimetype($path);
31-
$contentLength = $cache->getSize($path);
32-
33-
if ($contentType === false) {
34-
throw new FilesystemException('Unable to determine the image content type.');
35-
}
36-
37-
if ($contentLength === false) {
38-
throw new FilesystemException('Unable to determine the image content length.');
39-
}
29+
$contentType = $cache->mimeType($path);
30+
$contentLength = $cache->fileSize($path);
4031

4132
return (new Response())->withBody($stream)
4233
->withHeader('Content-Type', $contentType)

tests/TestCase/Middleware/GlideMiddlewareTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66
use ADmad\Glide\Exception\ResponseException;
77
use ADmad\Glide\Exception\SignatureException;
88
use ADmad\Glide\Middleware\GlideMiddleware;
9+
use Cake\Core\Configure;
910
use Cake\Event\EventManager;
1011
use Cake\Http\Response;
1112
use Cake\Http\ServerRequestFactory;
1213
use Cake\TestSuite\TestCase;
1314
use Cake\Utility\Security;
15+
use League\Flysystem\UnableToRetrieveMetadata;
1416
use League\Glide\ServerFactory;
1517
use League\Glide\Signatures\Signature;
1618
use TestApp\Http\TestRequestHandler;
@@ -180,15 +182,28 @@ public function testSignatureException()
180182
$middleware->process($request, $this->handler);
181183
}
182184

185+
public function test3rdPartyException()
186+
{
187+
$middleware = new GlideMiddleware($this->config);
188+
$request = ServerRequestFactory::fromGlobals([
189+
'REQUEST_URI' => '/images/non-existent.jpg',
190+
]);
191+
192+
$this->expectException(UnableToRetrieveMetadata::class);
193+
$middleware->process($request, $this->handler);
194+
}
195+
183196
public function testResponseException()
184197
{
185198
$middleware = new GlideMiddleware($this->config);
186199
$request = ServerRequestFactory::fromGlobals([
187200
'REQUEST_URI' => '/images/non-existent.jpg',
188201
]);
189202

203+
Configure::write('debug', false);
190204
$this->expectException(ResponseException::class);
191205
$middleware->process($request, $this->handler);
206+
Configure::write('debug', true);
192207
}
193208

194209
public function testExceptionEventListener()

0 commit comments

Comments
 (0)