1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\Base\app\Console\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
use Illuminate\Support\Facades\Storage; |
7
|
|
|
use Symfony\Component\Process\Exception\ProcessFailedException; |
8
|
|
|
use Symfony\Component\Process\Process; |
9
|
|
|
|
10
|
|
|
class AddCustomRouteContent extends Command |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* The name and signature of the console command. |
14
|
|
|
* |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected $signature = 'backpack:base:add-custom-route |
18
|
|
|
{code : HTML/PHP code that registers a route. 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 routes/backpack/custom.php 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 = 'routes/backpack/custom.php'; |
45
|
|
|
$disk_name = config('backpack.base.root_disk_name'); |
46
|
|
|
$disk = Storage::disk($disk_name); |
47
|
|
|
$code = $this->argument('code'); |
48
|
|
|
|
49
|
|
|
if ($disk->exists($path)) { |
50
|
|
|
$old_file_path = $disk->path($path); |
51
|
|
|
|
52
|
|
|
// insert the given code before the file's last line |
53
|
|
|
$file_lines = file($old_file_path, FILE_IGNORE_NEW_LINES); |
54
|
|
|
$end_line_number = $this->customRoutesFileEndLine($file_lines); |
55
|
|
|
$file_lines[$end_line_number + 1] = $file_lines[$end_line_number]; |
56
|
|
|
$file_lines[$end_line_number] = ' '.$code; |
57
|
|
|
$new_file_content = implode(PHP_EOL, $file_lines); |
58
|
|
|
|
59
|
|
|
if ($disk->put($path, $new_file_content)) { |
60
|
|
|
$this->info('Successfully added code to '.$path); |
61
|
|
|
} else { |
62
|
|
|
$this->error('Could not write to file: '.$path); |
63
|
|
|
} |
64
|
|
|
} else { |
65
|
|
|
$command = 'php artisan vendor:publish --provider="Backpack\Base\BaseServiceProvider" --tag=custom_routes'; |
66
|
|
|
|
67
|
|
|
$process = new Process($command, null, null, null, 300, null); |
|
|
|
|
68
|
|
|
|
69
|
|
|
$process->run(function ($type, $buffer) { |
70
|
|
|
if (Process::ERR === $type) { |
71
|
|
|
$this->line($buffer); |
72
|
|
|
} else { |
73
|
|
|
$this->line($buffer); |
74
|
|
|
} |
75
|
|
|
}); |
76
|
|
|
|
77
|
|
|
// executes after the command finishes |
78
|
|
|
if (!$process->isSuccessful()) { |
79
|
|
|
throw new ProcessFailedException($process); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$this->handle(); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
private function customRoutesFileEndLine($file_lines) |
87
|
|
|
{ |
88
|
|
|
// in case the last line has not been modified at all |
89
|
|
|
$end_line_number = array_search('}); // this should be the absolute last line of this file', $file_lines); |
90
|
|
|
|
91
|
|
|
if ($end_line_number) { |
92
|
|
|
return $end_line_number; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
// otherwise, in case the last line HAS been modified |
96
|
|
|
// return the last line that has an ending in it |
97
|
|
|
$possible_end_lines = array_filter($file_lines, function ($k) { |
98
|
|
|
return strpos($k, '});') === 0; |
99
|
|
|
}); |
100
|
|
|
|
101
|
|
|
if ($possible_end_lines) { |
|
|
|
|
102
|
|
|
end($possible_end_lines); |
103
|
|
|
$end_line_number = key($possible_end_lines); |
104
|
|
|
|
105
|
|
|
return $end_line_number; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.