Skip to content

Commit 1795514

Browse files
authored
Fix #19741: Added option to use a closure for $variations definition in yii\filters\PageCache
1 parent ff2d004 commit 1795514

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

framework/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Yii Framework 2 Change Log
44
2.0.48 under development
55
------------------------
66

7+
- Enh #19741: Added option to use a closure for `$variations` definition in `yii\filters\PageCache` (nadar)
78
- Bug #15376: Added $userId for RBAC roles cache (manchenkoff)
89
- Bug #18867: Fixed multiple issues with `yii\grid\CheckboxColumn`: "check all" checkbox not being checked on page load when all data row checkboxes are initially checked; clicking checkboxes triggered "change" event for other checkboxes that do not change their state; "check all" checkbox not being checked when disabled checkboxes are present and clicking last non-checked data row checkbox (PowerGamer1)
910
- Bug #19635: PHP 8.2 compatibility fix for `yii\validators\DateValidator` (PowerGamer1)

framework/filters/PageCache.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace yii\filters;
99

10+
use Closure;
1011
use Yii;
1112
use yii\base\Action;
1213
use yii\base\ActionFilter;
@@ -98,7 +99,7 @@ class PageCache extends ActionFilter implements DynamicContentAwareInterface
9899
*/
99100
public $dependency;
100101
/**
101-
* @var string[]|string list of factors that would cause the variation of the content being cached.
102+
* @var string[]|string|callable list of factors that would cause the variation of the content being cached.
102103
* Each factor is a string representing a variation (e.g. the language, a GET parameter).
103104
* The following variation setting will cause the content to be cached in different versions
104105
* according to the current application language:
@@ -108,6 +109,20 @@ class PageCache extends ActionFilter implements DynamicContentAwareInterface
108109
* Yii::$app->language,
109110
* ]
110111
* ```
112+
*
113+
* Since version 2.0.48 you can provide an anonymous function to generate variations. This is especially helpful
114+
* when you need to access the User component, which is resolved before the PageCache behavior:
115+
*
116+
* ```php
117+
* 'variations' => function() {
118+
* return [
119+
* Yii::$app->language,
120+
* Yii::$app->user->id
121+
* ];
122+
* }
123+
* ```
124+
*
125+
* The callable should return an array.
111126
*/
112127
public $variations;
113128
/**
@@ -318,7 +333,13 @@ protected function calculateCacheKey()
318333
if ($this->varyByRoute) {
319334
$key[] = Yii::$app->requestedRoute;
320335
}
321-
return array_merge($key, (array)$this->variations);
336+
337+
if ($this->variations instanceof Closure || (is_array($this->variations) && is_callable($this->variations))) {
338+
$variations = call_user_func($this->variations, $this);
339+
} else {
340+
$variations = $this->variations;
341+
}
342+
return array_merge($key, (array) $variations);
322343
}
323344

324345
/**

tests/framework/filters/PageCacheTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,4 +472,24 @@ public function testCalculateCacheKey()
472472
$keys = $this->invokeMethod(new PageCache(), 'calculateCacheKey');
473473
$this->assertEquals(['yii\filters\PageCache', 'test'], $keys);
474474
}
475+
476+
public function testClosureVariations()
477+
{
478+
$keys = $this->invokeMethod(new PageCache([
479+
'variations' => function() {
480+
return [
481+
'foobar'
482+
];
483+
}
484+
]), 'calculateCacheKey');
485+
$this->assertEquals(['yii\filters\PageCache', 'test', 'foobar'], $keys);
486+
487+
// test type cast of string
488+
$keys = $this->invokeMethod(new PageCache([
489+
'variations' => function() {
490+
return 'foobarstring';
491+
}
492+
]), 'calculateCacheKey');
493+
$this->assertEquals(['yii\filters\PageCache', 'test', 'foobarstring'], $keys);
494+
}
475495
}

0 commit comments

Comments
 (0)