Skip to content

Commit 3102b1f

Browse files
committed
Refactor: change appendTime to cacheBusting configuration
Changes: - Renamed 'appendTime' boolean to 'cacheBusting' with value 'time' or false - Updated schema to use Expect::anyOf('time', false) - Made documentation more compact following existing style - All tests passing (12/12) This provides better extensibility for future cache-busting methods while maintaining the same functionality.
1 parent 8a5c51d commit 3102b1f

File tree

7 files changed

+29
-81
lines changed

7 files changed

+29
-81
lines changed

.docs/README.md

Lines changed: 9 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ version:
5555

5656
## CDN Extension
5757

58-
This extension adds support for CDN (Content Delivery Network) URLs in your templates. It provides both a macro `{cdn}` and a filter `|cdn` for easy asset URL management across different environments.
58+
This extension provides CDN support with `{cdn}` macro and `|cdn` filter for managing asset URLs.
5959

6060
### Install
6161

@@ -69,81 +69,29 @@ extensions:
6969
```neon
7070
cdn:
7171
url: https://cdn.example.com
72-
appendTime: false
72+
cacheBusting: time # or false
7373
```
7474

75-
**Options:**
76-
77-
- `url` (string, default: `''`) - Base CDN URL. Leave empty for localhost/development mode.
78-
- `appendTime` (bool, default: `false`) - Automatically append `?time=timestamp` for cache busting.
79-
8075
### Usage
8176

82-
#### Using the macro syntax
83-
8477
```latte
85-
<link rel="stylesheet" href="{cdn 'assets/dist/style.css'}">
86-
<script src="{cdn 'assets/dist/app.js'}"></script>
87-
<img src="{cdn 'images/logo.png'}" alt="Logo">
88-
```
78+
{* Macro syntax *}
79+
<link rel="stylesheet" href="{cdn 'assets/style.css'}">
8980
90-
#### Using the filter syntax
91-
92-
```latte
93-
<link rel="stylesheet" href="{='assets/dist/style.css'|cdn}">
81+
{* Filter syntax *}
9482
<script src="{='assets/dist/app.js'|cdn}"></script>
95-
<img src="{='images/logo.png'|cdn}" alt="Logo">
9683
```
9784

98-
#### Using the standalone filter
99-
100-
```php
101-
use Contributte\Latte\Filters\CdnFilter;
102-
103-
$url = CdnFilter::filter('assets/style.css', [
104-
'url' => 'https://cdn.example.com',
105-
'appendTime' => true,
106-
]);
107-
// Result: https://cdn.example.com/assets/style.css?time=1234567890
85+
**Development (empty url):**
10886
```
109-
110-
### Environment-specific behavior
111-
112-
**Development (localhost):**
113-
114-
```neon
115-
cdn:
116-
url: ""
117-
appendTime: true
87+
/assets/style.css?time=123456789
11888
```
11989

120-
```latte
121-
{cdn 'assets/style.css'}
90+
**Production (with CDN url):**
12291
```
123-
124-
Output: `/assets/style.css?time=1234567890`
125-
126-
**Production:**
127-
128-
```neon
129-
cdn:
130-
url: https://cdn.example.com
131-
appendTime: true
92+
https://cdn.example.com/assets/style.css?time=123456789
13293
```
13394

134-
```latte
135-
{cdn 'assets/style.css'}
136-
```
137-
138-
Output: `https://cdn.example.com/assets/style.css?time=1234567890`
139-
140-
### Benefits
141-
142-
- **Environment flexibility** - Switch between local and CDN URLs with configuration
143-
- **Cache busting** - Optional timestamp appending for automatic cache invalidation
144-
- **Multiple syntax options** - Use macro `{cdn}` or filter `|cdn` based on preference
145-
- **Clean templates** - No need to modify templates when switching between environments
146-
14795
## Filters Extension
14896

14997
Install filters by single extension and simple `FiltersProvider` implementation.

src/DI/CdnExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function getConfigSchema(): Schema
2222
{
2323
return Expect::structure([
2424
'url' => Expect::string()->default(''),
25-
'appendTime' => Expect::bool(false),
25+
'cacheBusting' => Expect::anyOf('time', false)->default(false),
2626
]);
2727
}
2828

src/Extensions/CdnExtension.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class CdnExtension extends Extension
99
{
1010

1111
/**
12-
* @param array{url?: string, appendTime?: bool} $config
12+
* @param array{url?: string, cacheBusting?: string|false} $config
1313
*/
1414
public function __construct(
1515
private array $config = [],
@@ -31,7 +31,7 @@ public function getProviders(): array
3131
$baseUrl = $this->config['url'] ?? '';
3232
$url = rtrim($baseUrl, '/') . '/' . ltrim($path, '/');
3333

34-
if ($this->config['appendTime'] ?? false) {
34+
if (($this->config['cacheBusting'] ?? false) === 'time') {
3535
$separator = str_contains($url, '?') ? '&' : '?';
3636
$url .= $separator . 'time=' . time();
3737
}
@@ -48,7 +48,7 @@ public function getFilters(): array
4848
$baseUrl = $this->config['url'] ?? '';
4949
$url = rtrim($baseUrl, '/') . '/' . ltrim($path, '/');
5050

51-
if ($this->config['appendTime'] ?? false) {
51+
if (($this->config['cacheBusting'] ?? false) === 'time') {
5252
$separator = str_contains($url, '?') ? '&' : '?';
5353
$url .= $separator . 'time=' . time();
5454
}

src/Filters/CdnFilter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ class CdnFilter
66
{
77

88
/**
9-
* @param array{url?: string, appendTime?: bool} $parameters
9+
* @param array{url?: string, cacheBusting?: string|false} $parameters
1010
*/
1111
public static function filter(string $path, array $parameters = []): string
1212
{
1313
$baseUrl = $parameters['url'] ?? '';
1414
$url = rtrim($baseUrl, '/') . '/' . ltrim($path, '/');
1515

16-
if ($parameters['appendTime'] ?? false) {
16+
if (($parameters['cacheBusting'] ?? false) === 'time') {
1717
$separator = str_contains($url, '?') ? '&' : '?';
1818
$url .= $separator . 'time=' . time();
1919
}

tests/Cases/DI/CdnExtension.phpt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Toolkit::test(function (): void {
3535
Assert::equal('https://cdn.example.com/assets/style.css', $latteFactory->create()->renderToString(FileMock::create('{="assets/style.css"|cdn}', 'latte')));
3636
});
3737

38-
// Test DI integration with CDN URL and appendTime
38+
// Test DI integration with CDN URL and cacheBusting
3939
Toolkit::test(function (): void {
4040
$loader = new ContainerLoader(Environment::getTestDir(), true);
4141
$class = $loader->load(function (Compiler $compiler): void {
@@ -44,7 +44,7 @@ Toolkit::test(function (): void {
4444
$compiler->loadConfig(FileMock::create('
4545
cdn:
4646
url: https://cdn.example.com
47-
appendTime: true
47+
cacheBusting: time
4848
', 'neon'));
4949
}, 2);
5050

@@ -70,7 +70,7 @@ Toolkit::test(function (): void {
7070
$compiler->loadConfig(FileMock::create('
7171
cdn:
7272
url: ""
73-
appendTime: true
73+
cacheBusting: time
7474
', 'neon'));
7575
}, 3);
7676

tests/Cases/Extensions/CdnExtension.phpt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ Toolkit::test(function (): void {
2323
Assert::equal('https://cdn.example.com/js/app.js', $latte->renderToString('{cdn "js/app.js"}'));
2424
});
2525

26-
// Test CDN macro with base URL and appendTime
26+
// Test CDN macro with base URL and cacheBusting
2727
Toolkit::test(function (): void {
2828
$latte = new Engine();
2929
$latte->setTempDirectory(Environment::getTestDir());
3030
$latte->setLoader(new StringLoader());
3131
$latte->addExtension(new CdnExtension([
3232
'url' => 'https://cdn.example.com',
33-
'appendTime' => true,
33+
'cacheBusting' => 'time',
3434
]));
3535

3636
$result = $latte->renderToString('{cdn "assets/style.css"}');
@@ -49,14 +49,14 @@ Toolkit::test(function (): void {
4949
Assert::equal('/assets/style.css', $latte->renderToString('{cdn "assets/style.css"}'));
5050
});
5151

52-
// Test CDN macro without base URL with appendTime
52+
// Test CDN macro without base URL with cacheBusting
5353
Toolkit::test(function (): void {
5454
$latte = new Engine();
5555
$latte->setTempDirectory(Environment::getTestDir());
5656
$latte->setLoader(new StringLoader());
5757
$latte->addExtension(new CdnExtension([
5858
'url' => '',
59-
'appendTime' => true,
59+
'cacheBusting' => 'time',
6060
]));
6161

6262
$result = $latte->renderToString('{cdn "assets/style.css"}');
@@ -87,14 +87,14 @@ Toolkit::test(function (): void {
8787
Assert::equal('https://cdn.example.com/js/app.js', $latte->renderToString('{="js/app.js"|cdn}'));
8888
});
8989

90-
// Test CDN filter with appendTime
90+
// Test CDN filter with cacheBusting
9191
Toolkit::test(function (): void {
9292
$latte = new Engine();
9393
$latte->setTempDirectory(Environment::getTestDir());
9494
$latte->setLoader(new StringLoader());
9595
$latte->addExtension(new CdnExtension([
9696
'url' => 'https://cdn.example.com',
97-
'appendTime' => true,
97+
'cacheBusting' => 'time',
9898
]));
9999

100100
$result = $latte->renderToString('{="assets/style.css"|cdn}');

tests/Cases/Filters/CdnFilter.phpt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ Toolkit::test(function (): void {
1919
Assert::equal('https://cdn.example.com/js/app.js', $url);
2020
});
2121

22-
// Test CDN filter with base URL and appendTime
22+
// Test CDN filter with base URL and cacheBusting
2323
Toolkit::test(function (): void {
2424
$url = CdnFilter::filter('assets/style.css', [
2525
'url' => 'https://cdn.example.com',
26-
'appendTime' => true,
26+
'cacheBusting' => 'time',
2727
]);
2828
Assert::true(str_starts_with($url, 'https://cdn.example.com/assets/style.css?time='));
2929
Assert::match('~^https://cdn\.example\.com/assets/style\.css\?time=\d+$~', $url);
@@ -40,11 +40,11 @@ Toolkit::test(function (): void {
4040
Assert::equal('/assets/style.css', $url);
4141
});
4242

43-
// Test CDN filter without base URL with appendTime
43+
// Test CDN filter without base URL with cacheBusting
4444
Toolkit::test(function (): void {
4545
$url = CdnFilter::filter('assets/style.css', [
4646
'url' => '',
47-
'appendTime' => true,
47+
'cacheBusting' => 'time',
4848
]);
4949
Assert::true(str_starts_with($url, '/assets/style.css?time='));
5050
Assert::match('~^/assets/style\.css\?time=\d+$~', $url);
@@ -72,7 +72,7 @@ Toolkit::test(function (): void {
7272
Toolkit::test(function (): void {
7373
$url = CdnFilter::filter('assets/style.css?v=1.0', [
7474
'url' => 'https://cdn.example.com',
75-
'appendTime' => true,
75+
'cacheBusting' => 'time',
7676
]);
7777
Assert::match('~^https://cdn\.example\.com/assets/style\.css\?v=1\.0&time=\d+$~', $url);
7878
});

0 commit comments

Comments
 (0)