master

laravel/framework

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

ConsoleMakeCommand.php

TLDR

The ConsoleMakeCommand.php file in the Illuminate\Foundation\Console namespace contains a class named ConsoleMakeCommand that extends GeneratorCommand. The class is responsible for generating a new Artisan command. It uses a stub file and provides methods to replace placeholders in the stub file with the appropriate values. The class also defines methods to customize the generated class name, namespace, arguments, and options.

Methods

replaceClass

This method replaces the dummy:command placeholder in the stub file with the provided command name. It uses the Str helper class to convert the command name to kebab case.

getStub

This method returns the path of the stub file used for generating the new Artisan command. It first checks if a custom stub file exists in the project, and if not, it returns the default stub file in the same directory as the ConsoleMakeCommand class.

getDefaultNamespace

This method returns the default namespace for the new class being generated. It appends \Console\Commands to the root namespace provided as an argument.

getArguments

This method returns an array of console command arguments. It defines a required name argument for the command.

getOptions

This method returns an array of console command options. It defines two options: force and command. The force option allows the class to be created even if the console command already exists, and the command option specifies the terminal command that will be used to invoke the class.

Classes

ConsoleMakeCommand

This class extends the GeneratorCommand class and is responsible for generating a new Artisan command. It provides methods to customize the generated class name, namespace, stub file, and replaces placeholders in the stub file with appropriate values. The class also defines console command arguments and options.

<?php

namespace Illuminate\Foundation\Console;

use Illuminate\Console\Concerns\CreatesMatchingTest;
use Illuminate\Console\GeneratorCommand;
use Illuminate\Support\Str;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;

#[AsCommand(name: 'make:command')]
class ConsoleMakeCommand extends GeneratorCommand
{
    use CreatesMatchingTest;

    /**
     * The console command name.
     *
     * @var string
     */
    protected $name = 'make:command';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Create a new Artisan command';

    /**
     * The type of class being generated.
     *
     * @var string
     */
    protected $type = 'Console command';

    /**
     * Replace the class name for the given stub.
     *
     * @param  string  $stub
     * @param  string  $name
     * @return string
     */
    protected function replaceClass($stub, $name)
    {
        $stub = parent::replaceClass($stub, $name);

        $command = $this->option('command') ?: 'app:'.Str::of($name)->classBasename()->kebab()->value();

        return str_replace(['dummy:command', '{{ command }}'], $command, $stub);
    }

    /**
     * Get the stub file for the generator.
     *
     * @return string
     */
    protected function getStub()
    {
        $relativePath = '/stubs/console.stub';

        return file_exists($customPath = $this->laravel->basePath(trim($relativePath, '/')))
            ? $customPath
            : __DIR__.$relativePath;
    }

    /**
     * Get the default namespace for the class.
     *
     * @param  string  $rootNamespace
     * @return string
     */
    protected function getDefaultNamespace($rootNamespace)
    {
        return $rootNamespace.'\Console\Commands';
    }

    /**
     * Get the console command arguments.
     *
     * @return array
     */
    protected function getArguments()
    {
        return [
            ['name', InputArgument::REQUIRED, 'The name of the command'],
        ];
    }

    /**
     * Get the console command options.
     *
     * @return array
     */
    protected function getOptions()
    {
        return [
            ['force', 'f', InputOption::VALUE_NONE, 'Create the class even if the console command already exists'],
            ['command', null, InputOption::VALUE_OPTIONAL, 'The terminal command that will be used to invoke the class'],
        ];
    }
}