|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Imanghafoori\Widgets\Utils; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Debug\ExceptionHandler; |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
class WidgetRenderer |
|
9
|
|
|
{ |
|
10
|
|
|
public $html; |
|
11
|
|
|
private $viewData; |
|
12
|
|
|
private $policies; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* BaseWidget constructor. |
|
16
|
|
|
*/ |
|
17
|
|
|
public function __construct() |
|
18
|
|
|
{ |
|
19
|
|
|
$this->policies = app('imanghafoori.widget.policies'); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param $widget |
|
24
|
|
|
* @param array $args |
|
25
|
|
|
* @return string |
|
26
|
|
|
*/ |
|
27
|
|
|
public function renderWidget($widget, ...$args) |
|
28
|
|
|
{ |
|
29
|
|
|
app('imanghafoori.widget.normalizer')->normalizeWidgetConfig($widget); |
|
30
|
|
|
try { |
|
31
|
|
|
$html = $this->generateHtml($widget, ...$args); |
|
32
|
|
|
} catch (\Exception $e) { |
|
33
|
|
|
return app()->make(ExceptionHandler::class)->render(app('request'), $e)->send(); |
|
34
|
|
|
} |
|
35
|
|
|
return $html; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* It tries to get the html from cache if possible, otherwise generates it. |
|
40
|
|
|
* @param $widget |
|
41
|
|
|
* @param array ...$args |
|
42
|
|
|
* @return string |
|
43
|
|
|
*/ |
|
44
|
|
|
private function generateHtml($widget, ...$args) |
|
45
|
|
|
{ |
|
46
|
|
|
// Everything inside this function is executed only when the cache is not available. |
|
47
|
|
|
$expensivePhpCode = function () use ($widget, $args) { |
|
48
|
|
|
$this->prepareDataForView($widget, $args); |
|
49
|
|
|
// render the template with the resulting data. |
|
50
|
|
|
return $this->renderTemplate($widget); |
|
51
|
|
|
}; |
|
52
|
|
|
|
|
53
|
|
|
// We first try to get the output from the cache before trying to run the expensive $expensivePhpCode... |
|
54
|
|
|
if ($this->policies->widgetShouldUseCache($widget->cacheLifeTime)) { |
|
55
|
|
|
return app('imanghafoori.widget.cache')->cacheResult($args, $expensivePhpCode, $widget); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return $expensivePhpCode(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param $widget |
|
64
|
|
|
* @param $args |
|
65
|
|
|
* @return null |
|
66
|
|
|
*/ |
|
67
|
|
|
private function prepareDataForView($widget, $args) |
|
68
|
|
|
{ |
|
69
|
|
|
// Here we call the data method on the widget class. |
|
70
|
|
|
$viewData = \App::call($widget->controller, $args); |
|
71
|
|
|
|
|
72
|
|
|
if (($widget->presenter)) { |
|
73
|
|
|
// We make an object and call the `present` method on it. |
|
74
|
|
|
// Piping the data through the presenter before sending it to view. |
|
75
|
|
|
$viewData = \App::call($widget->presenter, [$viewData]); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
$this->viewData = $viewData; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
private function renderTemplate($widget) |
|
82
|
|
|
{ |
|
83
|
|
|
// Here we render the view file to raw html. |
|
84
|
|
|
$this->html = view($widget->template, [$widget->contextAs => $this->viewData])->render(); |
|
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
// We try to minify the html before storing it in cache to save space. |
|
87
|
|
|
if ($this->policies->widgetShouldBeMinified()) { |
|
88
|
|
|
$this->html = app('imanghafoori.widget.minifier')->minify($this->html); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
// We add some HTML comments before and after the widget output |
|
92
|
|
|
// So then, we will be able to easily identify the widget in browser's developer tool. |
|
93
|
|
|
if ($this->policies->widgetShouldHaveDebugInfo()) { |
|
94
|
|
|
$this->html = app('imanghafoori.widget.debugInfo')->addIdentifierToHtml($widget, $this->html); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
return $this->html; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
} |
|
102
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: