1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Console\Commands; |
4
|
|
|
|
5
|
|
|
use File; |
6
|
|
|
use Illuminate\Console\Command; |
7
|
|
|
use Str; |
8
|
|
|
use Symfony\Component\Process\Process; |
9
|
|
|
|
10
|
|
|
class RequireDevTools extends Command |
11
|
|
|
{ |
12
|
|
|
use \Backpack\CRUD\app\Console\Commands\Traits\PrettyCommandOutput; |
13
|
|
|
|
14
|
|
|
protected $progressBar; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The name and signature of the console command. |
18
|
|
|
* |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $signature = 'backpack:require:devtools |
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 = 'Require Backpack DevTools on dev.'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Execute the console command. |
33
|
|
|
* |
34
|
|
|
* @return mixed Command-line output |
35
|
|
|
*/ |
36
|
|
|
public function handle() |
37
|
|
|
{ |
38
|
|
|
$this->progressBar = $this->output->createProgressBar(45); |
39
|
|
|
$this->progressBar->minSecondsBetweenRedraws(0); |
40
|
|
|
$this->progressBar->maxSecondsBetweenRedraws(120); |
41
|
|
|
$this->progressBar->setRedrawFrequency(1); |
42
|
|
|
$this->progressBar->start(); |
43
|
|
|
|
44
|
|
|
$this->info(' Requiring DevTools. Please wait...'); |
45
|
|
|
$this->progressBar->advance(); |
46
|
|
|
|
47
|
|
|
// Check if repositories exists |
48
|
|
|
$this->composerRepositories(); |
49
|
|
|
|
50
|
|
|
// Check for authentication |
51
|
|
|
$this->checkForAuthentication(); |
52
|
|
|
|
53
|
|
|
// Require DevTools |
54
|
|
|
$this->requireDevTools(); |
55
|
|
|
|
56
|
|
|
// Check if devtools is installed |
57
|
|
|
$tries = 2; |
58
|
|
|
while (! file_exists('vendor/backpack/devtools/src/Console/Commands/InstallDevTools.php')) { |
59
|
|
|
// Abort at nth try |
60
|
|
|
if (--$tries === 0) { |
61
|
|
|
return $this->error(' DevTools was not installed.'); |
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
// Restart progress bar |
65
|
|
|
$this->error('Please make sure your access token is correct.'); |
66
|
|
|
$this->progressBar->start(); |
67
|
|
|
|
68
|
|
|
// Clear authentication |
69
|
|
|
if (File::exists('auth.json')) { |
70
|
|
|
$auth = json_decode(File::get('auth.json')); |
71
|
|
|
if ($auth->{'http-basic'}->{'backpackforlaravel.com'} ?? false) { |
72
|
|
|
unset($auth->{'http-basic'}->{'backpackforlaravel.com'}); |
73
|
|
|
|
74
|
|
|
File::put('auth.json', json_encode($auth, JSON_PRETTY_PRINT)); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
// Check for authentication |
79
|
|
|
$this->checkForAuthentication(); |
80
|
|
|
|
81
|
|
|
// Require DevTools |
82
|
|
|
$this->requireDevTools(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
// Finish |
86
|
|
|
$this->progressBar->finish(); |
87
|
|
|
$this->info(' DevTools is now required.'); |
88
|
|
|
|
89
|
|
|
// DevTools inside installer |
90
|
|
|
$this->info(''); |
91
|
|
|
$this->info(' Now running the DevTools installation command.'); |
92
|
|
|
|
93
|
|
|
// manually include the command in the run-time |
94
|
|
|
if (! class_exists(\Backpack\DevTools\Console\Commands\InstallDevTools::class)) { |
|
|
|
|
95
|
|
|
include base_path('vendor/backpack/devtools/src/Console/Commands/InstallDevTools.php'); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$this->call(\Backpack\DevTools\Console\Commands\InstallDevTools::class); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function composerRepositories() |
102
|
|
|
{ |
103
|
|
|
$details = null; |
104
|
|
|
$process = new Process(['composer', 'config', 'repositories.backpack/devtools']); |
105
|
|
|
$process->run(function ($type, $buffer) use (&$details) { |
106
|
|
|
if ($type !== Process::ERR && $buffer !== '') { |
107
|
|
|
$details = json_decode($buffer); |
108
|
|
|
} else { |
109
|
|
|
$details = json_decode(File::get('composer.json'), true)['repositories']['backpack/devtools'] ?? false; |
110
|
|
|
} |
111
|
|
|
$this->progressBar->advance(); |
112
|
|
|
}); |
113
|
|
|
|
114
|
|
|
// Create repositories |
115
|
|
|
if (! $details) { |
116
|
|
|
$this->info(' Creating repositories entry in composer.json'); |
117
|
|
|
|
118
|
|
|
$process = new Process(['composer', 'config', 'repositories.backpack/devtools', 'composer', 'https://repo.backpackforlaravel.com']); |
119
|
|
|
$process->run(function ($type, $buffer) { |
|
|
|
|
120
|
|
|
if ($type === Process::ERR) { |
121
|
|
|
// Fallback |
122
|
|
|
$composerJson = Str::of(File::get('composer.json')); |
123
|
|
|
|
124
|
|
|
$currentRepositories = json_decode($composerJson, true)['repositories'] ?? false; |
125
|
|
|
|
126
|
|
|
$repositories = Str::of(json_encode([ |
127
|
|
|
'repositories' => [ |
128
|
|
|
'backpack/devtools' => [ |
129
|
|
|
'type' => 'composer', |
130
|
|
|
'url' => 'https://repo.backpackforlaravel.com', |
131
|
|
|
], |
132
|
|
|
], |
133
|
|
|
], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); |
134
|
|
|
|
135
|
|
|
$composerJson = $currentRepositories |
136
|
|
|
// Replace current repositories |
137
|
|
|
? preg_replace('/"repositories":\s{\r?\n/', $repositories->match("/\{\n\s+([\s\S]+)\n\s{4}\}/m")->append(",\n"), $composerJson) |
138
|
|
|
// Append to the end of the file |
139
|
|
|
: preg_replace("/\r?\n}/", $repositories->replaceFirst('{', ','), $composerJson); |
140
|
|
|
|
141
|
|
|
File::put('composer.json', $composerJson); |
142
|
|
|
} |
143
|
|
|
$this->progressBar->advance(); |
144
|
|
|
}); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function checkForAuthentication() |
149
|
|
|
{ |
150
|
|
|
// Check if auth exists |
151
|
|
|
$details = null; |
152
|
|
|
$process = new Process(['composer', 'config', 'http-basic.backpackforlaravel.com']); |
153
|
|
|
$process->run(function ($type, $buffer) use (&$details) { |
154
|
|
|
if ($type !== Process::ERR && $buffer !== '') { |
155
|
|
|
$details = json_decode($buffer); |
156
|
|
|
} elseif (File::exists('auth.json')) { |
157
|
|
|
$details = json_decode(File::get('auth.json'), true)['http-basic']['backpackforlaravel.com'] ?? false; |
158
|
|
|
} |
159
|
|
|
$this->progressBar->advance(); |
160
|
|
|
}); |
161
|
|
|
|
162
|
|
|
// Create an auth.json file |
163
|
|
|
if (! $details) { |
164
|
|
|
$this->info(' Creating auth.json file with your authentication token'); |
165
|
|
|
|
166
|
|
|
$this->line(' (Find your access token details on https://backpackforlaravel.com/user/tokens)'); |
167
|
|
|
$username = $this->ask('Access token username'); |
168
|
|
|
$password = $this->ask('Access token password'); |
169
|
|
|
|
170
|
|
|
$process = new Process(['composer', 'config', 'http-basic.backpackforlaravel.com', $username, $password]); |
171
|
|
|
$process->run(function ($type, $buffer) use ($username, $password) { |
|
|
|
|
172
|
|
|
if ($type === Process::ERR) { |
173
|
|
|
// Fallback |
174
|
|
|
$authFile = [ |
175
|
|
|
'http-basic' => [ |
176
|
|
|
'backpackforlaravel.com' => [ |
177
|
|
|
'username' => $username, |
178
|
|
|
'password' => $password, |
179
|
|
|
], |
180
|
|
|
], |
181
|
|
|
]; |
182
|
|
|
|
183
|
|
|
if (File::exists('auth.json')) { |
184
|
|
|
$currentFile = json_decode(File::get('auth.json'), true); |
185
|
|
|
if (! ($currentFile['http-basic']['backpackforlaravel.com'] ?? false)) { |
186
|
|
|
$authFile = array_merge_recursive($authFile, $currentFile); |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
File::put('auth.json', json_encode($authFile, JSON_PRETTY_PRINT)); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
$this->progressBar->advance(); |
194
|
|
|
}); |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
public function requireDevTools() |
199
|
|
|
{ |
200
|
|
|
// Require package |
201
|
|
|
$process = new Process(['composer', 'require', '--dev', '--with-all-dependencies', 'backpack/devtools']); |
202
|
|
|
$process->setTimeout(300); |
203
|
|
|
$process->run(function ($type, $buffer) { |
|
|
|
|
204
|
|
|
$this->progressBar->advance(); |
205
|
|
|
}); |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.