1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace FondBot\Console; |
6
|
|
|
|
7
|
|
|
use GuzzleHttp\Client; |
8
|
|
|
use FondBot\Foundation\Kernel; |
9
|
|
|
use Illuminate\Console\Command; |
10
|
|
|
use Symfony\Component\Process\Process; |
11
|
|
|
use Illuminate\Contracts\Filesystem\Filesystem; |
12
|
|
|
|
13
|
|
|
class InstallDriver extends Command |
14
|
|
|
{ |
15
|
|
|
protected $signature = 'driver:install |
16
|
|
|
{name : Driver name to be installed}'; |
17
|
|
|
|
18
|
|
|
public function handle(Client $http, Filesystem $filesystem): void |
19
|
|
|
{ |
20
|
|
|
$response = $http->request('GET', 'https://fondbot.com/api/drivers', [ |
21
|
|
|
'form_params' => ['version' => Kernel::VERSION], |
22
|
|
|
]); |
23
|
|
|
|
24
|
|
|
$items = json_decode((string) $response->getBody(), true); |
25
|
|
|
|
26
|
|
|
// Check if package is listed in store |
27
|
|
|
$name = $this->argument('name'); |
28
|
|
|
$drivers = collect($items); |
29
|
|
|
|
30
|
|
|
$driver = $drivers->first(function ($item) use ($name) { |
31
|
|
|
return $item['name'] === $name; |
32
|
|
|
}); |
33
|
|
|
|
34
|
|
|
if ($driver === null) { |
35
|
|
|
$this->error('"'.$name.'" is not found in the official drivers list.'); |
36
|
|
|
$package = $this->ask('Type composer package name if you know which one you want to install'); |
37
|
|
|
} else { |
38
|
|
|
// If driver is not official we should ask user to confirm installation |
39
|
|
|
if ($driver['official'] !== true) { |
40
|
|
|
if (!$this->confirm('"'.$name.'" is not official. Still want to install?')) { |
41
|
|
|
return; |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$package = $driver['package']; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
// Determine if package is already installed |
49
|
|
|
$composer = json_decode($filesystem->get('composer.json'), true); |
50
|
|
|
$installed = collect($composer['require']) |
51
|
|
|
->merge($composer['require-dev']) |
52
|
|
|
->search(function ($_, $item) use ($package) { |
53
|
|
|
return hash_equals($item, $package); |
54
|
|
|
}); |
55
|
|
|
|
56
|
|
|
if ($installed !== false) { |
57
|
|
|
$this->error('Driver is already installed.'); |
58
|
|
|
|
59
|
|
|
return; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
// Install driver |
63
|
|
|
$this->info('Installing driver...'); |
64
|
|
|
|
65
|
|
|
$process = new Process('composer require '.$package, path()); |
66
|
|
|
$output = ''; |
67
|
|
|
$result = $process->run(function ($_, $line) use (&$output) { |
68
|
|
|
$output .= $line; |
69
|
|
|
}); |
70
|
|
|
|
71
|
|
|
if ($result !== 0) { |
72
|
|
|
$this->error($output); |
73
|
|
|
$this->error('Installation failed.'); |
74
|
|
|
exit($result); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$this->info('Driver installed.'); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|