[11.x] Add support for stub option in Console/GeneratorCommand #50703
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This pull request adds support for adding a stub option to commands which extend the
GeneratorCommand
, iemake
commands. This is not a breaking change and it doesn't add any extra option to theGeneratorCommand
: 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
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)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 optionwill find and use
base_path('stubs/folio-index.stub')
if it exists.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.