Skip to content

Commit 7664f92

Browse files
committed
initial release
0 parents  commit 7664f92

23 files changed

+3692
-0
lines changed

Diff for: .dockerignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
vendor
2+
.git
3+
.github
4+
.idea

Diff for: .github/workflows/phpunit.yml

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: Run PHPUnit Tests (PHP-DI, nyholm/psr7)
2+
3+
on:
4+
push:
5+
paths-ignore:
6+
- 'README.md'
7+
8+
jobs:
9+
phpunit:
10+
runs-on: ubuntu-latest
11+
12+
strategy:
13+
matrix:
14+
php-version: [ 8.2, 8.3, 8.4 ]
15+
16+
steps:
17+
- name: Checkout code
18+
uses: actions/checkout@v4
19+
20+
- name: Set up PHP
21+
uses: shivammathur/setup-php@v2
22+
with:
23+
php-version: ${{ matrix.php-version }}
24+
coverage: none
25+
26+
- name: Install dependencies
27+
run: composer install --no-progress --prefer-dist
28+
29+
- name: Run PHPUnit tests/*
30+
run: vendor/bin/phpunit tests/*

Diff for: .gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea
2+
vendor
3+
composer*.phar

Diff for: README.md

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# HTTP cache header middlewares for PSR-7, PSR-11
2+
3+
**This package is under development, and not ready for production use.**
4+
5+
This package provides a set (eg. Cache-Control, ETag) of HTTP cache handling PSR-7, PSR-11 compatible middlewares.
6+
7+
## Installation
8+
9+
```bash
10+
composer require smartondev/httpcache-middleware
11+
```
12+
13+
## Usage
14+
15+
### Cache-Control
16+
17+
For use `Cache-Control`, `ETag`, `Last-Modified` and more http "caching" headers, you need to add `HttpCacheMiddleware`
18+
to your route.
19+
20+
```php
21+
use SmartonDev\HttpCacheMiddleware\Http\Middleware\HttpCacheMiddleware;
22+
use SmartonDev\HttpCacheMiddleware\Providers\Constants\ProviderConstants;
23+
24+
// add HttpCacheContextService to container with ProviderConstants::HTTP_CACHE_CONTEXT_SERVICE_ID as singleton per request
25+
26+
Route::get('/api/user/{id}', function ($id) {
27+
$user = User::find($id);
28+
app()->get(ProviderConstants::HTTP_CACHE_CONTEXT_SERVICE_ID)->getCacheHeaderBuilder()->maxAge(days: 30)->public();
29+
return response()->json($user);
30+
})->middleware(HttpCacheMiddleware::class);
31+
```
32+
33+
### ETag
34+
35+
For use ETag, you need to add `ETagMatchMiddleware` to your route. You need to implement `ETagResolverInterface` and add
36+
it to the container with `ProviderConstants::ETAG_RESOLVER_SERVICE_ID`. For use ETag you need to
37+
add `HttpCacheContextService` and`HttpCacheMiddleware` to the container
38+
with `ProviderConstants::HTTP_CACHE_CONTEXT_SERVICE_ID`
39+
and `ProviderConstants::HTTP_CACHE_MIDDLEWARE_SERVICE_ID`.
40+
41+
```php
42+
use SmartonDev\HttpCacheMiddleware\Http\Middleware\ETagMatchMiddleware;
43+
use SmartonDev\HttpCacheMiddleware\Http\Middleware\HttpCacheMiddleware;
44+
use SmartonDev\HttpCacheMiddleware\Providers\Constants\ProviderConstants;
45+
use SmartonDev\HttpCacheMiddleware\Contracts\ETagResolverInterface;
46+
47+
class ETagResolver implements ETagResolverInterface
48+
{
49+
public function resolve($request): string
50+
{
51+
// use it own logic to generate ETag
52+
$id = $request->route('id');
53+
$etagFromCache = Cache::get('etag_' . $id);
54+
return $etagFromCache;
55+
}
56+
}
57+
// add ETagResolver to container with ProviderConstants::ETAG_RESOLVER_SERVICE_ID
58+
// add HttpCacheContextService to container with ProviderConstants::HTTP_CACHE_CONTEXT_SERVICE_ID as singleton pre request
59+
60+
Route::get('/api/user/{id}', function ($id) {
61+
$user = User::find($id);
62+
app()->get(ProviderConstants::HTTP_CACHE_CONTEXT_SERVICE_ID)->getCacheHeaderBuilder()->maxAge(days: 30)->public();
63+
// etag added automatically, or you can set it manually
64+
return response()->json($user);
65+
})->middleware(HttpCacheMiddleware::class)->middleware(ETagMatchMiddleware::class);
66+
```
67+
68+
### Modified since
69+
70+
```php
71+
72+
```
73+
74+
## Author
75+
76+
- [Márton Somogyi](https://github.com/kamarton)

Diff for: composer.json

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
"name": "smartondev/httpcache-middleware",
3+
"description": "PSR-7, PSR-11 compatible middlewares for HTTP cache handling",
4+
"keywords": [
5+
"middleware",
6+
"PSR-7",
7+
"PSR-11",
8+
"HTTP",
9+
"HTTP-cache",
10+
"Cache-control",
11+
"ETag",
12+
"Cache"
13+
],
14+
"type": "library",
15+
"license": "Apache-2.0",
16+
"authors": [
17+
{
18+
"name": "Márton Somogyi",
19+
"email": "[email protected]"
20+
}
21+
],
22+
"require": {
23+
"php": "^8.2",
24+
"psr/http-message": "^2.0",
25+
"psr/container": "^2.0",
26+
"smartondev/httpcache": "^0.2.0",
27+
"psr/http-server-middleware": "^1.0"
28+
},
29+
"require-dev": {
30+
"phpunit/phpunit": "^11.0",
31+
"php-di/php-di": "^7.0",
32+
"nyholm/psr7": "^1.0"
33+
},
34+
"autoload": {
35+
"psr-4": {
36+
"SmartonDev\\HttpCacheMiddleware\\": "src/"
37+
}
38+
},
39+
"autoload-dev": {
40+
"psr-4": {
41+
"SmartonDev\\HttpCacheMiddleware\\Tests\\": "tests/"
42+
}
43+
},
44+
"minimum-stability": "dev",
45+
"prefer-stable": true
46+
}

0 commit comments

Comments
 (0)