Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
34 changes: 30 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
push:
branches:
- master
- cake6
pull_request:
branches:
- '*'
Expand All @@ -13,10 +14,35 @@ permissions:

jobs:
testsuite:
uses: ADmad/.github/.github/workflows/testsuite-without-db.yml@master
secrets:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
runs-on: ubuntu-24.04
strategy:
fail-fast: false
matrix:
glide: ['^3.0', '^4.0']

steps:
- uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.5'
extensions: mbstring, intl, pdo_sqlite, gd
ini-values: zend.assertions=1
coverage: pcov

- name: Install dependencies
run: composer require 'league/glide:${{ matrix.glide }}' --no-interaction --prefer-dist

- name: Run PHPUnit
run: vendor/bin/phpunit --display-all-issues --fail-on-all-issues --coverage-clover=coverage.xml

- name: Code Coverage Report
if: matrix.glide == '^4.0'
uses: codecov/codecov-action@v5
with:
token: ${{ secrets.CODECOV_TOKEN }}

cs-stan:
uses: ADmad/.github/.github/workflows/cs-stan.yml@master
Comment thread
josbeir marked this conversation as resolved.
Resolved
uses: ADmad/.github/.github/workflows/cs-stan.yml@cake6
secrets: inherit
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ Load the plugin using CLI:
bin/cake plugin load ADmad/Glide
```

## Upgrading from the CakePHP 5 version

Protected properties and methods no longer have a leading underscore, following
CakePHP 6 conventions. Update any subclasses accordingly, including overrides of
`$defaultConfig`. Failure event listeners must use `$event->setResult($response)`
instead of returning the response, as shown below.

## Usage

### Middleware
Expand Down Expand Up @@ -114,15 +121,16 @@ the middleware will throw a `ADmad\Glide\Exception\SignatureException` exception
If a response image could not be generated by the Glide library then by default
the middleware will throw a `ADmad\Glide\Exception\ResponseException`.
A `Glide.response_failure` event will also be triggered in this case. You can setup a
listener for this event and return a response instance from it. In this case
listener for this event and set a response instance as the event result. In this case
that response will be sent to client and `ResponseException` will not be thrown.

```php
\Cake\Event\EventManager::instance()->on(
\ADmad\Glide\Middleware\GlideMiddleware::RESPONSE_FAILURE_EVENT,
function ($event) {
return (new Response())
->withFile('/path/to/default-image.jpg');
$event->setResult(
(new \Cake\Http\Response())->withFile('/path/to/default-image.jpg'),
);
}
);
```
Expand Down
8 changes: 5 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@
"issues":"https://github.com/ADmad/cakephp-glide/issues"
},
"require": {
"php": ">=8.1",
"cakephp/cakephp": "^5.0",
"php": ">=8.5",
"cakephp/cakephp": "^6.0",
"league/glide": "^3.0 || ^4.0"
},
"require-dev": {
"phpunit/phpunit": "^10.1 || ^11.5.3 || ^12.1.3 || ^13.0"
"phpunit/phpunit": "^13.0"
},
"autoload": {
"psr-4": {
Expand All @@ -46,6 +46,8 @@
"TestApp\\": "tests/test_app/src"
}
},
"minimum-stability": "dev",
"prefer-stable": true,
"config": {
"sort-packages": true,
"allow-plugins": {
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" colors="true" processIsolation="false" stopOnFailure="false" bootstrap="./tests/bootstrap.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.2/phpunit.xsd" cacheDirectory=".phpunit.cache">
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" colors="true" processIsolation="false" stopOnFailure="false" bootstrap="./tests/bootstrap.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/13.0/phpunit.xsd" cacheDirectory=".phpunit.cache">
<testsuites>
<testsuite name="ADmad/Glide TestSuite">
<directory>./tests/TestCase</directory>
Expand Down
60 changes: 30 additions & 30 deletions src/Middleware/GlideMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ class GlideMiddleware implements MiddlewareInterface, EventDispatcherInterface

use InstanceConfigTrait;

public const RESPONSE_FAILURE_EVENT = 'Glide.response_failure';
public const string RESPONSE_FAILURE_EVENT = 'Glide.response_failure';

/**
* Default config.
*
* @var array
*/
protected array $_defaultConfig = [
protected array $defaultConfig = [
'path' => null,
'cacheTime' => '+1 days',
'server' => [
Expand All @@ -59,7 +59,7 @@ class GlideMiddleware implements MiddlewareInterface, EventDispatcherInterface
*
* @var string
*/
protected string $_path = '';
protected string $path = '';

/**
* Constructor.
Expand All @@ -86,21 +86,21 @@ public function __construct(array $config = [], ?EventManagerInterface $eventMan
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$uri = $request->getUri();
$this->_path = urldecode($uri->getPath());
$this->path = urldecode($uri->getPath());

$config = $this->getConfig();

if ($config['path'] && strpos($this->_path, $config['path']) !== 0) {
if ($config['path'] && strpos($this->path, $config['path']) !== 0) {
return $handler->handle($request);
}

$this->_checkSignature($request);
$this->checkSignature($request);

$server = $this->_getServer($config['server']);
$server = $this->getServer($config['server']);

$modifiedTime = null;
if ($config['cacheTime']) {
$return = $this->_checkModified($request, $server);
$return = $this->checkModified($request, $server);
if ($return === null) {
return $handler->handle($request);
}
Expand All @@ -110,21 +110,21 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
$modifiedTime = $return;
}

$response = $this->_getResponse($request, $server);
$response = $this->getResponse($request, $server);
if ($response === null) {
return $handler->handle($request);
}

if ($config['cacheTime']) {
/** @psalm-suppress PossiblyNullArgument */
$response = $this->_withCacheHeaders(
$response = $this->withCacheHeaders(
$response,
$config['cacheTime'],
$modifiedTime,
);
}

$response = $this->_withCustomHeaders($response);
$response = $this->withCustomHeaders($response);

return $response;
}
Expand All @@ -135,7 +135,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
* @param callable|array $config Config array or callable.
* @return \League\Glide\Server
*/
protected function _getServer(callable|array $config): Server
protected function getServer(callable|array $config): Server
{
if (is_array($config)) {
return ServerFactory::create($config);
Expand All @@ -151,7 +151,7 @@ protected function _getServer(callable|array $config): Server
* @throws \ADmad\Glide\Exception\SignatureException
* @return void
*/
protected function _checkSignature(ServerRequestInterface $request): void
protected function checkSignature(ServerRequestInterface $request): void
{
if (!$this->getConfig('security.secureUrls')) {
return;
Expand All @@ -160,7 +160,7 @@ protected function _checkSignature(ServerRequestInterface $request): void
$signKey = $this->getConfig('security.signKey') ?: Security::getSalt();
try {
SignatureFactory::create($signKey)->validateRequest(
$this->_path,
$this->path,
$request->getQueryParams(),
);
} catch (Exception $exception) {
Expand All @@ -178,18 +178,18 @@ protected function _checkSignature(ServerRequestInterface $request): void
* @param \League\Glide\Server $server Glide server.
* @return \Psr\Http\Message\ResponseInterface|int|null
*/
protected function _checkModified(ServerRequestInterface $request, Server $server): ResponseInterface|int|null
protected function checkModified(ServerRequestInterface $request, Server $server): ResponseInterface|int|null
{
try {
$modifiedTime = $server->getSource()
->lastModified($server->getSourcePath($this->_path));
->lastModified($server->getSourcePath($this->path));
} catch (Exception $exception) {
return $this->_handleException($request, $exception);
return $this->handleException($request, $exception);
}

if ($this->_isNotModified($request, $modifiedTime)) {
if ($this->isNotModified($request, $modifiedTime)) {
$response = new Response(['status' => 304]);
$response = $this->_withCustomHeaders($response);
$response = $this->withCustomHeaders($response);

return $response->withHeader('Last-Modified', (string)$modifiedTime);
}
Expand All @@ -204,7 +204,7 @@ protected function _checkModified(ServerRequestInterface $request, Server $serve
* @param \League\Glide\Server $server Glide server.
* @return \Psr\Http\Message\ResponseInterface|null Response instance on success else null
*/
protected function _getResponse(ServerRequestInterface $request, Server $server): ?ResponseInterface
protected function getResponse(ServerRequestInterface $request, Server $server): ?ResponseInterface
{
$queryParams = $request->getQueryParams();
$allowedParams = $this->getConfig('allowedParams');
Expand All @@ -219,9 +219,9 @@ protected function _getResponse(ServerRequestInterface $request, Server $server)
&& $this->getConfig('originalPassThrough')
) {
try {
$response = $this->_passThrough($request, $server);
$response = $this->passThrough($request, $server);
} catch (Exception $exception) {
return $this->_handleException($request, $exception);
return $this->handleException($request, $exception);
}

return $response;
Expand All @@ -234,9 +234,9 @@ protected function _getResponse(ServerRequestInterface $request, Server $server)
}

try {
$response = $server->getImageResponse($this->_path, $queryParams);
$response = $server->getImageResponse($this->path, $queryParams);
} catch (Exception $exception) {
return $this->_handleException($request, $exception);
return $this->handleException($request, $exception);
}

return $response;
Expand All @@ -250,10 +250,10 @@ protected function _getResponse(ServerRequestInterface $request, Server $server)
* @throws \ADmad\Glide\Exception\ResponseException
* @return \Psr\Http\Message\ResponseInterface Response instance
*/
protected function _passThrough(ServerRequestInterface $request, Server $server): ?ResponseInterface
protected function passThrough(ServerRequestInterface $request, Server $server): ?ResponseInterface
{
$source = $server->getSource();
$path = $server->getSourcePath($this->_path);
$path = $server->getSourcePath($this->path);

$resource = $source->readStream($path);
$stream = new Stream($resource);
Expand Down Expand Up @@ -283,7 +283,7 @@ protected function _passThrough(ServerRequestInterface $request, Server $server)
* @param string|int $modifiedTime Last modified time of file.
* @return bool
*/
protected function _isNotModified(ServerRequestInterface $request, string|int $modifiedTime): bool
protected function isNotModified(ServerRequestInterface $request, string|int $modifiedTime): bool
{
$modifiedSince = $request->getHeaderLine('If-Modified-Since');
if (!$modifiedSince) {
Expand All @@ -301,7 +301,7 @@ protected function _isNotModified(ServerRequestInterface $request, string|int $m
* @param string|int $modifiedTime Modified time.
* @return \Psr\Http\Message\ResponseInterface
*/
protected function _withCacheHeaders(
protected function withCacheHeaders(
ResponseInterface $response,
string $cacheTime,
string|int $modifiedTime,
Expand All @@ -323,7 +323,7 @@ protected function _withCacheHeaders(
* @param \Psr\Http\Message\ResponseInterface $response The response.
* @return \Psr\Http\Message\ResponseInterface
*/
protected function _withCustomHeaders(ResponseInterface $response): ResponseInterface
protected function withCustomHeaders(ResponseInterface $response): ResponseInterface
{
foreach ((array)$this->getConfig('headers') as $key => $value) {
$response = $response->withHeader($key, $value);
Expand All @@ -340,7 +340,7 @@ protected function _withCustomHeaders(ResponseInterface $response): ResponseInte
* @throws \ADmad\Glide\Exception\ResponseException
* @return \Psr\Http\Message\ResponseInterface|null
*/
protected function _handleException(ServerRequestInterface $request, Exception $exception): ?ResponseInterface
protected function handleException(ServerRequestInterface $request, Exception $exception): ?ResponseInterface
{
$event = $this->dispatchEvent(
static::RESPONSE_FAILURE_EVENT,
Expand Down
12 changes: 6 additions & 6 deletions src/View/Helper/GlideHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class GlideHelper extends Helper
*
* @var array<string, mixed>
*/
protected array $_defaultConfig = [
protected array $defaultConfig = [
'baseUrl' => '/images/',
'secureUrls' => false,
'signKey' => null,
Expand All @@ -45,7 +45,7 @@ class GlideHelper extends Helper
*
* @var \League\Glide\Urls\UrlBuilder|null
*/
protected ?UrlBuilder $_urlBuilder = null;
protected ?UrlBuilder $urlBuilder = null;

/**
* Creates a formatted IMG element.
Expand Down Expand Up @@ -97,18 +97,18 @@ public function url(string $path, array $params = []): string
public function urlBuilder(?UrlBuilder $urlBuilder = null): UrlBuilder
{
if ($urlBuilder !== null) {
return $this->_urlBuilder = $urlBuilder;
return $this->urlBuilder = $urlBuilder;
}

if (!isset($this->_urlBuilder)) {
if (!isset($this->urlBuilder)) {
$config = $this->getConfig();

$this->_urlBuilder = UrlBuilderFactory::create(
$this->urlBuilder = UrlBuilderFactory::create(
$config['baseUrl'],
$config['secureUrls'] ? ($config['signKey'] ?: Security::getSalt()) : null,
);
}

return $this->_urlBuilder;
return $this->urlBuilder;
}
}
Loading
Loading