/
InstallsRoadRunnerDependencies.php
178 lines (145 loc) · 5.15 KB
/
InstallsRoadRunnerDependencies.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
namespace Laravel\Octane\Commands\Concerns;
use Illuminate\Support\Str;
use Laravel\Octane\RoadRunner\Concerns\FindsRoadRunnerBinary;
use RuntimeException;
use Spiral\RoadRunner\Http\PSR7Worker;
use Symfony\Component\Process\Exception\ProcessSignaledException;
use Symfony\Component\Process\ExecutableFinder;
use Symfony\Component\Process\PhpExecutableFinder;
use Symfony\Component\Process\Process;
use Throwable;
trait InstallsRoadRunnerDependencies
{
use FindsRoadRunnerBinary;
/**
* The minimum required version of the RoadRunner binary.
*
* @var string
*/
protected $requiredVersion = '2.8.2';
/**
* Determine if RoadRunner is installed.
*
* @return bool
*/
protected function isRoadRunnerInstalled()
{
return class_exists(PSR7Worker::class);
}
/**
* Ensure the RoadRunner package is installed into the project.
*
* @return bool
*/
protected function ensureRoadRunnerPackageIsInstalled()
{
if ($this->isRoadRunnerInstalled()) {
return true;
}
if (! $this->confirm('Octane requires "spiral/roadrunner:^2.8.2". Do you wish to install it as a dependency?')) {
$this->error('Octane requires "spiral/roadrunner".');
return false;
}
$command = $this->findComposer().' require spiral/roadrunner:^2.8.2 --with-all-dependencies';
$process = Process::fromShellCommandline($command, null, null, null, null);
if ('\\' !== DIRECTORY_SEPARATOR && file_exists('/dev/tty') && is_readable('/dev/tty')) {
try {
$process->setTty(true);
} catch (RuntimeException $e) {
$this->output->writeln('Warning: '.$e->getMessage());
}
}
try {
$process->run(function ($type, $line) {
$this->output->write($line);
});
} catch (ProcessSignaledException $e) {
if (extension_loaded('pcntl') && $e->getSignal() !== SIGINT) {
throw $e;
}
}
return true;
}
/**
* Get the composer command for the environment.
*
* @return string
*/
protected function findComposer()
{
$composerPath = getcwd().'/composer.phar';
$phpPath = (new PhpExecutableFinder)->find();
if (! file_exists($composerPath)) {
$composerPath = (new ExecutableFinder())->find('composer');
}
return '"'.$phpPath.'" '.$composerPath;
}
/**
* Ensure the RoadRunner binary is installed into the project.
*
* @return string
*/
protected function ensureRoadRunnerBinaryIsInstalled(): string
{
if (! is_null($roadRunnerBinary = $this->findRoadRunnerBinary())) {
return $roadRunnerBinary;
}
if ($this->confirm('Unable to locate RoadRunner binary. Should Octane download the binary for your operating system?', true)) {
$this->downloadRoadRunnerBinary();
copy(__DIR__.'/../stubs/rr.yaml', base_path('.rr.yaml'));
}
return base_path('rr');
}
/**
* Ensure the RoadRunner binary installed in your project meets Octane requirements.
*
* @param string $roadRunnerBinary
* @return void
*/
protected function ensureRoadRunnerBinaryMeetsRequirements($roadRunnerBinary)
{
$version = tap(new Process([$roadRunnerBinary, '--version'], base_path()))
->run()
->getOutput();
if (! Str::startsWith($version, 'rr version 2.')) {
return $this->warn(
'Unable to determine the current RoadRunner binary version. Please report this issue: https://github.com/laravel/octane/issues/new.'
);
}
$version = explode(' ', $version)[2];
if (version_compare($version, $this->requiredVersion, '<')) {
$this->warn("Your RoadRunner binary version (<fg=red>$version</>) may be incompatible with Octane.");
if ($this->confirm('Should Octane download the latest RoadRunner binary version for your operating system?', true)) {
rename($roadRunnerBinary, "$roadRunnerBinary.backup");
try {
$this->downloadRoadRunnerBinary();
} catch (Throwable $e) {
report($e);
rename("$roadRunnerBinary.backup", $roadRunnerBinary);
return $this->warn('Unable to download RoadRunner binary. The HTTP request exception has been logged.');
}
unlink("$roadRunnerBinary.backup");
}
}
}
/**
* Download the latest version of the RoadRunner binary.
*
* @return void
*/
protected function downloadRoadRunnerBinary()
{
tap(new Process(array_filter([
(new PhpExecutableFinder)->find(),
'./vendor/bin/rr',
'get-binary',
'-n',
'--ansi',
]), base_path(), null, null, null))->mustRun(
fn ($type, $buffer) => $this->output->write($buffer)
);
chmod(base_path('rr'), 0755);
$this->line('');
}
}