1
|
|
|
<?php |
2
|
|
|
namespace Anavel\Crud\View\Composers; |
3
|
|
|
|
4
|
|
|
use Anavel\Crud\Contracts\Abstractor\ModelFactory as ModelAbstractorFactory; |
5
|
|
|
use Route; |
6
|
|
|
use Gate; |
7
|
|
|
|
8
|
|
|
class SidebarComposer |
9
|
|
|
{ |
10
|
|
|
protected $modelFactory; |
11
|
|
|
|
12
|
|
|
public function __construct(ModelAbstractorFactory $modelFactory) |
13
|
|
|
{ |
14
|
|
|
$this->modelFactory = $modelFactory; |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
public function compose($view) |
18
|
|
|
{ |
19
|
|
|
$menuItems = $this->getSidebarItems(); |
20
|
|
|
$view->with([ |
21
|
|
|
'menuItems' => $menuItems, |
22
|
|
|
'headerName' => config('anavel-crud.name') |
23
|
|
|
]); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @return array |
28
|
|
|
*/ |
29
|
|
|
private function getSidebarItems() |
30
|
|
|
{ |
31
|
|
|
$modelsGroups = config('anavel-crud.modelsGroups'); |
32
|
|
|
$models = config('anavel-crud.models'); |
33
|
|
|
$menuItems = []; |
34
|
|
|
|
35
|
|
|
if (!is_null($modelsGroups)) { |
36
|
|
|
foreach ($modelsGroups as $group => $items) { |
|
|
|
|
37
|
|
|
$menuItems[$group]['isActive'] = false; |
38
|
|
|
$menuItems[$group]['name'] = $group; |
39
|
|
|
$menuItems[$group]['items'] = []; |
40
|
|
|
foreach ($items as $itemName) { |
41
|
|
|
try { |
42
|
|
|
$item = $this->getModelItem($itemName); |
43
|
|
|
$menuItems[$group]['items'][] = $item; |
44
|
|
|
if (!$menuItems[$group]['isActive'] && $item['isActive']) { |
45
|
|
|
$menuItems[$group]['isActive'] = true; |
46
|
|
|
} |
47
|
|
|
if (isset($models[$itemName])) { |
48
|
|
|
unset($models[$itemName]); |
49
|
|
|
} |
50
|
|
|
} catch (\Exception $e) { |
51
|
|
|
continue; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
foreach ($models as $modelName => $model) { |
58
|
|
|
try { |
59
|
|
|
$item = $this->getModelItem($modelName); |
60
|
|
|
} catch (\Exception $e) { |
61
|
|
|
continue; |
62
|
|
|
} |
63
|
|
|
$menuItems[$modelName]['isActive'] = $item['isActive']; |
64
|
|
|
$menuItems[$modelName]['name'] = $item['name']; |
65
|
|
|
$menuItems[$modelName]['items'][] = $item; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
//Sort alphabetically de menu items |
69
|
|
|
usort($menuItems, function ($itemA, $itemB) { |
70
|
|
|
return strcmp($itemA['name'], $itemB['name']); |
71
|
|
|
}); |
72
|
|
|
return $menuItems; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param $modelName |
77
|
|
|
* @return array |
78
|
|
|
* @throws \Exception |
79
|
|
|
*/ |
80
|
|
|
private function getModelItem($modelName) |
81
|
|
|
{ |
82
|
|
|
$modelAbstractor = $this->modelFactory->getByName($modelName); |
83
|
|
|
if (array_key_exists('authorize', $config = $modelAbstractor->getConfig()) && $config['authorize'] === true) { |
84
|
|
|
if (Gate::denies('adminIndex', $modelAbstractor->getInstance())) { |
85
|
|
|
throw new \Exception('Access denied'); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$isActive = false; |
90
|
|
|
if (Route::current()->getParameter('model') === $modelAbstractor->getSlug()) { |
91
|
|
|
$isActive = true; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$item = [ |
95
|
|
|
'route' => route('anavel-crud.model.index', $modelAbstractor->getSlug()), |
96
|
|
|
'name' => $modelAbstractor->getName(), |
97
|
|
|
'isActive' => $isActive |
98
|
|
|
]; |
99
|
|
|
|
100
|
|
|
return $item; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.