1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Imanghafoori\Widgets\Utils; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Debug\ExceptionHandler; |
6
|
|
|
|
7
|
|
|
class WidgetRenderer |
8
|
|
|
{ |
9
|
|
|
public $html; |
10
|
|
|
|
11
|
|
|
private $_viewData; |
12
|
|
|
|
13
|
|
|
private $_policies; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* BaseWidget constructor. |
17
|
|
|
*/ |
18
|
18 |
|
public function __construct() |
19
|
|
|
{ |
20
|
18 |
|
$this->_policies = app(Policies::class); |
21
|
18 |
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param $widget object|string |
25
|
|
|
* @param array $args |
26
|
|
|
* @return string |
27
|
|
|
*/ |
28
|
18 |
|
public function renderWidget($widget, ...$args) |
29
|
|
|
{ |
30
|
18 |
|
if (is_string($widget)) { |
31
|
1 |
|
$widget = $this->makeWidgetObj($widget); |
32
|
|
|
} |
33
|
|
|
|
34
|
18 |
|
app(Normalizer::class)->normalizeWidgetConfig($widget); |
35
|
|
|
|
36
|
18 |
|
if (app()->offsetExists('debugbar')) { |
37
|
|
|
app('widgetize.debugger')->addMessage(['widget class:' => $widget, 'args:' => $args]); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
try { |
41
|
18 |
|
$html = $this->generateHtml($widget, ...$args); |
42
|
|
|
} catch (\Exception $e) { |
43
|
|
|
return app()->make(ExceptionHandler::class)->render(app('request'), $e)->send(); |
44
|
|
|
} |
45
|
|
|
|
46
|
18 |
|
return $html; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param $widget object |
51
|
|
|
* @return \Illuminate\Foundation\Application|mixed |
52
|
|
|
*/ |
53
|
1 |
|
private function makeWidgetObj($widget) |
54
|
|
|
{ |
55
|
1 |
|
if (starts_with($widget, ['\\'])) { |
56
|
|
|
return app($widget); |
57
|
|
|
} |
58
|
|
|
|
59
|
1 |
|
$widget = app()->getNamespace().'Widgets\\'.$widget; |
|
|
|
|
60
|
|
|
|
61
|
1 |
|
return app($widget); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* It tries to get the html from cache if possible, otherwise generates it. |
66
|
|
|
* |
67
|
|
|
* @param $widget object |
68
|
|
|
* @param array ...$args |
69
|
|
|
* |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
|
|
private function generateHtml($widget, ...$args) |
73
|
|
|
{ |
74
|
|
|
// Everything inside this function is executed only when the cache is not available. |
75
|
18 |
|
$expensivePhpCode = function () use ($widget, $args) { |
76
|
16 |
|
$this->makeDataForView($widget, $args); |
77
|
|
|
|
78
|
16 |
|
return $this->renderTemplate($widget); |
79
|
18 |
|
}; |
80
|
|
|
|
81
|
18 |
|
if (! $widget->cacheView) { |
82
|
2 |
|
return $expensivePhpCode(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
// We first try to get the output from the cache before trying to run the expensive $expensivePhpCode... |
86
|
17 |
|
return app(Cache::class)->cacheResult($args, $expensivePhpCode, $widget); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param $widget object |
91
|
|
|
* @param $args array |
92
|
|
|
* |
93
|
|
|
* @return null |
94
|
|
|
*/ |
95
|
|
|
private function makeDataForView($widget, array $args) |
96
|
|
|
{ |
97
|
16 |
|
$expensiveCode = function () use ($widget, $args) { |
98
|
|
|
|
99
|
|
|
// Here we call the data method on the widget class. |
100
|
16 |
|
$viewData = \App::call($widget->controller, ...$args); |
|
|
|
|
101
|
|
|
|
102
|
16 |
|
if (($widget->presenter)) { |
103
|
|
|
// We make an object and call the `present` method on it. |
104
|
|
|
// Piping the data through the presenter before sending it to view. |
105
|
1 |
|
$viewData = \App::call($widget->presenter, [$viewData]); |
106
|
|
|
} |
107
|
|
|
|
108
|
16 |
|
return $viewData; |
109
|
16 |
|
}; |
110
|
|
|
|
111
|
16 |
|
if ($widget->cacheView) { |
112
|
15 |
|
$this->_viewData = $expensiveCode(); |
113
|
|
|
} else { |
114
|
2 |
|
$this->_viewData = app(Cache::class)->cacheResult($args, $expensiveCode, $widget, 'dataProvider'); |
|
|
|
|
115
|
|
|
} |
116
|
16 |
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param $widget object |
120
|
|
|
* @return string HTML output |
121
|
|
|
* @throws \Throwable |
122
|
|
|
*/ |
123
|
16 |
|
private function renderTemplate($widget) |
124
|
|
|
{ |
125
|
|
|
// Here we render the view file to raw html. |
126
|
16 |
|
$this->html = view($widget->template, [$widget->contextAs => $this->_viewData])->render(); |
127
|
|
|
|
128
|
|
|
// We try to minify the html before storing it in cache to save space. |
129
|
16 |
|
if ($this->_policies->widgetShouldBeMinified($widget)) { |
130
|
9 |
|
$this->html = app(HtmlMinifier::class)->minify($this->html); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
// We add some HTML comments before and after the widget output |
134
|
|
|
// So then, we will be able to easily identify the widget in browser's developer tool. |
135
|
16 |
|
if ($this->_policies->widgetShouldHaveDebugInfo()) { |
136
|
6 |
|
$this->html = app(DebugInfo::class)->addIdentifierToHtml($widget, $this->html); |
137
|
|
|
} |
138
|
|
|
|
139
|
16 |
|
return $this->html; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|