1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace luya\web\filters; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use luya\traits\CacheableTrait; |
7
|
|
|
use yii\filters\PageCache; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Filter to enable Response Cache. |
11
|
|
|
* |
12
|
|
|
* Extends the {{yii\filters\PageCache}} class by using the {{luya\traits\CacheableTrait}} whether caching |
13
|
|
|
* is enabled or not. Thefore you can attach this page cache to any controller whether caching is enabled |
14
|
|
|
* or not, it will not throw an exception |
15
|
|
|
* |
16
|
|
|
* @author Basil Suter <[email protected]> |
17
|
|
|
* @since 1.0.0 |
18
|
|
|
*/ |
19
|
|
|
class ResponseCache extends PageCache |
20
|
|
|
{ |
21
|
|
|
use CacheableTrait; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var array Define an array with the actions and a corresponding callable function. This will be called whether caching |
25
|
|
|
* is enabled or not or the response is loaded from the cache. |
26
|
|
|
* |
27
|
|
|
* ```php |
28
|
|
|
* 'actionsCallable' => ['get-posts' => function($result) { |
29
|
|
|
* // do something whether is the response cached or not |
30
|
|
|
* }); |
31
|
|
|
* ``` |
32
|
|
|
* @deprecated Replaced in favor of {{beforeCacheResponse}} and {{afterRestoreResponse}}. |
33
|
|
|
*/ |
34
|
|
|
public $actionsCallable = []; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var array |
38
|
|
|
* @deprecated Use {{$only}} or {{$except}} instead. |
39
|
|
|
*/ |
40
|
|
|
public $actions = []; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @inheritdoc |
44
|
|
|
*/ |
45
|
|
|
public function init() |
46
|
|
|
{ |
47
|
|
|
parent::init(); |
48
|
|
|
|
49
|
|
|
// support deprecated $actions property |
50
|
|
|
if (!empty($this->actions)) { |
|
|
|
|
51
|
|
|
$this->only = $this->actions; |
|
|
|
|
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* call the action callable if available. |
57
|
|
|
* |
58
|
|
|
* @param string $action The action ID name |
59
|
|
|
* @param string $result The cahed or not cached action response, this is a string as its after the action filters. |
60
|
|
|
*/ |
61
|
|
|
private function callActionCallable($action, $result) |
62
|
|
|
{ |
63
|
|
|
if (isset($this->actionsCallable[$action]) && is_callable($this->actionsCallable[$action])) { |
|
|
|
|
64
|
|
|
call_user_func($this->actionsCallable[$action], $result); |
|
|
|
|
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @inheritdoc |
70
|
|
|
*/ |
71
|
|
|
public function beforeAction($action) |
72
|
|
|
{ |
73
|
|
|
if (!$this->isCachable()) { |
74
|
|
|
return true; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$result = parent::beforeAction($action); |
78
|
|
|
|
79
|
|
|
// support legacy property. |
80
|
|
|
if (!empty($this->actionsCallable)) { |
|
|
|
|
81
|
|
|
$this->callActionCallable($action->id, Yii::$app->response->content); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $result; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
This property has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.