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