1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Imanghafoori\Widgets\Utils; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use Imanghafoori\Widgets\BaseWidget; |
7
|
|
|
|
8
|
|
|
class Normalizer |
9
|
|
|
{ |
10
|
|
|
private $widget; |
11
|
|
|
|
12
|
|
|
public function normalizeWidgetConfig(BaseWidget $widget) |
13
|
|
|
{ |
14
|
|
|
// to avoid normalizing a widget multiple times unnecessarily : |
15
|
|
|
if (isset($widget->isNormalized)) { |
16
|
|
|
return null; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
$this->widget = $widget; |
20
|
|
|
$this->normalizeControllerMethod(); |
21
|
|
|
$this->normalizePresenterName(); |
22
|
|
|
$this->normalizeTemplateName(); |
23
|
|
|
$this->normalizeContextAs(); |
24
|
|
|
$this->normalizeCacheLifeTime(); |
25
|
|
|
$this->normalizeCacheTags(); |
26
|
|
|
$widget->isNormalized = true; |
|
|
|
|
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Figures out which method should be called as the controller. |
31
|
|
|
* @return null |
32
|
|
|
*/ |
33
|
|
|
private function normalizeControllerMethod() |
34
|
|
|
{ |
35
|
|
|
// We decide to call data method on widget object by default. |
36
|
|
|
$controllerMethod = [$this->widget, 'data']; |
37
|
|
|
$ctrlClass = get_class($this->widget); |
38
|
|
|
|
39
|
|
|
// If the user has explicitly declared controller class path on widget |
40
|
|
|
// then we decide to call data method on that instead. |
41
|
|
|
if ($this->widget->controller) { |
42
|
|
|
$ctrlClass = $this->widget->controller; |
43
|
|
|
$controllerMethod = ($this->widget->controller) . '@data'; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if (!method_exists($ctrlClass, 'data')) { |
47
|
|
|
throw new \BadMethodCallException("'data' method not found on " . class_basename($this->widget)); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
if (!class_exists($ctrlClass)) { |
51
|
|
|
throw new \InvalidArgumentException("Controller class: [{$ctrlClass}] not found."); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$this->widget->controller = $controllerMethod; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Figures out which method should be called as the presenter |
59
|
|
|
* @return null |
60
|
|
|
*/ |
61
|
|
|
private function normalizePresenterName() |
62
|
|
|
{ |
63
|
|
|
if ($this->widget->presenter !== 'default') { |
64
|
|
|
if (!class_exists($this->widget->presenter)) { |
65
|
|
|
throw new \InvalidArgumentException("Presenter Class [{$this->widget->presenter}] not found."); |
66
|
|
|
} |
67
|
|
|
$presenter = $this->widget->presenter; |
68
|
|
|
} else { |
69
|
|
|
$presenter = get_class($this->widget) . 'Presenter'; |
70
|
|
|
$method = null; |
|
|
|
|
71
|
|
|
if (!class_exists($presenter)) { |
72
|
|
|
$this->widget->presenter = null; |
73
|
|
|
return ; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if (!method_exists($presenter, 'present')) { |
78
|
|
|
throw new \InvalidArgumentException("'present' method not found on : " . get_class($this->widget)); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$this->widget->presenter = $presenter . '@present'; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Figures out which template to render. |
86
|
|
|
* @return null |
87
|
|
|
*/ |
88
|
|
|
private function normalizeTemplateName() |
89
|
|
|
{ |
90
|
|
|
// class name without namespace. |
91
|
|
|
$className = str_replace('App\\Widgets\\', '', class_basename($this->widget)); |
92
|
|
|
// replace slashes with dots |
93
|
|
|
$className = str_replace(['\\', '/'], '.', $className); |
94
|
|
|
|
95
|
|
|
if ($this->widget->template === null) { |
96
|
|
|
$this->widget->template = 'Widgets::' . $className . 'View'; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
if (!view()->exists($this->widget->template)) { |
|
|
|
|
100
|
|
|
throw new \InvalidArgumentException("View file [{$className}View] not found by: '" . class_basename($this->widget) . " '"); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Figures out what the variable name should be in view file. |
106
|
|
|
* @return null |
107
|
|
|
*/ |
108
|
|
|
private function normalizeContextAs() |
109
|
|
|
{ |
110
|
|
|
// removes the $ sign. |
111
|
|
|
$this->widget->contextAs = str_replace('$', '', (string)$this->widget->contextAs); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* ّFigures out how long the cache life time should be. |
116
|
|
|
* @return null |
117
|
|
|
*/ |
118
|
|
|
private function normalizeCacheLifeTime() |
119
|
|
|
{ |
120
|
|
|
if ($this->widget->cacheLifeTime === 'env_default') { |
121
|
|
|
$this->widget->cacheLifeTime = (int)(env('WIDGET_DEFAULT_CACHE_LIFETIME', 0)); |
|
|
|
|
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
if ($this->widget->cacheLifeTime === 'forever') { |
125
|
|
|
$this->widget->cacheLifeTime = -1; |
|
|
|
|
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* ّFigures out what the cache tags should be. |
132
|
|
|
* @return null |
133
|
|
|
*/ |
134
|
|
|
private function normalizeCacheTags() |
135
|
|
|
{ |
136
|
|
|
if (!$this->cacheCanUseTags() || !$this->widget->cacheTags) { |
137
|
|
|
$this->widget->cacheTags = null; |
138
|
|
|
return null; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
if (is_string($this->widget->cacheTags)) { |
142
|
|
|
$this->widget->cacheTags = [$this->widget->cacheTags]; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
if (!is_array($this->widget->cacheTags)) { |
146
|
|
|
throw new \InvalidArgumentException('Cache Tags should be of type String or Array.'); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Determine whether cache tags should be applied or not |
152
|
|
|
* @return bool |
153
|
|
|
*/ |
154
|
|
|
private function cacheCanUseTags() |
155
|
|
|
{ |
156
|
|
|
return !in_array(env('CACHE_DRIVER', 'file'), ['file', 'database']); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
} |
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.