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