From 49688755028e4b7d6677df3aafa5f98071dd11fb Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Mon, 11 Mar 2024 11:36:13 +0800 Subject: [PATCH] [11.x] Allow `install:broadcasting` to populate `echo.js` configuration either with or without Reverb fixes #50440 Signed-off-by: Mior Muhammad Zaki --- .../Console/BroadcastingInstallCommand.php | 31 +++++++++++++------ .../Console/stubs/echo-js.pusher.stub | 15 +++++++++ 2 files changed, 37 insertions(+), 9 deletions(-) create mode 100644 src/Illuminate/Foundation/Console/stubs/echo-js.pusher.stub diff --git a/src/Illuminate/Foundation/Console/BroadcastingInstallCommand.php b/src/Illuminate/Foundation/Console/BroadcastingInstallCommand.php index 720646929284..d723298cee70 100644 --- a/src/Illuminate/Foundation/Console/BroadcastingInstallCommand.php +++ b/src/Illuminate/Foundation/Console/BroadcastingInstallCommand.php @@ -9,6 +9,7 @@ use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Process\PhpExecutableFinder; +use function Illuminate\Filesystem\join_paths; use function Laravel\Prompts\confirm; #[AsCommand(name: 'install:broadcasting')] @@ -53,9 +54,11 @@ public function handle() $this->uncommentChannelsRoutesFile(); } + $installReverb = $this->shouldInstallReverb(); + // Install bootstrapping... if (! file_exists($echoScriptPath = $this->laravel->resourcePath('js/echo.js'))) { - copy(__DIR__.'/stubs/echo-js.stub', $echoScriptPath); + copy(join_paths(__DIR__, 'stubs', $installReverb ? 'echo-js.stub' : 'echo.js.pusher.stub'), $echoScriptPath); } if (file_exists($bootstrapScriptPath = $this->laravel->resourcePath('js/bootstrap.js'))) { @@ -71,7 +74,9 @@ public function handle() } } - $this->installReverb(); + if ($installReverb) { + $this->performReverbInstallation(); + } $this->installNodeDependencies(); } @@ -111,18 +116,12 @@ protected function uncommentChannelsRoutesFile() * * @return void */ - protected function installReverb() + protected function performReverbInstallation() { if (InstalledVersions::isInstalled('laravel/reverb')) { return; } - $install = confirm('Would you like to install Laravel Reverb?', default: true); - - if (! $install) { - return; - } - $this->requireComposerPackages($this->option('composer'), [ 'laravel/reverb:@beta', ]); @@ -138,6 +137,20 @@ protected function installReverb() $this->components->info('Reverb installed successfully.'); } + /** + * Determine if Reverb should be installed. + * + * @return bool + */ + public function shouldInstallReverb() + { + if (InstalledVersions::isInstalled('laravel/reverb')) { + return true; + } + + return confirm('Would you like to install Laravel Reverb?', default: true); + } + /** * Install and build Node dependencies. * diff --git a/src/Illuminate/Foundation/Console/stubs/echo-js.pusher.stub b/src/Illuminate/Foundation/Console/stubs/echo-js.pusher.stub new file mode 100644 index 000000000000..520887d57d4a --- /dev/null +++ b/src/Illuminate/Foundation/Console/stubs/echo-js.pusher.stub @@ -0,0 +1,15 @@ +import Echo from 'laravel-echo'; + +import Pusher from 'pusher-js'; +window.Pusher = Pusher; + +window.Echo = new Echo({ + broadcaster: 'pusher', + key: import.meta.env.VITE_PUSHER_APP_KEY, + cluster: import.meta.env.VITE_PUSHER_APP_CLUSTER ?? 'mt1', + wsHost: import.meta.env.VITE_PUSHER_HOST ? import.meta.env.VITE_PUSHER_HOST : `ws-${import.meta.env.VITE_PUSHER_APP_CLUSTER}.pusher.com`, + wsPort: import.meta.env.VITE_PUSHER_PORT ?? 80, + wssPort: import.meta.env.VITE_PUSHER_PORT ?? 443, + forceTLS: (import.meta.env.VITE_PUSHER_SCHEME ?? 'https') === 'https', + enabledTransports: ['ws', 'wss'], +});