|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Console\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
|
6
|
|
|
use Illuminate\Support\Facades\Storage; |
|
7
|
|
|
|
|
8
|
|
|
class AddMenuContent extends Command |
|
9
|
|
|
{ |
|
10
|
|
|
use \Backpack\CRUD\app\Console\Commands\Traits\PrettyCommandOutput; |
|
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* The name and signature of the console command. |
|
14
|
|
|
* |
|
15
|
|
|
* @var string |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $signature = 'backpack:add-menu-content |
|
18
|
|
|
{code : HTML/PHP code that shows menu items. Use either single quotes or double quotes. Never both. }'; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* The console command description. |
|
22
|
|
|
* |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $description = 'Add HTML/PHP code to the Backpack menu_items file'; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Create a new command instance. |
|
29
|
|
|
* |
|
30
|
|
|
* @return void |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct() |
|
33
|
|
|
{ |
|
34
|
|
|
parent::__construct(); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Execute the console command. |
|
39
|
|
|
* |
|
40
|
|
|
* @return mixed |
|
41
|
|
|
*/ |
|
42
|
|
|
public function handle() |
|
43
|
|
|
{ |
|
44
|
|
|
$path = 'resources/views/vendor/backpack/ui/inc/menu_items.blade.php'; |
|
45
|
|
|
$disk_name = config('backpack.base.root_disk_name'); |
|
46
|
|
|
$disk = Storage::disk($disk_name); |
|
47
|
|
|
$code = $this->argument('code'); |
|
48
|
|
|
|
|
49
|
|
|
$this->progressBlock("Adding menu entry to <fg=blue>$path</>"); |
|
50
|
|
|
|
|
51
|
|
|
// Validate file exists |
|
52
|
|
|
if (! $disk->exists($path)) { |
|
53
|
|
|
$this->errorProgressBlock(); |
|
54
|
|
|
$this->note('The menu_items file does not exist. Make sure Backpack is properly installed.', 'red'); |
|
55
|
|
|
|
|
56
|
|
|
return; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$contents = $disk->get($path); |
|
60
|
|
|
$file_lines = file($disk->path($path), FILE_IGNORE_NEW_LINES); |
|
61
|
|
|
|
|
62
|
|
|
// Validate the entry already exists |
|
63
|
|
|
if ($this->getLastLineNumberThatContains($code, $file_lines)) { |
|
|
|
|
|
|
64
|
|
|
$this->closeProgressBlock('Already existed', 'yellow'); |
|
65
|
|
|
|
|
66
|
|
|
return; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
if (! $disk->put($path, $contents.PHP_EOL.$code)) { |
|
70
|
|
|
$this->errorProgressBlock(); |
|
71
|
|
|
$this->note('Could not write to menu_items file.', 'red'); |
|
72
|
|
|
|
|
73
|
|
|
return; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$this->closeProgressBlock(); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Parse the given file stream and return the line number where a string is found. |
|
81
|
|
|
* |
|
82
|
|
|
* @param string $needle The string that's being searched for. |
|
83
|
|
|
* @param array $haystack The file where the search is being performed. |
|
84
|
|
|
* @return bool|int The last line number where the string was found. Or false. |
|
85
|
|
|
*/ |
|
86
|
|
|
private function getLastLineNumberThatContains($needle, $haystack) |
|
87
|
|
|
{ |
|
88
|
|
|
$matchingLines = array_filter($haystack, function ($k) use ($needle) { |
|
89
|
|
|
return strpos($k, $needle) !== false; |
|
90
|
|
|
}); |
|
91
|
|
|
|
|
92
|
|
|
if ($matchingLines) { |
|
|
|
|
|
|
93
|
|
|
return array_key_last($matchingLines); |
|
|
|
|
|
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return false; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|