master

laravel/framework

Last updated at: 29/12/2023 09:23

ApiInstallCommand.php

TLDR

This file, ApiInstallCommand.php, is a part of the Illuminate\Foundation\Console namespace and contains the ApiInstallCommand class. This class extends the Command class and is responsible for creating an API routes file and installing Laravel Sanctum.

Methods

handle

This method is responsible for executing the console command. It installs Laravel Sanctum, checks for the existence of the API routes file, copies a stub file to the specified location, and calls the uncommentApiRoutesFile method.

uncommentApiRoutesFile

This method is responsible for uncommenting the API routes file in the application bootstrap file (app.php). It reads the content of the bootstrap file, checks for specific patterns, and replaces them with the desired values if found.

installSanctum

This method is responsible for installing Laravel Sanctum into the application. It utilizes the requireComposerPackages method to install the laravel/sanctum package using Composer. It also runs the vendor:publish command to publish the Sanctum service provider.

Classes

ApiInstallCommand

This class extends the Command class from the Illuminate\Console namespace. It represents a console command that creates an API routes file and installs Laravel Sanctum. It has a $signature property that defines the command's signature, a $description property that provides a brief description of the command, and it implements the handle method to execute the command.

<?php

namespace Illuminate\Foundation\Console;

use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Facades\Process;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Process\PhpExecutableFinder;

#[AsCommand(name: 'install:api')]
class ApiInstallCommand extends Command
{
    use InteractsWithComposerPackages;

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'install:api
                    {--composer=global : Absolute path to the Composer binary which should be used to install packages}
                    {--force : Overwrite any existing API routes file}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Create an API routes file and install Laravel Sanctum';

    /**
     * Execute the console command.
     *
     * @return int
     */
    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.');
        } else {
            $this->components->info('Published API routes file.');

            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: ',
                $appBootstrapPath,
            );
        } 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.');

            return;
        }
    }

    /**
     * Install Laravel Sanctum into the application.
     *
     * @return void
     */
    protected function installSanctum()
    {
        $this->requireComposerPackages($this->option('composer'), [
            'laravel/sanctum:^4.0',
        ]);

        $php = (new PhpExecutableFinder())->find(false) ?: 'php';

        $result = Process::run([
            $php,
            defined('ARTISAN_BINARY') ? ARTISAN_BINARY : 'artisan',
            'vendor:publish',
            '--provider',
            'Laravel\\Sanctum\\SanctumServiceProvider',
        ]);
    }
}