1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaravelFlare\Flare; |
4
|
|
|
|
5
|
|
|
use Illuminate\Routing\Router; |
6
|
|
|
use LaravelFlare\Flare\Admin\Attributes\BaseAttribute; |
7
|
|
|
|
8
|
|
|
class Flare |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* The Flare version. |
12
|
|
|
* |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
const VERSION = '0.9.x-dev'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Array of expected configuration keys |
19
|
|
|
* with the absolute bare-minimum defaults. |
20
|
|
|
* |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
protected $configurationKeys = [ |
24
|
|
|
'admin_title' => 'Laravel Flare', |
25
|
|
|
'admin_url' => 'admin', |
26
|
|
|
'admin_theme' => 'red', |
27
|
|
|
'admin' => [], |
28
|
|
|
'attributes' => [], |
29
|
|
|
'models' => [], |
30
|
|
|
'modules' => [], |
31
|
|
|
'widgets' => [], |
32
|
|
|
'permissions' => \LaravelFlare\Flare\Permissions\Permissions::class, |
33
|
|
|
'policies' => [], |
34
|
|
|
'core_notifications' => true, |
35
|
|
|
]; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The Title of the Admin Panel |
39
|
|
|
* |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
protected $adminTitle; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Safe Title of the Admin Panel |
46
|
|
|
* |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
protected $safeAdminTitle; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Relative Base URL of Admin Panel |
53
|
|
|
* |
54
|
|
|
* @var string |
55
|
|
|
*/ |
56
|
|
|
protected $relativeAdminUrl; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* __construct. |
60
|
|
|
*/ |
61
|
|
|
public function __construct(Router $router) |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
|
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Returns Flare configuration values, falling |
68
|
|
|
* back to the defined bare-minimum configuration |
69
|
|
|
* defaults if, for whatever reason the config is |
70
|
|
|
* undefined. |
71
|
|
|
* |
72
|
|
|
* @param string $key |
73
|
|
|
* |
74
|
|
|
* @return mixed |
75
|
|
|
*/ |
76
|
|
|
public function config($key) |
77
|
|
|
{ |
78
|
|
|
if (array_key_exists($key, $this->configurationKeys)) { |
79
|
|
|
return config('flare.config.'.$key, $this->configurationKeys[$key]); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return config('flare.'.$key); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return string |
87
|
|
|
* |
88
|
|
|
* @deprecated 0.9 Use getAdminTitle() instead. |
89
|
|
|
*/ |
90
|
|
|
public function adminTitle() |
91
|
|
|
{ |
92
|
|
|
return $this->getAdminTitle(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Returns the defined Admin Title. |
97
|
|
|
* |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
|
|
public function getAdminTitle() |
101
|
|
|
{ |
102
|
|
|
return $this->adminTitle ? $this->adminTitle : \Flare::config('admin_title'); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Sets the Admin Title |
107
|
|
|
* |
108
|
|
|
* @param mixed $title |
109
|
|
|
* |
110
|
|
|
* @return void |
111
|
|
|
*/ |
112
|
|
|
public function setAdminTitle($title = null) |
113
|
|
|
{ |
114
|
|
|
$this->adminTitle = $title; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return string |
119
|
|
|
* |
120
|
|
|
* @deprecated 0.9 Use getSafeAdminTitle() instead. |
121
|
|
|
*/ |
122
|
|
|
public function safeAdminTitle() |
123
|
|
|
{ |
124
|
|
|
return $this->getSafeAdminTitle(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Returns the defined Admin Title, converted |
129
|
|
|
* to a safer format (for <title> tags etc.). |
130
|
|
|
* |
131
|
|
|
* @return string |
132
|
|
|
*/ |
133
|
|
|
public function getSafeAdminTitle() |
134
|
|
|
{ |
135
|
|
|
return $this->safeAdminTitle ? $this->adminTitle : strip_tags(\Flare::config('admin_title')); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Sets the Safe Admin Title which is used |
140
|
|
|
* in <title> tags etc. |
141
|
|
|
* |
142
|
|
|
* @param mixed $title |
143
|
|
|
* |
144
|
|
|
* @return void |
145
|
|
|
*/ |
146
|
|
|
public function setSafeAdminTitle($title = null) |
147
|
|
|
{ |
148
|
|
|
$this->safeAdminTitle = $title; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Returns URL to a path in the Admin Panel, using the |
153
|
|
|
* Admin URL defined in the Flare Config. |
154
|
|
|
* |
155
|
|
|
* @param string $path |
156
|
|
|
* |
157
|
|
|
* @return string |
158
|
|
|
*/ |
159
|
|
|
public function adminUrl($path = '') |
160
|
|
|
{ |
161
|
|
|
return url($this->getRelativeAdminUrl($path)); |
|
|
|
|
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Returns URL to a path in the Admin Panel, using the |
166
|
|
|
* Admin URL defined in the Flare Config. |
167
|
|
|
* |
168
|
|
|
* @param string $path |
169
|
|
|
* |
170
|
|
|
* @return string |
171
|
|
|
*/ |
172
|
|
|
public function relativeAdminUrl($path = '') |
173
|
|
|
{ |
174
|
|
|
return rtrim($this->getRelativeAdminUrl().'/'.$path, '/'); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Returns URL to a path in the Admin Panel, using the |
179
|
|
|
* Admin URL defined in the Flare Config. |
180
|
|
|
* |
181
|
|
|
* @return string |
182
|
|
|
*/ |
183
|
|
|
public function getRelativeAdminUrl() |
184
|
|
|
{ |
185
|
|
|
return $this->relativeAdminUrl ? $this->relativeAdminUrl : \Flare::config('admin_url'); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Set the Flare Relative Admin URL. |
190
|
|
|
* |
191
|
|
|
* If the provided path is null the relative path provided |
192
|
|
|
* with the getRelativeAdminUrl() method will return the |
193
|
|
|
* configuration file default (or the Flare fallbacks). |
194
|
|
|
* |
195
|
|
|
* @param mixed $path |
196
|
|
|
*/ |
197
|
|
|
public function setRelativeAdminUrl($path = null) |
198
|
|
|
{ |
199
|
|
|
$this->relativeAdminUrl = $path; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Returns URL to a path in the Flare Documentation. |
204
|
|
|
* This is COMING SOON! |
205
|
|
|
* |
206
|
|
|
* @param string $path |
207
|
|
|
* |
208
|
|
|
* @return string |
209
|
|
|
*/ |
210
|
|
|
public function docsUrl($path = '') |
211
|
|
|
{ |
212
|
|
|
return url('#'.$path); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Returns an array of all of the Available Attribute Types. |
217
|
|
|
* |
218
|
|
|
* @return array |
219
|
|
|
*/ |
220
|
|
|
protected function availableAttributes() |
221
|
|
|
{ |
222
|
|
|
$availableAttributes = []; |
223
|
|
|
|
224
|
|
|
foreach (\Flare::config('attributes') as $attributeFullClassname) { |
225
|
|
|
$availableAttributes = array_add( |
226
|
|
|
$availableAttributes, |
227
|
|
|
class_basename($attributeFullClassname), |
228
|
|
|
$attributeFullClassname |
229
|
|
|
); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
return $availableAttributes; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Determines if an AttributeType class exists or not. |
237
|
|
|
* |
238
|
|
|
* @param string $type |
239
|
|
|
* |
240
|
|
|
* @return bool |
241
|
|
|
*/ |
242
|
|
|
protected function attributeTypeExists($type) |
243
|
|
|
{ |
244
|
|
|
return $this->resolveAttributeClass($type) ? true : false; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Render Attribute. |
249
|
|
|
* |
250
|
|
|
* @param string $action |
251
|
|
|
* @param string $attribute |
252
|
|
|
* @param string $field |
253
|
|
|
* @param string $model |
254
|
|
|
* @param string $modelManager |
255
|
|
|
* |
256
|
|
|
* @return \Illuminate\Http\Response |
257
|
|
|
*/ |
258
|
|
|
public function renderAttribute($action, $attribute, $field, $model, $modelManager) |
259
|
|
|
{ |
260
|
|
|
if (!isset($field['type'])) { |
261
|
|
|
throw new \Exception('Attribute Field Type cannot be empty or undefined.'); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
if ($this->attributeTypeExists($field['type'])) { |
265
|
|
|
$fieldType = $this->resolveAttributeClass($field['type']); |
266
|
|
|
|
267
|
|
|
return call_user_func_array([new $fieldType($attribute, $field, $model, $modelManager), camel_case('render_'.$action)], []); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
return call_user_func_array([new BaseAttribute($attribute, $field, $model, $modelManager), camel_case('render_'.$action)], []); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Resolves the Class of an Attribute and returns it as a string. |
275
|
|
|
* |
276
|
|
|
* @param string $type |
277
|
|
|
* |
278
|
|
|
* @return string |
279
|
|
|
*/ |
280
|
|
|
protected function resolveAttributeClass($type) |
281
|
|
|
{ |
282
|
|
|
$fullClassname = array_key_exists(title_case($type).'Attribute', $this->availableAttributes()) ? $this->availableAttributes()[title_case($type).'Attribute'] : false; |
283
|
|
|
|
284
|
|
|
if (!$fullClassname || !class_exists($fullClassname)) { |
285
|
|
|
return false; |
|
|
|
|
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
return $fullClassname; |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* Returns the current Flare Version. |
293
|
|
|
* |
294
|
|
|
* @return string |
295
|
|
|
*/ |
296
|
|
|
public function version() |
297
|
|
|
{ |
298
|
|
|
return self::VERSION; |
299
|
|
|
} |
300
|
|
|
} |
301
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.