Skip to content

Commit

Permalink
make route uncommenting and adding a bit less brittle
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Nov 17, 2023
1 parent 3a54ee7 commit ba52648
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 10 deletions.
36 changes: 31 additions & 5 deletions src/Illuminate/Foundation/Console/ApiInstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class ApiInstallCommand extends Command
*/
public function handle()
{
$this->installSanctum();

if (file_exists($apiRoutesPath = $this->laravel->basePath('routes/api.php')) &&
! $this->option('force')) {
$this->components->error('API routes file already exists.');
Expand All @@ -44,16 +46,40 @@ public function handle()

copy(__DIR__.'/stubs/api-routes.stub', $apiRoutesPath);

$this->uncommentApiRoutesFile();
}

$this->components->info('API scaffolding installed. Please add the "Laravel\Sanctum\HasApiTokens" trait to your User model.');
}

/**
* Uncomment the API routes file in the application bootstrap file.
*
* @return void
*/
protected function uncommentApiRoutesFile()
{
$appBootstrapPath = $this->laravel->bootstrapPath('app.php');

$content = file_get_contents($appBootstrapPath);

if (str_contains($content, '// api: ')) {
(new Filesystem)->replaceInFile(
'// api: ',
'api: ',
$this->laravel->bootstrapPath('app.php'),
$appBootstrapPath,
);
}

$this->installSanctum();
} elseif (str_contains($content, 'web: __DIR__.\'/../routes/web.php\',')) {
(new Filesystem)->replaceInFile(
'web: __DIR__.\'/../routes/web.php\',',
'web: __DIR__.\'/../routes/web.php\','.PHP_EOL.' api: __DIR__.\'/../routes/api.php\',',
$appBootstrapPath,
);
} else {
$this->components->warn('Unable to automatically add API route definition to bootstrap file. API route file should be registered manually.');

$this->components->info('API scaffolding installed. Please add the "Laravel\Sanctum\HasApiTokens" trait to your User model.');
return;
}
}

/**
Expand Down
36 changes: 31 additions & 5 deletions src/Illuminate/Foundation/Console/BroadcastingInstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,7 @@ public function handle()

copy(__DIR__.'/stubs/broadcasting-routes.stub', $broadcastingRoutesPath);

(new Filesystem)->replaceInFile(
'// channels: ',
'channels: ',
$this->laravel->bootstrapPath('app.php'),
);
$this->uncommentChannelsRoutesFile();
}

// Install bootstrapping...
Expand All @@ -65,4 +61,34 @@ public function handle()
}
}
}

/**
* Uncomment the "channels" routes file in the application bootstrap file.
*
* @return void
*/
protected function uncommentChannelsRoutesFile()
{
$appBootstrapPath = $this->laravel->bootstrapPath('app.php');

$content = file_get_contents($appBootstrapPath);

if (str_contains($content, '// channels: ')) {
(new Filesystem)->replaceInFile(
'// channels: ',
'channels: ',
$appBootstrapPath,
);
} elseif (str_contains($content, 'commands: __DIR__.\'/../routes/console.php\',')) {
(new Filesystem)->replaceInFile(
'commands: __DIR__.\'/../routes/console.php\',',
'commands: __DIR__.\'/../routes/console.php\','.PHP_EOL.' channels: __DIR__.\'/../routes/channels.php\',',
$appBootstrapPath,
);
} else {
$this->components->warn('Unable to automatically add channel route definition to bootstrap file. Channel route file should be registered manually.');

return;
}
}
}

0 comments on commit ba52648

Please sign in to comment.