--- /dev/null
+<?php
+
+namespace Cubist\Backpack\Console\Commands;
+
+use Illuminate\Support\Facades\Artisan;
+
+class MenuGenerate extends CubistCommand
+{
+ protected $signature = 'cubist:menu:generate';
+
+ public function handle()
+ {
+
+ Artisan::call('config:cache');
+ $items = config('backpack.menu.items');
+
+ $res = '';
+ foreach ($items as $item) {
+ $res .= $this->render($item);
+ }
+ file_put_contents(resource_path('views/vendor/backpack/ui/inc/menu_items.blade.php'), $res);
+ Artisan::call('view:cache');
+ }
+
+ protected function render($item, $level = 0, $type = "menu-item")
+ {
+ if ($item === '----') {
+ return;
+ return str_repeat("\t", $level) . '<x-backpack::menu-separator title="--" />' . "\n";
+ }
+ $res = '';
+ $can = $this->getCan($item);
+ $litem = $level;
+ if ($can) {
+ $litem = $level + 1;
+ $res .= str_repeat("\t", $level) . $can . "\n";
+ }
+ if (isset($item['items'])) {
+ $res .= str_repeat("\t", $litem) . '<x-backpack::menu-dropdown title="{!!__(\'' . addcslashes($item['label'], "'") . '\')!!}" icon="' . $item['icon'] . '">' . "\n";
+ foreach ($item['items'] as $i) {
+ $res .= $this->render($i, $litem + 1, 'menu-dropdown-item');
+ }
+ $res .= str_repeat("\t", $litem) . '</x-backpack::menu-dropdown>' . "\n";
+ } else {
+ $res .= str_repeat("\t", $litem) . '<x-backpack::' . $type . ' title="{!!__(\'' . addcslashes($item['label'], "'") . '\')!!}" icon="' . $item['icon'] . '" :link="backpack_url(\'' . $item['url'] . '\')" />' . "\n";
+ }
+ if ($can) {
+ $res .= str_repeat("\t", $level) . '@endcan' . "\n";
+ }
+
+ return $res;
+ }
+
+ protected function getCan($item)
+ {
+ $can = [];
+ if (isset($item['can'])) {
+ if (is_string($item['can'])) {
+ $can = [$item['can']];
+ } else {
+ $can = $item['can'];
+ }
+ } else if (isset($item['items'])) {
+ foreach ($item['items'] as $i) {
+ if (isset($i['can'])) {
+ if (is_string($i['can'])) {
+ $can[] = $i['can'];
+ } else {
+ $can = array_merge($can, $i['can']);
+ }
+ }
+ }
+ }
+ if (empty($can)) {
+ return false;
+ } else if (count($can) > 1) {
+ return "@canany('" . json_encode($can) . "')";
+ } else {
+ return "@can('" . $can[0] . "')";
+ }
+ }
+}