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 AddCustomRouteContent extends Command |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* The name and signature of the console command. |
12
|
|
|
* |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
protected $signature = 'backpack:add-custom-route |
16
|
|
|
{code : HTML/PHP code that registers a route. Use either single quotes or double quotes. Never both. }'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The console command description. |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $description = 'Add HTML/PHP code to the routes/backpack/custom.php file'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Create a new command instance. |
27
|
|
|
* |
28
|
|
|
* @return void |
29
|
|
|
*/ |
30
|
|
|
public function __construct() |
31
|
|
|
{ |
32
|
|
|
parent::__construct(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Execute the console command. |
37
|
|
|
* |
38
|
|
|
* @return mixed |
39
|
|
|
*/ |
40
|
|
|
public function handle() |
41
|
|
|
{ |
42
|
|
|
$path = 'routes/backpack/custom.php'; |
43
|
|
|
$disk_name = config('backpack.base.root_disk_name'); |
44
|
|
|
$disk = Storage::disk($disk_name); |
45
|
|
|
$code = $this->argument('code'); |
46
|
|
|
|
47
|
|
|
if ($disk->exists($path)) { |
48
|
|
|
$old_file_path = $disk->path($path); |
49
|
|
|
|
50
|
|
|
// insert the given code before the file's last line |
51
|
|
|
$file_lines = file($old_file_path, FILE_IGNORE_NEW_LINES); |
52
|
|
|
|
53
|
|
|
// if the code already exists in the file, abort |
54
|
|
|
if ($this->getLastLineNumberThatContains($code, $file_lines)) { |
|
|
|
|
55
|
|
|
return $this->comment('Route already existed.'); |
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$end_line_number = $this->customRoutesFileEndLine($file_lines); |
59
|
|
|
$file_lines[$end_line_number + 1] = $file_lines[$end_line_number]; |
60
|
|
|
$file_lines[$end_line_number] = ' '.$code; |
|
|
|
|
61
|
|
|
$new_file_content = implode(PHP_EOL, $file_lines); |
62
|
|
|
|
63
|
|
|
if ($disk->put($path, $new_file_content)) { |
64
|
|
|
$this->info('Successfully added code to '.$path); |
65
|
|
|
} else { |
66
|
|
|
$this->error('Could not write to file: '.$path); |
67
|
|
|
} |
68
|
|
|
} else { |
69
|
|
|
Artisan::call('vendor:publish', ['--provider' => 'Backpack\CRUD\BackpackServiceProvider', '--tag' => 'custom_routes']); |
|
|
|
|
70
|
|
|
|
71
|
|
|
$this->handle(); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
private function customRoutesFileEndLine($file_lines) |
76
|
|
|
{ |
77
|
|
|
// in case the last line has not been modified at all |
78
|
|
|
$end_line_number = array_search('}); // this should be the absolute last line of this file', $file_lines); |
79
|
|
|
|
80
|
|
|
if ($end_line_number) { |
81
|
|
|
return $end_line_number; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
// otherwise, in case the last line HAS been modified |
85
|
|
|
// return the last line that has an ending in it |
86
|
|
|
$possible_end_lines = array_filter($file_lines, function ($k) { |
87
|
|
|
return strpos($k, '});') === 0; |
88
|
|
|
}); |
89
|
|
|
|
90
|
|
|
if ($possible_end_lines) { |
|
|
|
|
91
|
|
|
end($possible_end_lines); |
92
|
|
|
$end_line_number = key($possible_end_lines); |
93
|
|
|
|
94
|
|
|
return $end_line_number; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Parse the given file stream and return the line number where a string is found. |
100
|
|
|
* |
101
|
|
|
* @param string $needle The string that's being searched for. |
102
|
|
|
* @param array $haystack The file where the search is being performed. |
103
|
|
|
* @return bool|int The last line number where the string was found. Or false. |
104
|
|
|
*/ |
105
|
|
|
private function getLastLineNumberThatContains($needle, $haystack) |
106
|
|
|
{ |
107
|
|
|
$matchingLines = array_filter($haystack, function ($k) use ($needle) { |
108
|
|
|
return strpos($k, $needle) !== false; |
109
|
|
|
}); |
110
|
|
|
|
111
|
|
|
if ($matchingLines) { |
|
|
|
|
112
|
|
|
return array_key_last($matchingLines); |
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return false; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|