1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Console\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
use Symfony\Component\Process\Exception\ProcessFailedException; |
7
|
|
|
use Symfony\Component\Process\Process; |
8
|
|
|
use Artisan; |
9
|
|
|
|
10
|
|
|
class Install extends Command |
11
|
|
|
{ |
12
|
|
|
protected $progressBar; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* The name and signature of the console command. |
16
|
|
|
* |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $signature = 'backpack:install |
20
|
|
|
{--elfinder=ask : Should it install the File Manager. } |
21
|
|
|
{--timeout=300} : How many seconds to allow each process to run. |
22
|
|
|
{--debug} : Show process output or not. Useful for debugging.'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The console command description. |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $description = 'Install Backpack requirements on dev, publish files and create uploads directory.'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Execute the console command. |
33
|
|
|
* |
34
|
|
|
* @return mixed Command-line output |
|
|
|
|
35
|
|
|
*/ |
36
|
|
|
public function handle() |
37
|
|
|
{ |
38
|
|
|
/* |
39
|
|
|
* "ask" comes by default, when no option is provided, like: "backpack:install" |
40
|
|
|
* https://laravel.com/docs/6.0/artisan#options |
41
|
|
|
*/ |
42
|
|
|
$shouldInstallElfinder = false; |
|
|
|
|
43
|
|
|
|
44
|
|
|
if ($this->option('elfinder') == 'ask') { |
45
|
|
|
$shouldInstallElfinder = $this->confirm('Install barryvdh/laravel-elfinder to provide an admin interface for File Management?', false); |
46
|
|
|
} elseif ($this->option('elfinder') == 'no') { |
47
|
|
|
$shouldInstallElfinder = false; |
48
|
|
|
} elseif ($this->option('elfinder') == 'yes') { |
49
|
|
|
$shouldInstallElfinder = true; |
50
|
|
|
} else { |
51
|
|
|
$this->error('Option not recognized: '.$this->option('elfinder')); |
52
|
|
|
|
53
|
|
|
return false; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$this->progressBar = $this->output->createProgressBar($shouldInstallElfinder ? 11 : 6); |
57
|
|
|
$this->progressBar->minSecondsBetweenRedraws(0); |
58
|
|
|
$this->progressBar->maxSecondsBetweenRedraws(120); |
59
|
|
|
$this->progressBar->setRedrawFrequency(1); |
60
|
|
|
|
61
|
|
|
$this->progressBar->start(); |
62
|
|
|
|
63
|
|
|
$this->info(' Backpack installation started. Please wait...'); |
64
|
|
|
$this->progressBar->advance(); |
65
|
|
|
|
66
|
|
|
$this->line(' Publishing configs, langs, views, js and css files'); |
67
|
|
|
$this->executeArtisanProcess('vendor:publish', [ |
68
|
|
|
'--provider' => 'Backpack\CRUD\BackpackServiceProvider', |
69
|
|
|
'--tag' => 'minimum' |
70
|
|
|
]); |
71
|
|
|
|
72
|
|
|
$this->line(' Publishing config for notifications - prologue/alerts'); |
73
|
|
|
$this->executeArtisanProcess('vendor:publish', [ |
74
|
|
|
'--provider' => 'Prologue\Alerts\AlertsServiceProvider' |
75
|
|
|
]); |
76
|
|
|
|
77
|
|
|
$this->line(" Generating users table (using Laravel's default migrations)"); |
78
|
|
|
$this->executeArtisanProcess('migrate'); |
79
|
|
|
|
80
|
|
|
$this->line(" Creating App\Models\BackpackUser.php"); |
81
|
|
|
$this->executeArtisanProcess('backpack:publish-user-model'); |
82
|
|
|
|
83
|
|
|
$this->line(" Creating App\Http\Middleware\CheckIfAdmin.php"); |
84
|
|
|
$this->executeArtisanProcess('backpack:publish-middleware'); |
85
|
|
|
|
86
|
|
|
// elFinder steps |
87
|
|
|
if ($shouldInstallElfinder) { |
88
|
|
|
$this->line(' Installing barryvdh/laravel-elfinder'); |
89
|
|
|
$this->executeProcess(['composer', 'require', 'barryvdh/laravel-elfinder']); |
|
|
|
|
90
|
|
|
|
91
|
|
|
$this->line(' Creating uploads directory'); |
92
|
|
|
switch (DIRECTORY_SEPARATOR) { |
93
|
|
|
case '/': // unix |
94
|
|
|
$createUploadDirectoryCommand = ['mkdir', '-p', 'public/uploads']; |
95
|
|
|
break; |
96
|
|
|
case '\\': // windows |
97
|
|
|
if (! file_exists('public\uploads')) { |
98
|
|
|
$createUploadDirectoryCommand = ['mkdir', 'public\uploads']; |
99
|
|
|
} |
100
|
|
|
break; |
101
|
|
|
} |
102
|
|
|
if (isset($createUploadDirectoryCommand)) { |
103
|
|
|
$this->executeProcess($createUploadDirectoryCommand); |
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$this->line(' Publishing elFinder assets'); |
107
|
|
|
$this->executeArtisanProcess('elfinder:publish'); |
108
|
|
|
|
109
|
|
|
$this->line(' Publishing custom elfinder views'); |
110
|
|
|
$this->executeArtisanProcess('vendor:publish', [ |
111
|
|
|
'--provider' => 'Backpack\CRUD\BackpackServiceProvider', |
112
|
|
|
'--tag' => 'elfinder' |
113
|
|
|
]); |
114
|
|
|
|
115
|
|
|
$this->line(' Adding sidebar menu item for File Manager'); |
116
|
|
|
switch (DIRECTORY_SEPARATOR) { |
117
|
|
|
case '/': // unix |
118
|
|
|
$this->executeArtisanProcess('backpack:add-sidebar-content', [ |
119
|
|
|
'code' => '<li class="nav-item"><a class="nav-link" href="{{ backpack_url(\'elfinder\') }}\"><i class="nav-icon fa fa-files-o"></i> <span>{{ trans(\'backpack::crud.file_manager\') }}</span></a></li>']); |
120
|
|
|
break; |
121
|
|
|
case '\\': // windows |
122
|
|
|
$this->executeArtisanProcess('backpack:add-sidebar-content', [ |
123
|
|
|
'code' => '<li class="nav-item"><a class="nav-link" href=""{{ backpack_url(\'elfinder\') }}""><i class=""nav-icon fa fa-files-o""></i> <span>{{ trans(\'backpack::crud.file_manager\') }}</span></a></li>']); |
124
|
|
|
break; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
// end of elFinder steps |
128
|
|
|
|
129
|
|
|
$this->progressBar->finish(); |
130
|
|
|
$this->info(' Backpack installation finished.'); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Run a SSH command. |
135
|
|
|
* |
136
|
|
|
* @param string $command The SSH command that needs to be run |
137
|
|
|
* @param bool $beforeNotice Information for the user before the command is run |
138
|
|
|
* @param bool $afterNotice Information for the user after the command is run |
139
|
|
|
* |
140
|
|
|
* @return mixed Command-line output |
141
|
|
|
*/ |
142
|
|
|
public function executeProcess($command, $beforeNotice = false, $afterNotice = false) |
143
|
|
|
{ |
144
|
|
|
$this->echo('info', $beforeNotice ? ' '.$beforeNotice : $command); |
145
|
|
|
|
146
|
|
|
// make sure the command is an array as per Symphony 4.3+ requirement |
147
|
|
|
$command = is_string($command) ? explode(' ', $command) : $command; |
148
|
|
|
|
149
|
|
|
$process = new Process($command, null, null, null, $this->option('timeout')); |
150
|
|
|
$process->run(function ($type, $buffer) { |
151
|
|
|
if (Process::ERR === $type) { |
152
|
|
|
$this->echo('comment', $buffer); |
153
|
|
|
} else { |
154
|
|
|
$this->echo('line', $buffer); |
155
|
|
|
} |
156
|
|
|
}); |
157
|
|
|
|
158
|
|
|
// executes after the command finishes |
159
|
|
|
if (! $process->isSuccessful()) { |
160
|
|
|
throw new ProcessFailedException($process); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
if ($this->progressBar) { |
164
|
|
|
$this->progressBar->advance(); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
if ($afterNotice) { |
168
|
|
|
$this->echo('info', $afterNotice); |
|
|
|
|
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Run an artisan command. |
174
|
|
|
* |
175
|
|
|
* @param string $command The artisan command to be run. |
176
|
|
|
* @param array $arguments Key-value array of arguments to the artisan command. |
177
|
|
|
* @param bool $beforeNotice Information for the user before the command is run |
178
|
|
|
* @param bool $afterNotice Information for the user after the command is run |
179
|
|
|
* |
180
|
|
|
* @return mixed Command-line output |
181
|
|
|
*/ |
182
|
|
|
public function executeArtisanProcess($command, $arguments = [], $beforeNotice = false, $afterNotice = false) |
183
|
|
|
{ |
184
|
|
|
$beforeNotice = $beforeNotice ? ' '.$beforeNotice : 'php artisan '.$command.' '.implode(' ', $arguments); |
185
|
|
|
|
186
|
|
|
$this->echo('info', $beforeNotice); |
187
|
|
|
|
188
|
|
|
try { |
189
|
|
|
Artisan::call($command, $arguments); |
190
|
|
|
} catch (Exception $e) { |
|
|
|
|
191
|
|
|
throw new ProcessFailedException($e); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
if ($this->progressBar) { |
195
|
|
|
$this->progressBar->advance(); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
if ($afterNotice) { |
199
|
|
|
$this->echo('info', $afterNotice); |
|
|
|
|
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Write text to the screen for the user to see. |
205
|
|
|
* |
206
|
|
|
* @param string $type line, info, comment, question, error |
207
|
|
|
* @param string $content |
208
|
|
|
*/ |
209
|
|
|
public function echo($type, $content) |
210
|
|
|
{ |
211
|
|
|
if ($this->option('debug') == false) { |
212
|
|
|
return; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
// skip empty lines |
216
|
|
|
if (trim($content)) { |
217
|
|
|
$this->{$type}($content); |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.