1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
5
|
|
|
* @license https://flipboxfactory.com/software/guardian/license |
6
|
|
|
* @link https://www.flipboxfactory.com/software/guardian/ |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace flipbox\spark\filters; |
10
|
|
|
|
11
|
|
|
use Craft; |
12
|
|
|
use craft\helpers\Template; |
13
|
|
|
use flipbox\spark\helpers\ViewHelper; |
14
|
|
|
use flipbox\spark\views\ViewInterface; |
15
|
|
|
use yii\base\ActionFilter; |
16
|
|
|
use yii\base\Exception; |
17
|
|
|
use yii\web\Controller; |
18
|
|
|
use yii\web\Response; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @author Flipbox Factory <[email protected]> |
22
|
|
|
* @since 1.0.0 |
23
|
|
|
* |
24
|
|
|
* @property Controller $sender |
25
|
|
|
*/ |
26
|
|
|
class RawFilter extends ActionFilter |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var array this property defines the transformers for each action. |
30
|
|
|
* Each action that should only support one transformer. |
31
|
|
|
* |
32
|
|
|
* You can use `'*'` to stand for all actions. When an action is explicitly |
33
|
|
|
* specified, it takes precedence over the specification given by `'*'`. |
34
|
|
|
* |
35
|
|
|
* For example, |
36
|
|
|
* |
37
|
|
|
* ```php |
38
|
|
|
* [ |
39
|
|
|
* 'create' => $view, |
40
|
|
|
* 'update' => $view, |
41
|
|
|
* 'delete' => $view, |
42
|
|
|
* '*' => $view, |
43
|
|
|
* ] |
44
|
|
|
* ``` |
45
|
|
|
*/ |
46
|
|
|
public $actions = []; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var array|ViewInterface |
50
|
|
|
*/ |
51
|
|
|
public $view; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param \yii\base\Action $action |
55
|
|
|
* @param mixed $result |
56
|
|
|
* @return mixed|\Twig_Markup |
57
|
|
|
*/ |
58
|
|
|
public function afterAction($action, $result) |
59
|
|
|
{ |
60
|
|
|
if (Craft::$app->getResponse()->format === Response::FORMAT_RAW) { |
61
|
|
|
return $this->renderTemplate($result); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return parent::afterAction($action, $result); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param $data |
69
|
|
|
* @return \Twig_Markup |
70
|
|
|
*/ |
71
|
|
|
protected function renderTemplate($data) |
72
|
|
|
{ |
73
|
|
|
if (!$view = $this->findView()) { |
74
|
|
|
return $data; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return Template::raw( |
78
|
|
|
$this->resolveView($view)->render($data) |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return ViewInterface|array|null |
84
|
|
|
* @throws Exception |
85
|
|
|
*/ |
86
|
|
|
protected function findView() |
87
|
|
|
{ |
88
|
|
|
// The requested action |
89
|
|
|
$action = Craft::$app->requestedAction->id; |
90
|
|
|
|
91
|
|
|
// Default view |
92
|
|
|
$view = $this->view; |
93
|
|
|
|
94
|
|
|
// Look for definitions |
95
|
|
|
if (isset($this->actions[$action])) { |
96
|
|
|
$view = $this->actions[$action]; |
97
|
|
|
} elseif (isset($this->actions['*'])) { |
98
|
|
|
$view = $this->actions['*']; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $view; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param $view |
106
|
|
|
* @return ViewInterface |
107
|
|
|
* @throws Exception |
108
|
|
|
*/ |
109
|
|
|
protected function resolveView($view): ViewInterface |
110
|
|
|
{ |
111
|
|
|
if (ViewHelper::isView($view)) { |
112
|
|
|
return $view; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
if (ViewHelper::isViewClass($view)) { |
116
|
|
|
return new $view(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$view = Craft::createObject($view); |
120
|
|
|
|
121
|
|
|
if (!$view instanceof ViewInterface) { |
122
|
|
|
throw new Exception("Invalid view"); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $view; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|