Skip to content

Commit 51b1cb6

Browse files
committed
initial commit
0 parents  commit 51b1cb6

17 files changed

Lines changed: 1168 additions & 0 deletions

.github/workflows/tests.yml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: Run Tests
2+
3+
on:
4+
push:
5+
branches: [ main ] # Assuming 'main' is your default branch
6+
pull_request:
7+
branches: [ main ] # Assuming 'main' is your default branch
8+
9+
jobs:
10+
tests:
11+
runs-on: ubuntu-latest
12+
strategy:
13+
fail-fast: false # Allow other jobs to continue even if one fails
14+
matrix:
15+
php-versions: ['8.1', '8.2', '8.3'] # PHP versions to test
16+
17+
steps:
18+
- name: Checkout code
19+
uses: actions/checkout@v4
20+
21+
- name: Setup PHP
22+
uses: shivammathur/setup-php@v2
23+
with:
24+
php-version: ${{ matrix.php-versions }}
25+
extensions: mbstring, xml, ctype, json, fileinfo # Common extensions
26+
coverage: none # No code coverage needed for basic tests
27+
tools: composer:v2 # Specify Composer version if needed
28+
29+
- name: Validate composer.json and composer.lock
30+
run: composer validate --strict
31+
32+
- name: Cache Composer dependencies
33+
uses: actions/cache@v4
34+
with:
35+
path: vendor # The path to cache
36+
key: ${{ runner.os }}-php-${{ matrix.php-versions }}-composer-${{ hashFiles('**/composer.lock') }}
37+
restore-keys: |
38+
${{ runner.os }}-php-${{ matrix.php-versions }}-composer-
39+
40+
- name: Install Dependencies
41+
run: composer install --prefer-dist --no-progress --no-suggest
42+
43+
- name: Run Tests
44+
run: composer test

.gitignore

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/vendor/
2+
/node_modules/
3+
.env
4+
.env.backup
5+
.phpunit.cache
6+
composer.lock
7+
npm-debug.log
8+
yarn-error.log
9+
/.idea
10+
/.vscode
11+
/.vagrant
12+
.DS_Store

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# The MIT License (MIT)
2+
3+
Copyright (c) Statamic
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# Statamic Cloudflare Cache
2+
3+
Automatically purge your Cloudflare cache when content changes in Statamic.
4+
5+
## Features
6+
7+
- Automatically purges Cloudflare cache when Statamic content changes.
8+
- Configurable events that trigger cache purging.
9+
- Optional queuing of purge jobs for background processing.
10+
- CLI command for manual cache purging.
11+
- Simple configuration.
12+
13+
## Installation
14+
15+
You can install the package via composer:
16+
17+
```bash
18+
composer require eminos/statamic-cloudflare-cache
19+
```
20+
21+
Publish the config file:
22+
23+
```bash
24+
php artisan vendor:publish --tag="cloudflare-cache-config"
25+
```
26+
27+
## Configuration
28+
29+
Add the following environment variables to your `.env` file:
30+
31+
```
32+
CLOUDFLARE_API_TOKEN=your-api-token
33+
CLOUDFLARE_ZONE_ID=your-zone-id
34+
CLOUDFLARE_CACHE_ENABLED=true
35+
CLOUDFLARE_CACHE_QUEUE_PURGE=false # Optional: Set to true to dispatch purges to the queue
36+
CLOUDFLARE_CACHE_DEBUG=false # Optional: Set to true to log API calls
37+
```
38+
39+
You can get your API token and Zone ID from the Cloudflare dashboard:
40+
41+
1. **API Token**: Log in to your Cloudflare dashboard, go to "My Profile" > "API Tokens" and create a token with the "Zone.Cache Purge" permission.
42+
43+
2. **Zone ID**: Go to your domain's overview page in Cloudflare. The Zone ID is displayed in the right sidebar under "API" section.
44+
45+
## Usage
46+
47+
### Automatic Cache Purging
48+
49+
Once configured, the addon will automatically purge the Cloudflare cache when content changes in Statamic. By default, it listens for the following events:
50+
51+
- Entry saved/deleted
52+
- Term saved/deleted
53+
- Asset saved/deleted
54+
55+
You can configure which events trigger cache purging in the config file.
56+
57+
#### Queued Purging
58+
59+
If you prefer to handle cache purging in the background to avoid potential delays during web requests, you can enable queued purging. Set the `CLOUDFLARE_CACHE_QUEUE_PURGE` environment variable to `true` or set `'queue_purge' => true` in the configuration file.
60+
61+
When enabled, purge operations triggered by events will be dispatched as jobs to your application's queue. **Note:** This requires you to have a queue worker running (`php artisan queue:work`).
62+
63+
### Manual Cache Purging
64+
65+
You can manually purge the cache using the following command:
66+
67+
```bash
68+
# Purge all cache
69+
php please cloudflare:purge
70+
71+
# Purge specific URL
72+
php please cloudflare:purge --url=https://example.com/specific-page
73+
```
74+
75+
## Advanced Configuration
76+
77+
The published configuration file (`config/cloudflare-cache.php`) allows you to customize the behavior of the addon:
78+
79+
```php
80+
return [
81+
// Enable or disable the addon
82+
'enabled' => env('CLOUDFLARE_CACHE_ENABLED', true),
83+
84+
// Cloudflare API credentials
85+
'api_token' => env('CLOUDFLARE_API_TOKEN', ''),
86+
'zone_id' => env('CLOUDFLARE_ZONE_ID', ''),
87+
88+
// Configure which events trigger cache purging
89+
'purge_on' => [
90+
'entry_saved' => true,
91+
'entry_deleted' => true,
92+
'term_saved' => true,
93+
'term_deleted' => true,
94+
'asset_saved' => true,
95+
'asset_deleted' => true,
96+
],
97+
98+
// Dispatch purge jobs to the queue instead of running synchronously
99+
'queue_purge' => env('CLOUDFLARE_CACHE_QUEUE_PURGE', false),
100+
101+
// Advanced settings
102+
'purge_urls' => true, // Purge specific URLs if possible
103+
'purge_everything_fallback' => true, // Fallback to purging everything if specific URLs can't be determined
104+
'debug' => env('CLOUDFLARE_CACHE_DEBUG', false), // Log API call attempts when purging
105+
];
106+
```
107+
108+
## Testing
109+
110+
```bash
111+
composer test
112+
```
113+
114+
## License
115+
116+
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

composer.json

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
"name": "eminos/statamic-cloudflare-cache",
3+
"description": "Cloudflare cache integration for Statamic",
4+
"type": "statamic-addon",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Emin Jasarevic",
9+
"email": "emin@kiwikiwi.se"
10+
}
11+
],
12+
"require": {
13+
"php": "^8.0",
14+
"statamic/cms": "^5.0"
15+
},
16+
"require-dev": {
17+
"pestphp/pest": "^3.0",
18+
"orchestra/testbench": "^10.0"
19+
},
20+
"autoload": {
21+
"psr-4": {
22+
"Eminos\\StatamicCloudflareCache\\": "src/"
23+
}
24+
},
25+
"autoload-dev": {
26+
"psr-4": {
27+
"Eminos\\StatamicCloudflareCache\\Tests\\": "tests/"
28+
}
29+
},
30+
"extra": {
31+
"statamic": {
32+
"name": "Cloudflare Cache",
33+
"description": "Automatically purge Cloudflare cache when content changes"
34+
},
35+
"laravel": {
36+
"providers": [
37+
"Eminos\\StatamicCloudflareCache\\CloudflareCacheServiceProvider"
38+
]
39+
}
40+
},
41+
"config": {
42+
"allow-plugins": {
43+
"pestphp/pest-plugin": true,
44+
"pixelfear/composer-dist-plugin": true
45+
}
46+
},
47+
"scripts": {
48+
"test": "vendor/bin/pest"
49+
}
50+
}

config/cloudflare-cache.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
return [
4+
/*
5+
|--------------------------------------------------------------------------
6+
| Cloudflare API Settings
7+
|--------------------------------------------------------------------------
8+
*/
9+
'enabled' => env('CLOUDFLARE_CACHE_ENABLED', true),
10+
11+
'api_token' => env('CLOUDFLARE_API_TOKEN', ''),
12+
13+
'zone_id' => env('CLOUDFLARE_ZONE_ID', ''),
14+
15+
/*
16+
|--------------------------------------------------------------------------
17+
| Cache Purging Settings
18+
|--------------------------------------------------------------------------
19+
*/
20+
'purge_on' => [
21+
'entry_saved' => true,
22+
'entry_deleted' => true,
23+
'term_saved' => true,
24+
'term_deleted' => true,
25+
'asset_saved' => true,
26+
'asset_deleted' => true,
27+
],
28+
29+
'queue_purge' => env('CLOUDFLARE_CACHE_QUEUE_PURGE', false), // Dispatch purge jobs to the queue
30+
31+
/*
32+
|--------------------------------------------------------------------------
33+
| Advanced Settings
34+
|--------------------------------------------------------------------------
35+
*/
36+
'purge_urls' => true, // Purge specific URLs if possible
37+
'purge_everything_fallback' => true, // Fallback to purging everything if specific URLs can't be determined
38+
39+
'debug' => env('CLOUDFLARE_CACHE_DEBUG', false), // Log API call attempts when purging
40+
];

phpunit.xml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
4+
bootstrap="vendor/autoload.php"
5+
colors="true"
6+
cacheDirectory=".phpunit.cache"
7+
>
8+
<testsuites>
9+
<testsuite name="Unit">
10+
<directory suffix="Test.php">./tests/Unit</directory>
11+
</testsuite>
12+
<testsuite name="Feature">
13+
<directory suffix="Test.php">./tests/Feature</directory>
14+
</testsuite>
15+
</testsuites>
16+
<source>
17+
<include>
18+
<directory suffix=".php">./src</directory>
19+
</include>
20+
</source>
21+
<php>
22+
<env name="APP_ENV" value="testing"/>
23+
<env name="CACHE_DRIVER" value="array"/>
24+
<env name="SESSION_DRIVER" value="array"/>
25+
<env name="QUEUE_DRIVER" value="sync"/>
26+
<env name="CLOUDFLARE_API_TOKEN" value="test-token"/>
27+
<env name="CLOUDFLARE_ZONE_ID" value="test-zone-id"/>
28+
<env name="CLOUDFLARE_CACHE_ENABLED" value="true"/>
29+
</php>
30+
</phpunit>
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
namespace Eminos\StatamicCloudflareCache;
4+
5+
use Statamic\Providers\AddonServiceProvider;
6+
use Eminos\StatamicCloudflareCache\Commands\PurgeCache;
7+
use Eminos\StatamicCloudflareCache\Listeners\PurgeCloudflareCache;
8+
use Statamic\Events\EntrySaved;
9+
use Statamic\Events\EntryDeleted;
10+
use Statamic\Events\TermSaved;
11+
use Statamic\Events\TermDeleted;
12+
use Statamic\Events\AssetSaved;
13+
use Statamic\Events\AssetDeleted;
14+
15+
class CloudflareCacheServiceProvider extends AddonServiceProvider
16+
{
17+
protected $commands = [
18+
PurgeCache::class,
19+
];
20+
21+
protected $listen = [
22+
EntrySaved::class => [
23+
PurgeCloudflareCache::class,
24+
],
25+
EntryDeleted::class => [
26+
PurgeCloudflareCache::class,
27+
],
28+
TermSaved::class => [
29+
PurgeCloudflareCache::class,
30+
],
31+
TermDeleted::class => [
32+
PurgeCloudflareCache::class,
33+
],
34+
AssetSaved::class => [
35+
PurgeCloudflareCache::class,
36+
],
37+
AssetDeleted::class => [
38+
PurgeCloudflareCache::class,
39+
],
40+
];
41+
42+
/**
43+
* Register the application services.
44+
*
45+
* @return void
46+
*/
47+
public function register(): void
48+
{
49+
$this->mergeConfigFrom(
50+
__DIR__.'/../config/cloudflare-cache.php', 'cloudflare-cache'
51+
);
52+
}
53+
54+
/**
55+
* Bootstrap the application services.
56+
*
57+
* @return void
58+
*/
59+
public function boot(): void
60+
{
61+
parent::boot();
62+
63+
if ($this->app->runningInConsole()) {
64+
$this->commands($this->commands);
65+
}
66+
67+
$this->publishes([
68+
__DIR__.'/../config/cloudflare-cache.php' => config_path('cloudflare-cache.php'),
69+
], 'cloudflare-cache-config');
70+
}
71+
}

0 commit comments

Comments
 (0)