From 51530d36c1c811fc53df288dde659c25e8c1f5e2 Mon Sep 17 00:00:00 2001 From: Joe Dixon Date: Wed, 14 Feb 2024 14:17:17 +0000 Subject: [PATCH 1/2] add install command --- composer.json | 1 + src/Console/Commands/InstallCommand.php | 154 ++++++++++++++++++++++++ src/ReverbServiceProvider.php | 3 + 3 files changed, 158 insertions(+) create mode 100644 src/Console/Commands/InstallCommand.php diff --git a/composer.json b/composer.json index ed2f79bd..12670b1c 100644 --- a/composer.json +++ b/composer.json @@ -27,6 +27,7 @@ "illuminate/contracts": "^10.0|^11.0", "illuminate/http": "^10.0|^11.0", "illuminate/support": "^10.0|^11.0", + "laravel/prompts": "^0.1.15", "pusher/pusher-php-server": "^7.2", "ratchet/rfc6455": "^0.3.1", "react/socket": "^1.14", diff --git a/src/Console/Commands/InstallCommand.php b/src/Console/Commands/InstallCommand.php new file mode 100644 index 00000000..fe13feeb --- /dev/null +++ b/src/Console/Commands/InstallCommand.php @@ -0,0 +1,154 @@ +addEnviromentVariables(); + $this->publishConfiguration(); + $this->enableBroadcasting(); + $this->updateBroadcastingDriver(); + + $this->components->info('Reverb installed successfully.'); + } + + /** + * Publishes the Reverb configuration file. + */ + protected function publishConfiguration(): void + { + $this->callSilently('vendor:publish', [ + '--provider' => 'Laravel\Reverb\ReverbServiceProvider', + '--tag' => 'reverb-config', + ]); + } + + /** + * Enable broadcasting functionality. + */ + protected function enableBroadcasting(): void + { + $enable = select('Would you like to enable event broadcasting?', [ + true => 'Yes', + false => 'No', + ]); + + if (! $enable) { + return; + } + + if (version_compare($this->laravel->version(), '11.0', '<')) { + $this->enableBroadcastServiceProvider(); + + return; + } + + $this->callSilently('install:broadcasting'); + } + + /** + * Uncomment the BroadcastServiceProvider in the application configuration. + */ + protected function enableBroadcastServiceProvider(): void + { + $config = File::get(app()->configPath('app.php')); + + if (Str::contains($config, '// App\Providers\BroadcastServiceProvider::class')) { + File::replaceInFile( + '// App\Providers\BroadcastServiceProvider::class', + 'App\Providers\BroadcastServiceProvider::class', + app()->configPath('app.php'), + ); + } + } + + /** + * Update the broadcasting driver. + */ + protected function updateBroadcastingDriver(): void + { + $enable = select('Would you like to enable the Reverb broadcast driver?', [ + true => 'Yes', + false => 'No', + ]); + + if (! $enable || File::missing($env = app()->environmentFile())) { + return; + } + + File::put( + $env, + Str::of(File::get($env))->replaceMatches('/(BROADCAST_(?:DRIVER|CONNECTION))=\w*/', function (array $matches) { + return $matches[1].'=reverb'; + }) + ); + } + + /** + * Adds the Reverb configuration to the environment. + */ + protected function addEnviromentVariables(): void + { + if (File::missing($env = app()->environmentFile())) { + return; + } + + $contents = File::get($env); + $appId = random_int(100000, 999999); + $appKey = Str::lower(Str::random(20)); + $appSecret = Str::lower(Str::random(20)); + + $variables = Arr::where([ + 'REVERB_APP_ID' => "REVERB_APP_ID={$appId}", + 'REVERB_APP_KEY' => "REVERB_APP_KEY={$appKey}", + 'REVERB_APP_SECRET' => "REVERB_APP_SECRET={$appSecret}", + 'REVERB_HOST' => 'REVERB_HOST="0.0.0.0"', + 'REVERB_PORT' => 'REVERB_PORT=8080', + 'REVERB_SCHEME' => 'REVERB_SCHEME=http', + 'REVERB_NEW_LINE' => null, + 'VITE_REVERB_APP_KEY' => 'VITE_REVERB_APP_KEY="${REVERB_APP_KEY}"', + 'VITE_REVERB_HOST' => 'VITE_REVERB_HOST="${REVERB_HOST}"', + 'VITE_REVERB_PORT' => 'VITE_REVERB_PORT="${REVERB_PORT}"', + 'VITE_REVERB_SCHEME' => 'VITE_REVERB_SCHEME="${REVERB_SCHEME}"', + ], function ($value, $key) use ($contents) { + return ! Str::contains($contents, PHP_EOL.$key); + }); + + $variables = trim(implode(PHP_EOL, $variables)); + + if ($variables === '') { + return; + } + + File::append( + $env, + Str::endsWith($contents, PHP_EOL) ? PHP_EOL.$variables.PHP_EOL : PHP_EOL.PHP_EOL.$variables.PHP_EOL, + ); + } +} diff --git a/src/ReverbServiceProvider.php b/src/ReverbServiceProvider.php index f51afb2b..dac8c97e 100644 --- a/src/ReverbServiceProvider.php +++ b/src/ReverbServiceProvider.php @@ -4,6 +4,7 @@ use Illuminate\Foundation\Application; use Illuminate\Support\ServiceProvider; +use Laravel\Reverb\Console\Commands\InstallCommand; use Laravel\Reverb\Contracts\Logger; use Laravel\Reverb\Loggers\NullLogger; use Laravel\Reverb\Pulse\Reverb; @@ -33,6 +34,8 @@ public function register(): void public function boot(): void { if ($this->app->runningInConsole()) { + $this->commands(InstallCommand::class); + $this->publishes([ __DIR__.'/../config/reverb.php' => config_path('reverb.php'), ], ['reverb', 'reverb-config']); From e2446861507f26ba3a851919a558e6a328ee3938 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 14 Feb 2024 13:29:40 -0600 Subject: [PATCH 2/2] formatting --- src/Console/Commands/InstallCommand.php | 104 ++++++++++++------------ 1 file changed, 51 insertions(+), 53 deletions(-) diff --git a/src/Console/Commands/InstallCommand.php b/src/Console/Commands/InstallCommand.php index fe13feeb..519c4963 100644 --- a/src/Console/Commands/InstallCommand.php +++ b/src/Console/Commands/InstallCommand.php @@ -7,7 +7,7 @@ use Illuminate\Support\Facades\File; use Illuminate\Support\Str; -use function Laravel\Prompts\select; +use function Laravel\Prompts\confirm; class InstallCommand extends Command { @@ -38,6 +38,48 @@ public function handle(): void $this->components->info('Reverb installed successfully.'); } + /** + * Adds the Reverb configuration to the environment. + */ + protected function addEnviromentVariables(): void + { + if (File::missing($env = app()->environmentFile())) { + return; + } + + $contents = File::get($env); + $appId = random_int(100000, 999999); + $appKey = Str::lower(Str::random(20)); + $appSecret = Str::lower(Str::random(20)); + + $variables = Arr::where([ + 'REVERB_APP_ID' => "REVERB_APP_ID={$appId}", + 'REVERB_APP_KEY' => "REVERB_APP_KEY={$appKey}", + 'REVERB_APP_SECRET' => "REVERB_APP_SECRET={$appSecret}", + 'REVERB_HOST' => 'REVERB_HOST="0.0.0.0"', + 'REVERB_PORT' => 'REVERB_PORT=8080', + 'REVERB_SCHEME' => 'REVERB_SCHEME=http', + 'REVERB_NEW_LINE' => null, + 'VITE_REVERB_APP_KEY' => 'VITE_REVERB_APP_KEY="${REVERB_APP_KEY}"', + 'VITE_REVERB_HOST' => 'VITE_REVERB_HOST="${REVERB_HOST}"', + 'VITE_REVERB_PORT' => 'VITE_REVERB_PORT="${REVERB_PORT}"', + 'VITE_REVERB_SCHEME' => 'VITE_REVERB_SCHEME="${REVERB_SCHEME}"', + ], function ($value, $key) use ($contents) { + return ! Str::contains($contents, PHP_EOL.$key); + }); + + $variables = trim(implode(PHP_EOL, $variables)); + + if ($variables === '') { + return; + } + + File::append( + $env, + Str::endsWith($contents, PHP_EOL) ? PHP_EOL.$variables.PHP_EOL : PHP_EOL.PHP_EOL.$variables.PHP_EOL, + ); + } + /** * Publishes the Reverb configuration file. */ @@ -50,14 +92,15 @@ protected function publishConfiguration(): void } /** - * Enable broadcasting functionality. + * Enable the Laravel broadcasting functionality. */ protected function enableBroadcasting(): void { - $enable = select('Would you like to enable event broadcasting?', [ - true => 'Yes', - false => 'No', - ]); + if (File::exists(base_path('routes/channels.php'))) { + return; + } + + $enable = confirm('Would you like to enable event broadcasting?', default: true); if (! $enable) { return; @@ -89,14 +132,11 @@ protected function enableBroadcastServiceProvider(): void } /** - * Update the broadcasting driver. + * Update the configured broadcasting driver. */ protected function updateBroadcastingDriver(): void { - $enable = select('Would you like to enable the Reverb broadcast driver?', [ - true => 'Yes', - false => 'No', - ]); + $enable = confirm('Would you like to enable the Reverb broadcasting driver?', default: true); if (! $enable || File::missing($env = app()->environmentFile())) { return; @@ -109,46 +149,4 @@ protected function updateBroadcastingDriver(): void }) ); } - - /** - * Adds the Reverb configuration to the environment. - */ - protected function addEnviromentVariables(): void - { - if (File::missing($env = app()->environmentFile())) { - return; - } - - $contents = File::get($env); - $appId = random_int(100000, 999999); - $appKey = Str::lower(Str::random(20)); - $appSecret = Str::lower(Str::random(20)); - - $variables = Arr::where([ - 'REVERB_APP_ID' => "REVERB_APP_ID={$appId}", - 'REVERB_APP_KEY' => "REVERB_APP_KEY={$appKey}", - 'REVERB_APP_SECRET' => "REVERB_APP_SECRET={$appSecret}", - 'REVERB_HOST' => 'REVERB_HOST="0.0.0.0"', - 'REVERB_PORT' => 'REVERB_PORT=8080', - 'REVERB_SCHEME' => 'REVERB_SCHEME=http', - 'REVERB_NEW_LINE' => null, - 'VITE_REVERB_APP_KEY' => 'VITE_REVERB_APP_KEY="${REVERB_APP_KEY}"', - 'VITE_REVERB_HOST' => 'VITE_REVERB_HOST="${REVERB_HOST}"', - 'VITE_REVERB_PORT' => 'VITE_REVERB_PORT="${REVERB_PORT}"', - 'VITE_REVERB_SCHEME' => 'VITE_REVERB_SCHEME="${REVERB_SCHEME}"', - ], function ($value, $key) use ($contents) { - return ! Str::contains($contents, PHP_EOL.$key); - }); - - $variables = trim(implode(PHP_EOL, $variables)); - - if ($variables === '') { - return; - } - - File::append( - $env, - Str::endsWith($contents, PHP_EOL) ? PHP_EOL.$variables.PHP_EOL : PHP_EOL.PHP_EOL.$variables.PHP_EOL, - ); - } }