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