Skip to content

Commit ab0b9fc

Browse files
authored
Merge pull request #32 from adriansuter/patch-slim4
Add Slim 4 support
2 parents 9b9e6c6 + e7228fb commit ab0b9fc

13 files changed

+425
-191
lines changed

.gitattributes

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Enforce Unix newlines
2+
* text=lf
3+
4+
# Exclude unused files
5+
# see: https://redd.it/2jzp6k
6+
/tests export-ignore
7+
/.gitattributes export-ignore
8+
/.gitignore export-ignore
9+
/.travis.yml export-ignore
10+
/CONTRIBUTING.md export-ignore
11+
/phpcs.xml.dist export-ignore
12+
/phpstan.neon.dist export-ignore
13+
/phpunit.xml.dist export-ignore
14+
/README.md export-ignore

.gitignore

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1-
vendor
1+
/.idea
2+
/coverage
3+
/vendor
4+
.phpunit.result.cache
25
composer.lock

.travis.yml

+22-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,26 @@
11
language: php
22

3-
php:
4-
- 5.5
5-
- 5.6
6-
- 7.0
7-
- 7.1
3+
dist: trusty
84

9-
before_script: composer install
5+
matrix:
6+
include:
7+
- php: 7.2
8+
- php: 7.3
9+
- php: 7.4
10+
env: ANALYSIS='true'
11+
- php: nightly
12+
allow_failures:
13+
- php: nightly
1014

11-
script: phpunit --coverage-text
15+
before_script:
16+
- if [[ "$ANALYSIS" == 'true' ]]; then composer require php-coveralls/php-coveralls:^2.1.0 ; fi
17+
- composer install -n
18+
19+
script:
20+
- if [[ "$ANALYSIS" != 'true' ]]; then vendor/bin/phpunit ; fi
21+
- if [[ "$ANALYSIS" == 'true' ]]; then vendor/bin/phpunit --coverage-clover clover.xml ; fi
22+
- if [[ "$ANALYSIS" == 'true' ]]; then vendor/bin/phpcs ; fi
23+
- if [[ "$ANALYSIS" == 'true' ]]; then vendor/bin/phpstan analyse src ; fi
24+
25+
after_success:
26+
- if [[ "$ANALYSIS" == 'true' ]]; then vendor/bin/php-coveralls --coverage_clover=clover.xml -v ; fi

README.md

+24-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Slim Framework HTTP Cache
22

33
[![Build Status](https://travis-ci.org/slimphp/Slim-HttpCache.svg?branch=master)](https://travis-ci.org/slimphp/Slim-HttpCache)
4+
[![Latest Stable Version](https://poser.pugx.org/slim/http-cache/v)](//packagist.org/packages/slim/http-cache)
5+
[![License](https://poser.pugx.org/slim/http-cache/license)](https://packagist.org/packages/slim/http-cache)
46

57
This repository contains a Slim Framework HTTP cache middleware and service provider.
68

@@ -12,30 +14,38 @@ Via Composer
1214
$ composer require slim/http-cache
1315
```
1416

15-
Requires Slim 3.0.0 or newer.
17+
Requires Slim 4.0.0 or newer.
1618

1719
## Usage
1820

1921
```php
20-
$app = new \Slim\App();
22+
declare(strict_types=1);
2123

22-
// Register middleware
24+
use Psr\Http\Message\ResponseInterface as Response;
25+
use Psr\Http\Message\ServerRequestInterface as Request;
26+
27+
require __DIR__.'/../vendor/autoload.php';
28+
29+
$app = \Slim\Factory\AppFactory::create();
30+
31+
// Register the http cache middleware.
2332
$app->add(new \Slim\HttpCache\Cache('public', 86400));
2433

25-
// Fetch DI Container
26-
$container = $app->getContainer();
34+
// Create the cache provider.
35+
$cacheProvider = new \Slim\HttpCache\CacheProvider();
2736

28-
// Register service provider
29-
$container['cache'] = function () {
30-
return new \Slim\HttpCache\CacheProvider();
31-
};
37+
// Register a route and let the closure callback inherit the cache provider.
38+
$app->get(
39+
'/',
40+
function (Request $request, Response $response, array $args) use ($cacheProvider): Response {
41+
// Use the cache provider.
42+
$response = $cacheProvider->withEtag($response, 'abc');
3243

33-
// Example route with ETag header
34-
$app->get('/foo', function ($req, $res, $args) {
35-
$resWithEtag = $this->cache->withEtag($res, 'abc');
44+
$response->getBody()->write('Hello world!');
3645

37-
return $resWithEtag;
38-
});
46+
return $response;
47+
}
48+
);
3949

4050
$app->run();
4151
```

composer.json

+48-30
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,51 @@
11
{
2-
"name": "slim/http-cache",
3-
"type": "library",
4-
"description": "Slim Framework HTTP cache middleware and service provider",
5-
"keywords": ["slim","framework","middleware","cache"],
6-
"homepage": "http://slimframework.com",
7-
"license": "MIT",
8-
"authors": [
9-
{
10-
"name": "Josh Lockhart",
11-
"email": "[email protected]",
12-
"homepage": "http://joshlockhart.com"
13-
}
14-
],
15-
"require": {
16-
"php": ">=5.5.0",
17-
"psr/http-message": "^1.0"
18-
},
19-
"require-dev": {
20-
"slim/slim": "^3.0",
21-
"phpunit/phpunit": "^4.0"
22-
},
23-
"autoload": {
24-
"psr-4": {
25-
"Slim\\HttpCache\\": "src"
26-
}
27-
},
28-
"autoload-dev": {
29-
"psr-4": {
30-
"Slim\\HttpCache\\Tests\\": "tests"
31-
}
2+
"name": "slim/http-cache",
3+
"type": "library",
4+
"description": "Slim Framework HTTP cache middleware and service provider",
5+
"keywords": [
6+
"slim",
7+
"framework",
8+
"middleware",
9+
"cache"
10+
],
11+
"homepage": "https://www.slimframework.com",
12+
"license": "MIT",
13+
"authors": [
14+
{
15+
"name": "Josh Lockhart",
16+
"email": "[email protected]",
17+
"homepage": "http://joshlockhart.com"
18+
}
19+
],
20+
"require": {
21+
"php": "^7.2",
22+
"psr/http-message": "^1.0",
23+
"psr/http-server-middleware": "^1.0"
24+
},
25+
"require-dev": {
26+
"slim/psr7": "^1.1",
27+
"phpunit/phpunit": "^8.5",
28+
"squizlabs/php_codesniffer": "^3.5",
29+
"phpstan/phpstan": "^0.12.28"
30+
},
31+
"autoload": {
32+
"psr-4": {
33+
"Slim\\HttpCache\\": "src"
3234
}
35+
},
36+
"autoload-dev": {
37+
"psr-4": {
38+
"Slim\\HttpCache\\Tests\\": "tests"
39+
}
40+
},
41+
"scripts": {
42+
"test": [
43+
"@phpcs",
44+
"@phpstan",
45+
"@phpunit"
46+
],
47+
"phpcs": "phpcs",
48+
"phpstan": "phpstan analyse src --memory-limit=-1",
49+
"phpunit": "phpunit"
50+
}
3351
}

phpcs.xml.dist

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0"?>
2+
<ruleset name="Slim coding standard">
3+
<description>Slim coding standard</description>
4+
5+
<!-- display progress -->
6+
<arg value="p"/>
7+
<!-- use colors in output -->
8+
<arg name="colors"/>
9+
10+
<!-- inherit rules from: -->
11+
<rule ref="PSR2"/>
12+
<rule ref="Generic.Arrays.DisallowLongArraySyntax"/>
13+
14+
<!-- Paths to check -->
15+
<file>src</file>
16+
<file>tests</file>
17+
</ruleset>

phpstan.neon.dist

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
parameters:
2+
level: max
3+
inferPrivatePropertyTypeFromConstructor: true

phpunit.xml.dist

+18-8
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,35 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
3-
<phpunit backupGlobals="false"
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/8.5/phpunit.xsd"
4+
backupGlobals="false"
45
backupStaticAttributes="false"
6+
beStrictAboutTestsThatDoNotTestAnything="true"
7+
beStrictAboutChangesToGlobalState="true"
8+
beStrictAboutOutputDuringTests="true"
59
colors="true"
610
convertErrorsToExceptions="true"
711
convertNoticesToExceptions="true"
812
convertWarningsToExceptions="true"
913
processIsolation="false"
1014
stopOnFailure="false"
11-
syntaxCheck="false"
1215
bootstrap="tests/bootstrap.php"
1316
>
1417
<testsuites>
15-
<testsuite name="Slim Test Suite">
16-
<directory>tests/</directory>
18+
<testsuite name="Slim HttpCache Test Suite">
19+
<directory>./tests/</directory>
1720
</testsuite>
1821
</testsuites>
19-
2022
<filter>
21-
<whitelist>
22-
<directory>src/</directory>
23+
<whitelist processUncoveredFilesFromWhitelist="true">
24+
<directory>./src/</directory>
2325
</whitelist>
2426
</filter>
27+
<logging>
28+
<log
29+
type="coverage-html"
30+
target="./coverage"
31+
lowUpperBound="20"
32+
highLowerBound="50"
33+
/>
34+
</logging>
2535
</phpunit>

src/Cache.php

+45-26
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,28 @@
11
<?php
2+
/**
3+
* Slim Framework (https://www.slimframework.com)
4+
*
5+
* @license https://github.com/slimphp/Slim-HttpCache/blob/master/LICENSE.md (MIT License)
6+
*/
7+
8+
declare(strict_types=1);
9+
210
namespace Slim\HttpCache;
311

4-
use Psr\Http\Message\RequestInterface;
512
use Psr\Http\Message\ResponseInterface;
6-
7-
class Cache
13+
use Psr\Http\Message\ServerRequestInterface;
14+
use Psr\Http\Server\MiddlewareInterface;
15+
use Psr\Http\Server\RequestHandlerInterface;
16+
17+
use function in_array;
18+
use function is_array;
19+
use function is_numeric;
20+
use function preg_split;
21+
use function reset;
22+
use function sprintf;
23+
use function strtotime;
24+
25+
class Cache implements MiddlewareInterface
826
{
927
/**
1028
* Cache-Control type (public or private)
@@ -34,44 +52,45 @@ class Cache
3452
* @param int $maxAge The maximum age of client-side cache
3553
* @param bool $mustRevalidate must-revalidate
3654
*/
37-
public function __construct($type = 'private', $maxAge = 86400, $mustRevalidate = false)
55+
public function __construct(string $type = 'private', int $maxAge = 86400, bool $mustRevalidate = false)
3856
{
3957
$this->type = $type;
4058
$this->maxAge = $maxAge;
4159
$this->mustRevalidate = $mustRevalidate;
4260
}
4361

4462
/**
45-
* Invoke cache middleware
46-
*
47-
* @param RequestInterface $request A PSR7 request object
48-
* @param ResponseInterface $response A PSR7 response object
49-
* @param callable $next The next middleware callable
50-
*
51-
* @return ResponseInterface A PSR7 response object
63+
* {@inheritDoc}
5264
*/
53-
public function __invoke(RequestInterface $request, ResponseInterface $response, callable $next)
65+
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
5466
{
55-
$response = $next($request, $response);
67+
$response = $handler->handle($request);
5668

5769
// Cache-Control header
5870
if (!$response->hasHeader('Cache-Control')) {
5971
if ($this->maxAge === 0) {
60-
$response = $response->withHeader('Cache-Control', sprintf(
61-
'%s, no-cache%s',
62-
$this->type,
63-
$this->mustRevalidate ? ', must-revalidate' : ''
64-
));
72+
$response = $response->withHeader(
73+
'Cache-Control',
74+
sprintf(
75+
'%s, no-cache%s',
76+
$this->type,
77+
$this->mustRevalidate ? ', must-revalidate' : ''
78+
)
79+
);
6580
} else {
66-
$response = $response->withHeader('Cache-Control', sprintf(
67-
'%s, max-age=%s%s',
68-
$this->type,
69-
$this->maxAge,
70-
$this->mustRevalidate ? ', must-revalidate' : ''
71-
));
81+
$response = $response->withHeader(
82+
'Cache-Control',
83+
sprintf(
84+
'%s, max-age=%s%s',
85+
$this->type,
86+
$this->maxAge,
87+
$this->mustRevalidate ? ', must-revalidate' : ''
88+
)
89+
);
7290
}
7391
}
7492

93+
7594
// ETag header and conditional GET check
7695
$etag = $response->getHeader('ETag');
7796
$etag = reset($etag);
@@ -81,7 +100,7 @@ public function __invoke(RequestInterface $request, ResponseInterface $response,
81100

82101
if ($ifNoneMatch) {
83102
$etagList = preg_split('@\s*,\s*@', $ifNoneMatch);
84-
if (in_array($etag, $etagList) || in_array('*', $etagList)) {
103+
if (is_array($etagList) && (in_array($etag, $etagList) || in_array('*', $etagList))) {
85104
return $response->withStatus(304);
86105
}
87106
}
@@ -92,7 +111,7 @@ public function __invoke(RequestInterface $request, ResponseInterface $response,
92111
$lastModified = $response->getHeaderLine('Last-Modified');
93112

94113
if ($lastModified) {
95-
if (!is_integer($lastModified)) {
114+
if (!is_numeric($lastModified)) {
96115
$lastModified = strtotime($lastModified);
97116
}
98117

0 commit comments

Comments
 (0)