1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaravelFlare\Flare; |
4
|
|
|
|
5
|
|
|
use Illuminate\Routing\Router; |
6
|
|
|
use LaravelFlare\Flare\Admin\AdminManager; |
7
|
|
|
use LaravelFlare\Flare\Admin\Attributes\BaseAttribute; |
8
|
|
|
|
9
|
|
|
class Flare |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* The Flare version. |
13
|
|
|
* |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
const VERSION = '0.9.x-dev'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Array of expected configuration keys |
20
|
|
|
* with the absolute bare-minimum defaults. |
21
|
|
|
* |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
protected $configurationKeys = [ |
25
|
|
|
'admin_title' => 'Laravel Flare', |
26
|
|
|
'admin_url' => 'admin', |
27
|
|
|
'admin_theme' => 'red', |
28
|
|
|
'admin' => [], |
29
|
|
|
'attributes' => [], |
30
|
|
|
'models' => [], |
31
|
|
|
'modules' => [], |
32
|
|
|
'widgets' => [], |
33
|
|
|
'permissions' => \LaravelFlare\Flare\Permissions\Permissions::class, |
34
|
|
|
'policies' => [], |
35
|
|
|
'show' => [ |
36
|
|
|
'github' => true, |
37
|
|
|
'login' => true, |
38
|
|
|
'notifications' => true, |
39
|
|
|
'version' => true, |
40
|
|
|
] |
41
|
|
|
]; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Admin Manager |
45
|
|
|
* |
46
|
|
|
* @var \LaravelFlare\Flare\Admin\AdminManager |
47
|
|
|
*/ |
48
|
|
|
protected $admin; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Flare Configuration |
52
|
|
|
* |
53
|
|
|
* @var array |
54
|
|
|
*/ |
55
|
|
|
protected $config; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* The Title of the Admin Panel |
59
|
|
|
* |
60
|
|
|
* @var string |
61
|
|
|
*/ |
62
|
|
|
protected $adminTitle; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Safe Title of the Admin Panel |
66
|
|
|
* |
67
|
|
|
* @var string |
68
|
|
|
*/ |
69
|
|
|
protected $safeAdminTitle; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Relative Base URL of Admin Panel |
73
|
|
|
* |
74
|
|
|
* @var string |
75
|
|
|
*/ |
76
|
|
|
protected $relativeAdminUrl; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* __construct. |
80
|
|
|
*/ |
81
|
|
|
public function __construct(AdminManager $adminManager) |
82
|
|
|
{ |
83
|
|
|
$this->setLoadedConfig(); |
84
|
|
|
|
85
|
|
|
$this->admin = $adminManager; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Returns the instance of the Admin Manager |
90
|
|
|
* |
91
|
|
|
* @return \LaravelFlare\Flare\Admin\AdminManager |
92
|
|
|
*/ |
93
|
|
|
public function admin() |
94
|
|
|
{ |
95
|
|
|
return $this->admin; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Returns a Flare configuration value(s). |
100
|
|
|
* |
101
|
|
|
* @param string $key |
102
|
|
|
* |
103
|
|
|
* @return mixed |
104
|
|
|
*/ |
105
|
|
|
public function config($key) |
106
|
|
|
{ |
107
|
|
|
return $this->getConfig($key); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Returns a Flare configuration value(s), falling back |
112
|
|
|
* to the defined bare-minimum configuration defaults |
113
|
|
|
* if, for whatever reason the config is undefined. |
114
|
|
|
* |
115
|
|
|
* @param string $key |
116
|
|
|
* |
117
|
|
|
* @return mixed |
118
|
|
|
*/ |
119
|
|
|
public function getConfig($key) |
120
|
|
|
{ |
121
|
|
|
if (array_key_exists($key, $this->config)) { |
122
|
|
|
return $this->config[$key]; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return config('flare.'.$key); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Allow setting of the Flare config at runtime. |
130
|
|
|
* |
131
|
|
|
* @return void |
132
|
|
|
*/ |
133
|
|
|
public function setConfig() |
134
|
|
|
{ |
135
|
|
|
|
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Set the loaded config to the protected property. |
140
|
|
|
* |
141
|
|
|
* @return void |
142
|
|
|
*/ |
143
|
|
|
public function setLoadedConfig() |
144
|
|
|
{ |
145
|
|
|
$this->config = config('flare.config'); |
|
|
|
|
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @return string |
150
|
|
|
* |
151
|
|
|
* @deprecated 0.9 Use getAdminTitle() instead. |
152
|
|
|
*/ |
153
|
|
|
public function adminTitle() |
154
|
|
|
{ |
155
|
|
|
return $this->getAdminTitle(); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Returns the defined Admin Title. |
160
|
|
|
* |
161
|
|
|
* @return string |
162
|
|
|
*/ |
163
|
|
|
public function getAdminTitle() |
164
|
|
|
{ |
165
|
|
|
return $this->adminTitle ? $this->adminTitle : \Flare::config('admin_title'); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Sets the Admin Title |
170
|
|
|
* |
171
|
|
|
* @param mixed $title |
172
|
|
|
* |
173
|
|
|
* @return void |
174
|
|
|
*/ |
175
|
|
|
public function setAdminTitle($title = null) |
176
|
|
|
{ |
177
|
|
|
$this->adminTitle = $title; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @return string |
182
|
|
|
* |
183
|
|
|
* @deprecated 0.9 Use getSafeAdminTitle() instead. |
184
|
|
|
*/ |
185
|
|
|
public function safeAdminTitle() |
186
|
|
|
{ |
187
|
|
|
return $this->getSafeAdminTitle(); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Returns the defined Admin Title, converted |
192
|
|
|
* to a safer format (for <title> tags etc.). |
193
|
|
|
* |
194
|
|
|
* @return string |
195
|
|
|
*/ |
196
|
|
|
public function getSafeAdminTitle() |
197
|
|
|
{ |
198
|
|
|
return $this->safeAdminTitle ? $this->adminTitle : strip_tags(\Flare::config('admin_title')); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Sets the Safe Admin Title which is used |
203
|
|
|
* in <title> tags etc. |
204
|
|
|
* |
205
|
|
|
* @param mixed $title |
206
|
|
|
* |
207
|
|
|
* @return void |
208
|
|
|
*/ |
209
|
|
|
public function setSafeAdminTitle($title = null) |
210
|
|
|
{ |
211
|
|
|
$this->safeAdminTitle = $title; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Returns URL to a path in the Admin Panel, using the |
216
|
|
|
* Admin URL defined in the Flare Config. |
217
|
|
|
* |
218
|
|
|
* @param string $path |
219
|
|
|
* |
220
|
|
|
* @return string |
221
|
|
|
*/ |
222
|
|
|
public function adminUrl($path = '') |
223
|
|
|
{ |
224
|
|
|
return url($this->relativeAdminUrl($path)); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Returns URL to a path in the Admin Panel, using the |
229
|
|
|
* Admin URL defined in the Flare Config. |
230
|
|
|
* |
231
|
|
|
* @param string $path |
232
|
|
|
* |
233
|
|
|
* @return string |
234
|
|
|
*/ |
235
|
|
|
public function relativeAdminUrl($path = '') |
236
|
|
|
{ |
237
|
|
|
return rtrim($this->getRelativeAdminUrl().'/'.$path, '/'); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Returns URL to a path in the Admin Panel, using the |
242
|
|
|
* Admin URL defined in the Flare Config. |
243
|
|
|
* |
244
|
|
|
* @return string |
245
|
|
|
*/ |
246
|
|
|
public function getRelativeAdminUrl() |
247
|
|
|
{ |
248
|
|
|
return $this->relativeAdminUrl ? $this->relativeAdminUrl : \Flare::config('admin_url'); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Set the Flare Relative Admin URL. |
253
|
|
|
* |
254
|
|
|
* If the provided path is null the relative path provided |
255
|
|
|
* with the getRelativeAdminUrl() method will return the |
256
|
|
|
* configuration file default (or the Flare fallbacks). |
257
|
|
|
* |
258
|
|
|
* @param mixed $path |
259
|
|
|
*/ |
260
|
|
|
public function setRelativeAdminUrl($path = null) |
261
|
|
|
{ |
262
|
|
|
$this->relativeAdminUrl = $path; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Returns URL to a path in the Flare Documentation. |
267
|
|
|
* This is COMING SOON! |
268
|
|
|
* |
269
|
|
|
* @param string $path |
270
|
|
|
* |
271
|
|
|
* @return string |
272
|
|
|
*/ |
273
|
|
|
public function docsUrl($path = '') |
274
|
|
|
{ |
275
|
|
|
return url('#'.$path); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* Determines whether part of the Flare Admin Panel |
280
|
|
|
* should be displayed or not and returns true / false. |
281
|
|
|
* |
282
|
|
|
* @param string $key |
283
|
|
|
* |
284
|
|
|
* @return boolean |
285
|
|
|
*/ |
286
|
|
|
public function show($key = false) |
287
|
|
|
{ |
288
|
|
|
if (!$key) { |
|
|
|
|
289
|
|
|
return false; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
return $this->getShow($key); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* Determines whether part of the Flare Admin Panel |
297
|
|
|
* should be displayed or not and returns true / false. |
298
|
|
|
* |
299
|
|
|
* Accessor for getShow(). |
300
|
|
|
* |
301
|
|
|
* @param string $key |
302
|
|
|
* |
303
|
|
|
* @return boolean |
304
|
|
|
*/ |
305
|
|
|
public function getShow($key = false) |
306
|
|
|
{ |
307
|
|
|
if (array_key_exists($key, $showConfig = $this->getConfig('show'))) { |
308
|
|
|
return $showConfig[$key]; |
309
|
|
|
} |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* Returns the current Flare Version. |
314
|
|
|
* |
315
|
|
|
* @return string |
316
|
|
|
*/ |
317
|
|
|
public function version() |
318
|
|
|
{ |
319
|
|
|
return self::VERSION; |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* Returns an array of all of the Available Attribute Types. |
324
|
|
|
* |
325
|
|
|
* @return array |
326
|
|
|
*/ |
327
|
|
|
protected function availableAttributes() |
328
|
|
|
{ |
329
|
|
|
$availableAttributes = []; |
330
|
|
|
|
331
|
|
|
foreach (\Flare::config('attributes') as $attributeFullClassname) { |
332
|
|
|
$availableAttributes = array_add( |
333
|
|
|
$availableAttributes, |
334
|
|
|
class_basename($attributeFullClassname), |
335
|
|
|
$attributeFullClassname |
336
|
|
|
); |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
return $availableAttributes; |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* Determines if an AttributeType class exists or not. |
344
|
|
|
* |
345
|
|
|
* @param string $type |
346
|
|
|
* |
347
|
|
|
* @return bool |
348
|
|
|
*/ |
349
|
|
|
protected function attributeTypeExists($type) |
350
|
|
|
{ |
351
|
|
|
return $this->resolveAttributeClass($type) ? true : false; |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
/** |
355
|
|
|
* Render Attribute. |
356
|
|
|
* |
357
|
|
|
* @param string $action |
358
|
|
|
* @param string $attribute |
359
|
|
|
* @param string $field |
360
|
|
|
* @param string $model |
361
|
|
|
* @param string $modelManager |
362
|
|
|
* |
363
|
|
|
* @return \Illuminate\Http\Response |
364
|
|
|
*/ |
365
|
|
|
public function renderAttribute($action, $attribute, $field, $model, $modelManager) |
366
|
|
|
{ |
367
|
|
|
if (!isset($field['type'])) { |
368
|
|
|
throw new \Exception('Attribute Field Type cannot be empty or undefined.'); |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
if ($this->attributeTypeExists($field['type'])) { |
372
|
|
|
$fieldType = $this->resolveAttributeClass($field['type']); |
373
|
|
|
|
374
|
|
|
return call_user_func_array([new $fieldType($attribute, $field, $model, $modelManager), camel_case('render_'.$action)], []); |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
return call_user_func_array([new BaseAttribute($attribute, $field, $model, $modelManager), camel_case('render_'.$action)], []); |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
/** |
381
|
|
|
* Resolves the Class of an Attribute and returns it as a string. |
382
|
|
|
* |
383
|
|
|
* @param string $type |
384
|
|
|
* |
385
|
|
|
* @return string |
386
|
|
|
*/ |
387
|
|
|
protected function resolveAttributeClass($type) |
388
|
|
|
{ |
389
|
|
|
$fullClassname = array_key_exists(title_case($type).'Attribute', $this->availableAttributes()) ? $this->availableAttributes()[title_case($type).'Attribute'] : false; |
390
|
|
|
|
391
|
|
|
if (!$fullClassname || !class_exists($fullClassname)) { |
392
|
|
|
return false; |
|
|
|
|
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
return $fullClassname; |
396
|
|
|
} |
397
|
|
|
} |
398
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..