|
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 = Storage::disk('root'); |
|
46
|
|
|
$code = $this->argument('code'); |
|
47
|
|
|
|
|
48
|
|
|
if ($disk->exists($path)) { |
|
49
|
|
|
$old_file_content = Storage::disk('root')->get($path); |
|
50
|
|
|
|
|
51
|
|
|
// insert the given code before the file's last line |
|
52
|
|
|
$file_lines = explode(PHP_EOL, $old_file_content); |
|
53
|
|
|
$number_of_lines = count($file_lines); |
|
54
|
|
|
$file_lines[$number_of_lines] = $file_lines[$number_of_lines - 1]; |
|
55
|
|
|
$file_lines[$number_of_lines - 1] = ' '.$code; |
|
56
|
|
|
$new_file_content = implode(PHP_EOL, $file_lines); |
|
57
|
|
|
|
|
58
|
|
|
if ($disk->put($path, $new_file_content)) { |
|
59
|
|
|
$this->info('Successfully added code to '.$path); |
|
60
|
|
|
} else { |
|
61
|
|
|
$this->error('Could not write to file: '.$path); |
|
62
|
|
|
} |
|
63
|
|
|
} else { |
|
64
|
|
|
$command = 'php artisan vendor:publish --provider="Backpack\Base\BaseServiceProvider" --tag=custom_routes'; |
|
65
|
|
|
|
|
66
|
|
|
$process = new Process($command, null, null, null, 300, null); |
|
67
|
|
|
|
|
68
|
|
|
$process->run(function ($type, $buffer) { |
|
69
|
|
|
if (Process::ERR === $type) { |
|
70
|
|
|
$this->line($buffer); |
|
71
|
|
|
} else { |
|
72
|
|
|
$this->line($buffer); |
|
73
|
|
|
} |
|
74
|
|
|
}); |
|
75
|
|
|
|
|
76
|
|
|
// executes after the command finishes |
|
77
|
|
|
if (!$process->isSuccessful()) { |
|
78
|
|
|
throw new ProcessFailedException($process); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$this->handle(); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
Adding a
@returnannotation 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.