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