Download the PHP package thettler/laravel-console-toolkit without Composer
On this page you can find all versions of the php package thettler/laravel-console-toolkit. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download thettler/laravel-console-toolkit
More information about thettler/laravel-console-toolkit
Files in thettler/laravel-console-toolkit
Package laravel-console-toolkit
Short Description This Package provides some usefully console features like the attribute syntax for arguments and options, validation, auto ask and casting.
License MIT
Homepage https://github.com/thettler/laravel-console-toolkit
Informations about the package laravel-console-toolkit
Laravel Console Toolkit
This package makes it even easier to write maintainable and expressive Artisan commands, with argument/option casting, validation and autoAsk. Also, it lets you define your arguments/options with simple properties and attributes for better ide support and static analysis. And all this with a single trait.
🤯 Features
All the features:
Support | Name | Description |
---|---|---|
✅ | Laravel Features | Supports everything laravel can do |
✅ | Attribute Syntax | Use PHP-Attributes to automatically define your inputs based on types |
✅ | Casting | Automatically cast your inputs to Enums, Models, Objects or anything you want |
✅ | Validation | Use the Laravel Validator to validate the inputs from the console |
✅ | Auto Ask | If the user provides an invalid value toolkit will ask again for a valid value without the need to run the command again |
✅ | Negatable Options | Options can be specified as opposites: --dry or --no-dry |
✅ | Option required Value | Options can have required values |
:purple_heart: Support me
Visit my blog on https://bitbench.dev or follow me on Social Media Twitter @bitbench Instagram @bitbench.dev
:package: Installation
You can install the package via composer:
:wrench: Usage
:right_anger_bubble: Before you use this package you should already have an understanding of Artisan Commands. You can read about them here.
A Basic Command
To use the Toolkit you simply need to add the UsesConsoleToolkit
trait inside your command.
Then add the Thettler\LaravelConsoleToolkit\Attributes\ArtisanCommand
to the class to specify the name and other
things like description, help, and so on.
The ArtisanCommand
requires the name
parameter to be set. This will be the name of the Command which you can use to
call it from the commandline.
And call it like:
Traditional Syntax
Descriptions, Help and Hidden Commands
If you want to add a description, a help comment or mark the command as hidden, you can specify this on
the ArtisanCommand
Attribute like this:
I like to use named arguments for a more readable look.
Traditional Syntax
Defining Input Expectations
The basic workflow to add an argument or option is always to add a property and decorate it with an Attribute.
#[Option]
if you want an option and #[Argument]
if you want an argument. The property will be hydrated with the
value from the command line, so you can use it like any normal property inside your handle()
method.
More about that in the following sections. :arrow_down:
:exclamation: The property will only be hydrated inside the
handle()
method. Keep that in mind.
Arguments
To define Arguments you create a property and add the Argument
attribute to it. The property will be hydrated with the
value from the command line, so you can use it like any normal property inside your handle()
method.
call it like:
Traditional Syntax
Array Arguments
You can also use arrays in arguments, simply typehint the property as array
.
Call it like:
Traditional Syntax
Optional Arguments
Of course, you can use optional arguments as well. To achieve this you simply make the property nullable.
:information_source: This works with
array
as well but the property won't be null but an empty array instead
Traditional Syntax
If your argument should have a default value, you can assign a value to the property which will be used as default value.
Traditional Syntax
Argument Description
You can set a description for arguments as parameter on the Argument
Attribute.
Traditional Syntax
:exclamation: :exclamation: If you have more than one argument the order inside the class will also be the order on the commandline
Options
To use options in your commands you use the Options
Attribute. If you have set a typehint of boolean
it will be
false if the option was not set and true if it was set.
Call it like:
Traditional Syntax
Value Options
You can add a value to an option if you type hint the property with something different as bool
. This will
automatically make it to an option with a value. If your typehint is not nullable the option will have a required value.
This means the option can only be used with a value.
:x: Wont work --myoption
:white_check_mark: works --myoption=myvalue
If you want to make the value optional simply make the type nullable or assign a value to the property
Call it like:
Traditional Syntax
Option Description
You can set a description for an option on the Option
Attribute.
Traditional Syntax
Option Shortcuts
You can set a shortcut for an option on the Option
Attribute.
:warning: Be aware that a shortcut can only be one char long
Call it like:
Traditional Syntax
Negatable Options
You can make option negatable by adding the negatable parameter to the Option
Attribute. Now the option accepts either
the flag (e.g. --yell) or its negation (e.g. --no-yell).
Call it like:
Enum Types
It is also possible to type Arguments
or Options
as Enum. The Package will automatically cast the input from the
commandline to the typed Enum. If you use BackedEnums you use the value of the case and if you have a non backed Enum
you use the name of the case.
Input alias
By default, the input name used on the commandline will be same as the property name. You can change this with the as
parameter on the Option
or Argument
Attribute. This can be handy if you have conflicting property names or want a
more expressive api for your commands.
:warning: If you use the
->option()
syntax you need to specify the alias name to get the option.
Call it like:
Special Default values
If you want to use some objects with casts as default values you can use the configureDefauls()
method on the command
to set default values.
Casts
Cast can be specified on Arguments
and Options
. You can either provide a class-string of a caster to use or an
instance of the caster. This is helpful to configure the caster via the constructor.
Model Cast
The Toolkit provides a cast for eloquent models out of the box. So if you typehint an eloquent model toolkit will try to match the console input to the primary key of the model and fetches it from the database.
If you want to change the column that will be used to match the input to the database, load relations or only select specific columns you can use the manual cast like this:
Enum Cast
The enum cast will automatically cast every typed enum to this enum. But you can also manually specify it like so.
Array Cast
If you have an array and want to cast all its values to a specific type you can use the ArrayCaster. It expects a caster and a specific type:
Custom Casts
It's also possible to define your own casts. To do so you need to create a class that implements the Caster
Interface.
Let's have a look at small UserCast that allows to simply use the id of a user model on the command line and automatically fetch the correct user from the database:
Now you can use this cast ether locally on an attribute or register it globally for automatic casting like this in your AppServiceProvider
Validation
You can also use the normal laravel validation rules to validate the input.
If you want custom messages you need to use the Validation object
Auto Ask
By default, Auto Ask is enabled. Every time a command is called with an input that fails validation or is required but not specified the command automatically asks the user to enter a (new) value. If the type is an enum it will give the user choice with all the enum values.
If you want to disable this behavior you can do it locally:
or globally in your AppServiceProvider:
:robot: Testing
:open_book: Changelog
Please see CHANGELOG for more information on what has changed recently.
:angel: Contributing
Please see CONTRIBUTING for details.
:lock: Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
:copyright: Credits
- Tobias Hettler
- All Contributors
:books: License
The MIT License (MIT). Please see License File for more information.
All versions of laravel-console-toolkit with dependencies
symfony/console Version ^6.0
spatie/laravel-package-tools Version ^1.9.2
illuminate/contracts Version ^9.0|^10.0