Skip to content

Commit

Permalink
Adds install command (#45)
Browse files Browse the repository at this point in the history
* add install command

* formatting

---------

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
joedixon and taylorotwell authored Feb 14, 2024
1 parent 9ca0996 commit d5c8e14
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 0 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
152 changes: 152 additions & 0 deletions src/Console/Commands/InstallCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
<?php

namespace Laravel\Reverb\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;

use function Laravel\Prompts\confirm;

class InstallCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'reverb:install';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Install the Reverb dependencies';

/**
* Execute the console command.
*/
public function handle(): void
{
$this->addEnviromentVariables();
$this->publishConfiguration();
$this->enableBroadcasting();
$this->updateBroadcastingDriver();

$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.
*/
protected function publishConfiguration(): void
{
$this->callSilently('vendor:publish', [
'--provider' => 'Laravel\Reverb\ReverbServiceProvider',
'--tag' => 'reverb-config',
]);
}

/**
* Enable the Laravel broadcasting functionality.
*/
protected function enableBroadcasting(): void
{
if (File::exists(base_path('routes/channels.php'))) {
return;
}

$enable = confirm('Would you like to enable event broadcasting?', default: true);

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 configured broadcasting driver.
*/
protected function updateBroadcastingDriver(): void
{
$enable = confirm('Would you like to enable the Reverb broadcasting driver?', default: true);

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';
})
);
}
}
3 changes: 3 additions & 0 deletions src/ReverbServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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']);
Expand Down

0 comments on commit d5c8e14

Please sign in to comment.