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