Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] Add support for stub option in Console/GeneratorCommand #50703

Closed

Conversation

inmanturbo
Copy link
Contributor

@inmanturbo inmanturbo commented Mar 21, 2024

This pull request adds support for adding a stub option to commands which extend the GeneratorCommand, ie make commands. This is not a breaking change and it doesn't add any extra option to the GeneratorCommand: concrete commands have to add the option in order to use it.

laravel/folio#136
livewire/volt#101

This involves two small changes in the abstract Console/GeneratorCommand

  1. Getting the stub from an option Console/GeneratorCommand::getStubOption()
    The first is a method for determining whether there is a stub option present, and if so returning the path to the stub (if found)

  2. Using the stub in Console/GeneratorCommand::buildClass() (if present and found)

Example Usage

Using laravel/folio's make command for generating pages as an example of a command that can benefit from this change.

in laravel/folio/src/MakeCommand.php

    /**
     * Get the console command arguments.
     */
    protected function getOptions(): array
    {
        return [
            ['force', 'f', InputOption::VALUE_NONE, 'Create the Folio page even if the page already exists'],
+           ['stub', 's', InputOption::VALUE_OPTIONAL, 'The stub file to use'],
        ];
    }

Usage:

After the above change, folio:make would supports passing either the name of an existing stub, or full path to file to be used as a stub as an option

php artisan make:folio "todos/index" --stub="folio-index"

will find and use base_path('stubs/folio-index.stub') if it exists.

php artisan make:folio "todos/index" \
  --stub="/home/taylor/stubs/talks/laracon/2024/us/folio/todos-index.stub"

will find and use /home/taylor/stubs/talks/laracon/2024/us/folio/todos-index.stub if it exists.

If it cannot find a stub under the name or path, it will ignore the option altogether and behave as it always has.
Likewise, if you do not add a stub option to the command, it will behave as it always has, and the above would simply result in the error The "--stub" option does not exist.

Copy link

Thanks for submitting a PR!

Note that draft PR's are not reviewed. If you would like a review, please mark your pull request as ready for review in the GitHub user interface.

Pull requests that are abandoned in draft may be closed due to inactivity.

@inmanturbo inmanturbo changed the title [11.x] Allow passing stub as an option in Console/GeneratorCommand [11.x] Support stub option in Console/GeneratorCommand Mar 21, 2024
@inmanturbo inmanturbo changed the title [11.x] Support stub option in Console/GeneratorCommand [11.x] Add support for stub option in Console/GeneratorCommand Mar 21, 2024
@inmanturbo inmanturbo marked this pull request as ready for review March 21, 2024 22:34
@inmanturbo inmanturbo marked this pull request as draft March 21, 2024 22:45
@inmanturbo inmanturbo closed this Mar 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant