Download the PHP package thettler/laravel-command-attribute-syntax without Composer

On this page you can find all versions of the php package thettler/laravel-command-attribute-syntax. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package laravel-command-attribute-syntax

❌❌THIS REPO IS NO LONGER SUPPORTED PLEASE USE https://github.com/thettler/laravel-console-toolkit ❌❌

Laravel Command Attribute Syntax

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

This package lets you use PHP Attributes to define your Arguments and Options on Artisan Commands. You do not need to touch the handle() method in existing commands in order to use this package. Supports all Laravel features and more:

: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 attributes in your commands you first need to replace the default \Illuminate\Console\Command class with Thettler\LaravelCommandAttributeSyntax\Command. Then add the Thettler\LaravelCommandAttributeSyntax\Attributes\CommandAttribute to the class.

The CommandAttribute 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 CommandAttribute 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. It's also possible to access the arguments and options via the normal laravel methods $this->argument('propertyName') or $this->option('propertyName').

More about that in the following sections. :arrow_down:

:exclamation: The property will only be hydrated inside of 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

Option alias

By default, the option name used on the commandline will be same as the property name. You can change this with the name parameter on the Option 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:

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.

Casts

It's also possible to define your own casts. To do so you need to create a class that implements the CastInterface. The match() method checks if a type can be cast by this cast-class and returns true if it is possible and false if not.

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:

To register your cast you need to publish the config file first:

and add your cast to the cast array:

The package goes top to bottom through the array and uses the first cast that returns true from the match() method.

Now finally typehint our Argument (or Option).

: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

:books: License

The MIT License (MIT). Please see License File for more information.


All versions of laravel-command-attribute-syntax with dependencies

PHP Build Version
Package Version
Requires php Version ^8.1
symfony/console Version ^6.0
spatie/laravel-package-tools Version ^1.9.2
illuminate/contracts Version ^9.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package thettler/laravel-command-attribute-syntax contains the following files

Loading the files please wait ....