Skip to content

Commit

Permalink
Merge pull request #39 from ADmad/cake-5
Browse files Browse the repository at this point in the history
Cake 5
  • Loading branch information
ADmad committed Sep 28, 2023
2 parents cffd2da + d6ecf3f commit 0ae85da
Show file tree
Hide file tree
Showing 13 changed files with 107 additions and 108 deletions.
5 changes: 3 additions & 2 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
.gitattributes export-ignore
.gitignore export-ignore
phpunit.xml.dist export-ignore
.travis.yml export-ignore
tests export-ignore
psalm.xml
psalm.xml export-ignore
phpstan.neon export-ignore
.github export-ignore
73 changes: 14 additions & 59 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,66 +1,21 @@
name: CI

on: [push, pull_request]
on:
push:
branches:
- master
pull_request:
branches:
- '*'

permissions:
contents: read

jobs:
testsuite:
runs-on: ubuntu-18.04
strategy:
fail-fast: false
matrix:
php-version: ['7.4', '8.0', '8.1']
composer-opts: ['']
include:
- php-version: '7.2'
composer-opts: '--prefer-lowest'

steps:
- uses: actions/checkout@v2

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
extensions: mbstring, intl
coverage: pcov

- name: Composer install
run: |
composer update ${{ matrix.composer-opts }}
- name: Run PHPUnit
run: |
if [[ ${{ matrix.php-version }} == '7.4' ]]; then
vendor/bin/phpunit --coverage-clover=coverage.xml
else
vendor/bin/phpunit
fi
- name: Code Coverage Report
if: matrix.php-version == '7.4'
uses: codecov/codecov-action@v2
uses: ADmad/.github/.github/workflows/testsuite-without-db.yml@master
secrets: inherit

cs-stan:
name: Coding Standard & Static Analysis
runs-on: ubuntu-18.04

steps:
- uses: actions/checkout@v2

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '7.4'
extensions: mbstring, intl
coverage: none
tools: cs2pr, vimeo/psalm:^4

- name: Composer Install
run: composer require cakephp/cakephp-codesniffer:^4.5

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

- name: Run psalm
if: success() || failure()
run: psalm --output-format=github
uses: ADmad/.github/.github/workflows/cs-stan.yml@master
secrets: inherit
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/composer.lock
/vendor
.phpunit.cache
.phpunit.result.cache
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# CakePHP Glide

[![Build Status](https://img.shields.io/github/workflow/status/ADmad/cakephp-glide/CI/master?style=flat-square)](https://github.com/ADmad/cakephp-glide/actions?query=workflow%3ACI+branch%3Amaster)
[![Build Status](https://img.shields.io/github/actions/workflow/status/ADmad/cakephp-glide/ci.yml?style=flat-square&branch=master)](https://github.com/ADmad/cakephp-glide/actions?query=workflow%3ACI+branch%3Amaster)
[![Coverage Status](https://img.shields.io/codecov/c/github/ADmad/cakephp-glide.svg?style=flat-square)](https://codecov.io/github/ADmad/cakephp-glide)
[![Total Downloads](https://img.shields.io/packagist/dt/ADmad/cakephp-glide.svg?style=flat-square)](https://packagist.org/packages/ADmad/cakephp-glide)
[![License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](LICENSE.txt)
Expand Down
12 changes: 9 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@
"issues":"https://github.com/ADmad/cakephp-glide/issues"
},
"require": {
"cakephp/cakephp": "^4.0.2",
"league/glide": "^2.0"
"cakephp/cakephp": "^5.0",
"league/glide": "^2.3"
},
"require-dev": {
"phpunit/phpunit": "^8.5 || ^9.3"
"phpunit/phpunit": "^10.1"
},
"autoload": {
"psr-4": {
Expand All @@ -44,5 +44,11 @@
"ADmad\\Glide\\Test\\": "tests",
"TestApp\\": "tests/test_app/src"
}
},
"config": {
"sort-packages": true,
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
}
}
7 changes: 4 additions & 3 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
parameters:
level: 6
ignoreErrors:
- '#Access to an undefined property ADmad\\Glide\\View\\Helper\\GlideHelper::\$request#'
level: 8
checkMissingIterableValueType: false
paths:
- src/
29 changes: 10 additions & 19 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,26 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
colors="true"
processIsolation="false"
stopOnFailure="false"
bootstrap="./tests/bootstrap.php"
>
<php>
<ini name="memory_limit" value="-1"/>
</php>

<!-- Add any additional test suites you want to run here -->
<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">
<testsuites>
<testsuite name="ADmad/Glide TestSuite">
<directory>./tests/TestCase</directory>
</testsuite>
<!-- Add plugin test suites here. -->
</testsuites>

<filter>
<whitelist>
<directory suffix=".php">./src/</directory>
<directory suffix=".ctp">./src/</directory>
</whitelist>
</filter>
<extensions>
<bootstrap class="Cake\TestSuite\Fixture\Extension\PHPUnitExtension"/>
</extensions>
<coverage/>
<source>
<include>
<directory suffix=".php">src/</directory>
</include>
</source>
</phpunit>
3 changes: 2 additions & 1 deletion psalm.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?xml version="1.0"?>
<psalm
errorLevel="2"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
findUnusedBaselineEntry="true"
findUnusedCode="false"
>
<projectFiles>
<directory name="src" />
Expand Down
30 changes: 30 additions & 0 deletions src/GlidePlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
declare(strict_types=1);

namespace ADmad\Glide;

use Cake\Core\BasePlugin;

class GlidePlugin extends BasePlugin
{
/**
* Do bootstrapping or not
*
* @var bool
*/
protected bool $bootstrapEnabled = false;

/**
* Console middleware
*
* @var bool
*/
protected bool $consoleEnabled = false;

/**
* Load routes or not
*
* @var bool
*/
protected bool $routesEnabled = false;
}
36 changes: 23 additions & 13 deletions src/Middleware/GlideMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,16 @@
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;

/**
* @implements \Cake\Event\EventDispatcherInterface<\ADmad\Glide\Middleware\GlideMiddleware>
*/
class GlideMiddleware implements MiddlewareInterface, EventDispatcherInterface
{
/**
* @use \Cake\Event\EventDispatcherTrait<\ADmad\Glide\Middleware\GlideMiddleware>
*/
use EventDispatcherTrait;

use InstanceConfigTrait;

public const RESPONSE_FAILURE_EVENT = 'Glide.response_failure';
Expand All @@ -35,7 +42,7 @@ class GlideMiddleware implements MiddlewareInterface, EventDispatcherInterface
*
* @var array
*/
protected $_defaultConfig = [
protected array $_defaultConfig = [
'path' => null,
'cacheTime' => '+1 days',
'server' => [
Expand All @@ -56,7 +63,7 @@ class GlideMiddleware implements MiddlewareInterface, EventDispatcherInterface
*
* @var string
*/
protected $_path = '';
protected string $_path = '';

/**
* Constructor.
Expand Down Expand Up @@ -129,10 +136,10 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
/**
* Get glide server instance.
*
* @param array|callable $config Config array or callable.
* @param callable|array $config Config array or callable.
* @return \League\Glide\Server
*/
protected function _getServer($config): Server
protected function _getServer(callable|array $config): Server
{
if (is_array($config)) {
return ServerFactory::create($config);
Expand All @@ -148,7 +155,7 @@ protected function _getServer($config): Server
* @throws \ADmad\Glide\Exception\SignatureException
* @return void
*/
protected function _checkSignature(ServerRequestInterface $request)
protected function _checkSignature(ServerRequestInterface $request): void
{
if (!$this->getConfig('security.secureUrls')) {
return;
Expand All @@ -175,12 +182,12 @@ protected function _checkSignature(ServerRequestInterface $request)
* @param \League\Glide\Server $server Glide server.
* @return \Psr\Http\Message\ResponseInterface|int|null
*/
protected function _checkModified(ServerRequestInterface $request, Server $server)
protected function _checkModified(ServerRequestInterface $request, Server $server): ResponseInterface|int|null
{
$modifiedTime = false;

try {
/** @var int|string|false $modifiedTime */
/** @var string|int|false $modifiedTime */
$modifiedTime = $server->getSource()
->lastModified($server->getSourcePath($this->_path));
} catch (Exception $exception) {
Expand Down Expand Up @@ -278,7 +285,7 @@ protected function _passThrough(ServerRequestInterface $request, Server $server)
* @param string|int $modifiedTime Last modified time of file.
* @return bool
*/
protected function _isNotModified(ServerRequestInterface $request, $modifiedTime)
protected function _isNotModified(ServerRequestInterface $request, string|int $modifiedTime): bool
{
$modifiedSince = $request->getHeaderLine('If-Modified-Since');
if (!$modifiedSince) {
Expand All @@ -293,11 +300,14 @@ protected function _isNotModified(ServerRequestInterface $request, $modifiedTime
*
* @param \Psr\Http\Message\ResponseInterface $response The response.
* @param string $cacheTime Cache time.
* @param int|string $modifiedTime Modified time.
* @param string|int $modifiedTime Modified time.
* @return \Psr\Http\Message\ResponseInterface
*/
protected function _withCacheHeaders(ResponseInterface $response, string $cacheTime, $modifiedTime)
{
protected function _withCacheHeaders(
ResponseInterface $response,
string $cacheTime,
string|int $modifiedTime
): ResponseInterface {
/** @var int $expire */
$expire = strtotime($cacheTime);
$maxAge = $expire - time();
Expand All @@ -315,7 +325,7 @@ protected function _withCacheHeaders(ResponseInterface $response, string $cacheT
* @param \Psr\Http\Message\ResponseInterface $response The response.
* @return \Psr\Http\Message\ResponseInterface
*/
protected function _withCustomHeaders(ResponseInterface $response)
protected function _withCustomHeaders(ResponseInterface $response): ResponseInterface
{
foreach ((array)$this->getConfig('headers') as $key => $value) {
$response = $response->withHeader($key, $value);
Expand All @@ -332,7 +342,7 @@ protected function _withCustomHeaders(ResponseInterface $response)
* @throws \ADmad\Glide\Exception\ResponseException
* @return \Psr\Http\Message\ResponseInterface|null
*/
protected function _handleException(ServerRequestInterface $request, $exception): ?ResponseInterface
protected function _handleException(ServerRequestInterface $request, Exception $exception): ?ResponseInterface
{
$event = $this->dispatchEvent(
static::RESPONSE_FAILURE_EVENT,
Expand Down
5 changes: 4 additions & 1 deletion src/Response/PsrResponseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,21 @@
use League\Flysystem\FilesystemException;
use League\Flysystem\FilesystemOperator;
use League\Glide\Responses\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;

class PsrResponseFactory implements ResponseFactoryInterface
{
// phpcs:disable
/**
* Create response.
*
* @param \League\Flysystem\FilesystemOperator $cache Cache file system.
* @param string $path Cached file path.
* @return \Psr\Http\Message\ResponseInterface Response object.
*/
public function create(FilesystemOperator $cache, $path)
public function create(FilesystemOperator $cache, $path): ResponseInterface
{
// phpcs:enable
try {
$resource = $cache->readStream($path);
} catch (FilesystemException $e) {
Expand Down
6 changes: 3 additions & 3 deletions src/View/Helper/GlideHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class GlideHelper extends Helper
*
* @var array
*/
protected $helpers = ['Html'];
protected array $helpers = ['Html'];

/**
* Default config for this helper.
Expand All @@ -33,7 +33,7 @@ class GlideHelper extends Helper
*
* @var array<string, mixed>
*/
protected $_defaultConfig = [
protected array $_defaultConfig = [
'baseUrl' => '/images/',
'secureUrls' => false,
'signKey' => null,
Expand All @@ -44,7 +44,7 @@ class GlideHelper extends Helper
*
* @var \League\Glide\Urls\UrlBuilder|null
*/
protected $_urlBuilder;
protected ?UrlBuilder $_urlBuilder = null;

/**
* Creates a formatted IMG element.
Expand Down

0 comments on commit 0ae85da

Please sign in to comment.