1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Microboard\Foundations\Traits; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
|
8
|
|
|
trait ViewResolverTrait |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @param $file |
12
|
|
|
* @return string |
13
|
|
|
*/ |
14
|
|
|
protected function getViewPathFor($file) |
15
|
|
|
{ |
16
|
|
|
if (view()->exists($view = $this->buildVariables()['viewsPrefix'] . ".{$file}")) { |
|
|
|
|
17
|
|
|
return $view; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
return "microboard::resource.{$file}"; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Build variables depends on given $attributes. |
25
|
|
|
* |
26
|
|
|
* @param Model|null $model |
27
|
|
|
* @return array |
28
|
|
|
*/ |
29
|
|
|
protected function buildVariables(?Model $model = null) |
30
|
|
|
{ |
31
|
|
|
$routePrefix = 'microboard'; |
32
|
|
|
$translationsPrefix = ''; |
33
|
|
|
$viewsPrefix = ''; |
34
|
|
|
$viewsPath = 'admin'; |
35
|
|
|
|
36
|
|
|
if (property_exists($this, 'attributes')) { |
37
|
|
|
if (isset($this->attributes['routes_prefix'])) { |
38
|
|
|
$routePrefix = $this->attributes['routes_prefix']; |
|
|
|
|
39
|
|
|
} |
40
|
|
|
if (isset($this->attributes['translations_prefix'])) { |
41
|
|
|
$translationsPrefix = $this->attributes['translations_prefix']; |
42
|
|
|
} |
43
|
|
|
if (isset($this->attributes['views_prefix'])) { |
44
|
|
|
$viewsPrefix = $this->attributes['views_prefix']; |
45
|
|
|
} |
46
|
|
|
if (isset($this->attributes['views_path'])) { |
47
|
|
|
$viewsPath = $this->attributes['views_path']; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return [ |
52
|
|
|
'resource' => $this->model, |
|
|
|
|
53
|
|
|
'model' => $model, |
54
|
|
|
'resourceName' => $name = Str::of($this->baseName)->lower()->plural(), |
|
|
|
|
55
|
|
|
'resourceVariable' => Str::of($this->baseName)->lower(), |
56
|
|
|
'routePrefix' => $this->getRightPrefixFor($routePrefix, '.', $name), |
57
|
|
|
'translationsPrefix' => $this->getRightPrefixFor($translationsPrefix, '::', $name), |
58
|
|
|
'viewsPrefix' => $this->getRightPrefixFor( |
59
|
|
|
$viewsPrefix, '::', |
60
|
|
|
$this->getRightPrefixFor($viewsPath, '.', $name) |
61
|
|
|
), |
62
|
|
|
]; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Join given prefix and $resource with the delimiter. |
67
|
|
|
* |
68
|
|
|
* @param string $prefix |
69
|
|
|
* @param string $delimiter |
70
|
|
|
* @param string $resource |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
|
|
protected function getRightPrefixFor($prefix = '', $delimiter = '.', $resource = '') |
74
|
|
|
{ |
75
|
|
|
return ( |
76
|
|
|
$prefix ? $prefix . $delimiter : '' |
77
|
|
|
) . $resource; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param $view |
82
|
|
|
* @param Model|null $model |
83
|
|
|
* @return array |
84
|
|
|
*/ |
85
|
|
|
protected function getResourceVariables($view, ?Model $model = null): array |
86
|
|
|
{ |
87
|
|
|
return array_merge([ |
88
|
|
|
'widgets' => $this->getWidgetsFor($view) |
|
|
|
|
89
|
|
|
], $this->buildVariables($model)); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
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: