|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Laravel\Octane\Commands\Concerns; |
| 4 | + |
| 5 | +use GuzzleHttp\Client; |
| 6 | +use Illuminate\Support\Facades\Http; |
| 7 | +use Laravel\Octane\FrankenPhp\Concerns\FindsFrankenPhpBinary; |
| 8 | +use Symfony\Component\Process\Process; |
| 9 | +use Throwable; |
| 10 | + |
| 11 | +trait InstallsFrankenPhpDependencies |
| 12 | +{ |
| 13 | + use FindsFrankenPhpBinary; |
| 14 | + |
| 15 | + /** |
| 16 | + * The minimum required version of the FrankenPHP binary. |
| 17 | + * |
| 18 | + * @var string |
| 19 | + */ |
| 20 | + protected $requiredFrankenPhpVersion = '1.0.0'; |
| 21 | + |
| 22 | + /** |
| 23 | + * Ensure the FrankenPHP's Caddyfile and worker script are installed. |
| 24 | + * |
| 25 | + * @return void |
| 26 | + */ |
| 27 | + public function ensureFrankenPhpWorkerIsInstalled() |
| 28 | + { |
| 29 | + if (! file_exists(public_path('frankenphp-worker.php'))) { |
| 30 | + copy(__DIR__.'/../stubs/frankenphp-worker.php', public_path('frankenphp-worker.php')); |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Ensure the FrankenPHP binary is installed into the project. |
| 36 | + * |
| 37 | + * @return string |
| 38 | + */ |
| 39 | + protected function ensureFrankenPhpBinaryIsInstalled() |
| 40 | + { |
| 41 | + if (! is_null($frankenphpBinary = $this->findFrankenPhpBinary())) { |
| 42 | + return $frankenphpBinary; |
| 43 | + } |
| 44 | + |
| 45 | + if ($this->confirm('Unable to locate FrankenPHP binary. Should Octane download the binary for your operating system?', true)) { |
| 46 | + $this->downloadFrankenPhpBinary(); |
| 47 | + } |
| 48 | + |
| 49 | + return base_path('frankenphp'); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Download the latest version of the FrankenPHP binary. |
| 54 | + * |
| 55 | + * @return bool |
| 56 | + */ |
| 57 | + protected function downloadFrankenPhpBinary() |
| 58 | + { |
| 59 | + $arch = php_uname('m'); |
| 60 | + |
| 61 | + $assetName = match (true) { |
| 62 | + PHP_OS_FAMILY === 'Linux' && $arch === 'x86_64' => 'frankenphp-linux-x86_64', |
| 63 | + PHP_OS_FAMILY === 'Darwin' => "frankenphp-mac-$arch", |
| 64 | + default => null, |
| 65 | + }; |
| 66 | + |
| 67 | + if ($assetName === null) { |
| 68 | + $this->error('FrankenPHP binaries are currently only available for Linux (x86_64) and macOS. Other systems should use the Docker images or compile FrankenPHP manually.'); |
| 69 | + |
| 70 | + return false; |
| 71 | + } |
| 72 | + |
| 73 | + $assets = Http::accept('application/vnd.github+json') |
| 74 | + ->withHeaders(['X-GitHub-Api-Version' => '2022-11-28']) |
| 75 | + ->get('https://api.github.com/repos/dunglas/frankenphp/releases/latest')['assets']; |
| 76 | + |
| 77 | + foreach ($assets as $asset) { |
| 78 | + if ($asset['name'] !== $assetName) { |
| 79 | + continue; |
| 80 | + } |
| 81 | + |
| 82 | + $path = base_path('frankenphp'); |
| 83 | + |
| 84 | + $progressBar = null; |
| 85 | + |
| 86 | + (new Client)->get( |
| 87 | + $asset['browser_download_url'], |
| 88 | + [ |
| 89 | + 'sink' => $path, |
| 90 | + 'progress' => function ($downloadTotal, $downloadedBytes) use (&$progressBar) { |
| 91 | + if ($downloadTotal === 0) { |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + if ($progressBar === null) { |
| 96 | + $progressBar = $this->output->createProgressBar($downloadTotal); |
| 97 | + $progressBar->start($downloadTotal, $downloadedBytes); |
| 98 | + |
| 99 | + return; |
| 100 | + } |
| 101 | + |
| 102 | + $progressBar->setProgress($downloadedBytes); |
| 103 | + }, |
| 104 | + ] |
| 105 | + ); |
| 106 | + |
| 107 | + chmod($path, 0755); |
| 108 | + |
| 109 | + $progressBar->finish(); |
| 110 | + |
| 111 | + $this->newLine(); |
| 112 | + |
| 113 | + return $path; |
| 114 | + } |
| 115 | + |
| 116 | + $this->error('FrankenPHP asset not found.'); |
| 117 | + |
| 118 | + return $path; |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Ensure the installed FrankenPHP binary meets Octane's requirements. |
| 123 | + * |
| 124 | + * @param string $frakenPhpBinary |
| 125 | + * @return void |
| 126 | + */ |
| 127 | + protected function ensureFrankenPhpBinaryMeetsRequirements($frakenPhpBinary) |
| 128 | + { |
| 129 | + $version = tap(new Process([$frakenPhpBinary, '--version'], base_path())) |
| 130 | + ->run() |
| 131 | + ->getOutput(); |
| 132 | + |
| 133 | + $version = explode(' ', $version)[1] ?? null; |
| 134 | + |
| 135 | + if ($version === null) { |
| 136 | + return $this->warn( |
| 137 | + 'Unable to determine the current FrankenPHP binary version. Please report this issue: https://github.com/laravel/octane/issues/new.', |
| 138 | + ); |
| 139 | + } |
| 140 | + |
| 141 | + if (version_compare($version, $this->requiredFrankenPhpVersion, '>=')) { |
| 142 | + return; |
| 143 | + } |
| 144 | + |
| 145 | + $this->warn("Your FrankenPHP binary version (<fg=red>$version</>) may be incompatible with Octane."); |
| 146 | + |
| 147 | + if ($this->confirm('Should Octane download the latest FrankenPHP binary version for your operating system?', true)) { |
| 148 | + rename($frakenPhpBinary, "$frakenPhpBinary.backup"); |
| 149 | + |
| 150 | + try { |
| 151 | + $this->downloadFrankenPhpBinary(); |
| 152 | + } catch (Throwable $e) { |
| 153 | + report($e); |
| 154 | + |
| 155 | + rename("$frakenPhpBinary.backup", $frakenPhpBinary); |
| 156 | + |
| 157 | + return $this->warn('Unable to download FrankenPHP binary. The HTTP request exception has been logged.'); |
| 158 | + } |
| 159 | + |
| 160 | + unlink("$frakenPhpBinary.backup"); |
| 161 | + } |
| 162 | + } |
| 163 | +} |
0 commit comments