PHP code example of thettler / laravel-console-toolkit

1. Go to this page and download the library: Download thettler/laravel-console-toolkit library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

thettler / laravel-console-toolkit example snippets



namespace App\Console\Commands;

use Illuminate\Console\Command;
use Thettler\LaravelConsoleToolkit\Concerns\UsesConsoleToolkit;

#[ArtisanCommand(
    name: 'basic',
)]
class BasicCommand extends Command
{
    use UsesConsoleToolkit;
    
    public function handle()
    {
    }
}

    

namespace App\Console\Commands;

use Illuminate\Console\Command;

class BasicCommand extends Command
{
    protected $signature = 'basic';

    public function handle()
    {
    }
}

#[ArtisanCommand(
    name: 'basic',
    description: 'Some useful description.',
    help: 'Some helpful text.',
    hidden: true
)]
class BasicCommand extends Command
{
    use UsesConsoleToolkit;

    ...
}

...
class BasicCommand extends Command
{
    protected $signature = 'basic';

    protected $description = 'Some useful description.';

    protected $help = 'Some helpful text.';
    
    protected $hidden = true;
    ...
}

...
use \Thettler\LaravelConsoleToolkit\Attributes\Argument;

#[ArtisanCommand(
    name: 'basic',
)]
class BasicCommand extends Command
{
    use UsesConsoleToolkit;

    #[Argument]
    protected string $myArgument;
    
    public function handle() {
        $this->line($this->myArgument);
    }
}

class BasicCommand extends Command
{
    protected $signature = 'basic {myArgument}';
    
    public function handle() {
        $this->line($this->argument('myArgument'));
    }
}

#[ArtisanCommand(
    name: 'basic',
)]
class BasicCommand extends Command
{
    use UsesConsoleToolkit;

    #[Argument]
    protected array $myArray;
    
    public function handle() {
        $this->line(implode(', ', $this->myArray));
    }
}

class BasicCommand extends Command
{
    protected $signature = 'basic {myArgument*}';
    
    public function handle() {
        $this->line($this->argument('myArgument'));
    }
}

#[ArtisanCommand(
    name: 'basic',
)]
class BasicCommand extends Command
{
    use UsesConsoleToolkit;

    #[Argument]
    protected ?string $myArgument;
    
    ...
}

class BasicCommand extends Command
{
    protected $signature = 'basic {myArgument?}';
    
    ...
}

#[ArtisanCommand(
    name: 'basic',
)]
class BasicCommand extends Command
{
    use UsesConsoleToolkit;

    #[Argument]
    protected string $myArgument = 'default';
    
    ...
}

class BasicCommand extends Command
{
    protected $signature = 'basic {myArgument=default}';
    
    ...
}

#[ArtisanCommand(
    name: 'basic',
)]
class BasicCommand extends Command
{
    use UsesConsoleToolkit;

    #[Argument(
        description: 'Argument Description'
    )]
    protected string $myArgument;
    
    ...
}

...
class BasicCommand extends Command
{
    protected $signature = 'basic {myArgument: Argument Description}';
    
    ...
}

use \Thettler\LaravelConsoleToolkit\Attributes\Option;

#[ArtisanCommand(
    name: 'basic',
)]
class BasicCommand extends Command
{
    use UsesConsoleToolkit;

    #[Option]
    protected bool $myOption;
    
    public function handle() {
        dump($this->myOption);
    }
}

class BasicCommand extends Command
{
    protected $signature = 'basic {--myOption}';
    
    public function handle() {
        dump($this->option('myOption'));
    }
}

#[ArtisanCommand(
    name: 'basic',
)]
class BasicCommand extends Command
{
    use UsesConsoleToolkit;

    #[Option]
    protected string $t'; // The option has a default value

    #[Option]
    protected array $array; // an Array Option 

    #[Option]
    protected array $defaultArray = ['default1', 'default2']; // an Array Option with default
    ...
}

class BasicCommand extends Command
{
    // tected $signature = 'basic {--optionalValue=} {--defaultValue=default} {--array=*}';
   
   ...
}

#[ArtisanCommand(
    name: 'basic',
)]
class BasicCommand extends Command
{
    use UsesConsoleToolkit;

    #[Option(
        description: 'Option Description'
    )]
    protected bool $option;
    ...
}

class BasicCommand extends Command
{
    protected $signature = 'basic {--option: Option Description}';
}

#[ArtisanCommand(
    name: 'basic',
)]
class BasicCommand extends Command
{
    use UsesConsoleToolkit;

    #[Option(
        shortcut: 'Q'
    )]
    protected bool $option;
    ...
}

class BasicCommand extends Command
{
    protected $signature = 'basic {--Q|option}';
}

#[ArtisanCommand(
    name: 'basic',
)]
class BasicCommand extends Command
{
    use UsesConsoleToolkit;

    #[Option(
        negatable: true
    )]
    protected bool $yell;
    
    public function handle(){
       dump($this->yell); // true if called with --yell
       dump($this->yell); // false if called with --no-yell
    }
}

enum Enum
{
    case A;
    case B;
    case C;
}

enum IntEnum: int
{
    case A = 1;
    case B = 2;
    case C = 3;
}

enum StringEnum: string
{
    case A = 'String A';
    case B = 'String B';
    case C = 'String C';
}

    #[Argument]
    protected Enum $argEnum;

    #[Argument]
    protected StringEnum $argStringEnum;

    #[Argument]
    protected IntEnum $argIntEnum;

    #[Option]
    protected Enum $enum;

    #[Option]
    protected StringEnum $stringEnum;

    #[Option]
    protected IntEnum $intEnum;

#[ArtisanCommand(
    name: 'basic',
)]
class BasicCommand extends Command
{
    use UsesConsoleToolkit;

    #[Argument(
        as: 'alternativeArgument'
    )]
    protected string $myArgument;
 
    #[Option(
        as: 'alternativeName'
    )]
    protected bool $myOption;
    
    public function handle(){
       dump($this->myArgument);
       dump($this->myOption);
    }
}

#[ArtisanCommand(
    name: 'basic',
)]
class BasicCommand extends Command
{
    use UsesConsoleToolkit;

    #[Argument]
    protected BandModel $band;

    public function configureDefaults(): void {
        $this->band = BandModel::find('2');
    }
    
    public function handle(){
       dump($this->band); // The Band with id 2
    }
}

    #[Argument]
    protected BandModel $band;
    
    public function handle(){
        $this->band // Well be an instance of BandModel 
    }

    #[Argument(
        cast: new \Thettler\LaravelConsoleToolkit\Casts\ModelCaster(
            findBy: 'name',
            select: ['id', 'name']
            with: ['songs']
        )
    )]
    protected BandModel $band;
    
    public function handle(){
        $this->band // Will be an instance of BandModel 
    }

    #[Argument(
        cast: \Thettler\LaravelConsoleToolkit\Casts\EnumCaster::class
    )]
    protected Enum $argEnum;

    #[Option(
        cast: new \Thettler\LaravelConsoleToolkit\Casts\EnumCaster(Enum::class)
    )]
    protected Enum $enum;

    #[Argument(
        cast: new \Thettler\LaravelConsoleToolkit\Casts\ArrayCaster(
            caster: \Thettler\LaravelConsoleToolkit\Casts\EnumCaster::class, 
            type: StringEnum::class
        )
    )]
    protected array $enumArray;

    #[Option(
        cast: new \Thettler\LaravelConsoleToolkit\Casts\ArrayCaster(
            caster: \Thettler\LaravelConsoleToolkit\Casts\EnumCaster::class, 
            type: StringEnum::class
        )
    )]
    protected array $enumArray2;



class UserCast implements Caster
{
    /**
    * This method deals with the conversion from the default value to a value the console understand so only 
    * basic return types are allowed  
    * 
    * @param mixed $value The default value if one is present
    * @param string $type The type is a string representation of the type of the property 
    * @param \ReflectionProperty $property The property reflection itself for more control
    * @return int|float|array|string|bool|null
     */
    public function from(mixed $value, string $type, \ReflectionProperty $property): int|float|array|string|bool|null
    {
        if ($value instanceof Band){
            return $value->getKey();
        }
        
        throw new Exception(self::class . ' can only be used with type '. Band::class)
    }

    /**
     * This method deals with the conversion from console input to property value 
     * 
     * @param  mixed  $value The Value from the command line
     * @param  class-string<Band>  $type The type is a string representation of the type of the property 
     * @param  \ReflectionProperty  $property The property reflection itself for more control
     * @return mixed
     */
    public function to(mixed $value, string $type, \ReflectionProperty $property)
    {
        return $type::find($value);
    }
}

    /** Uses the UserCaster everytime the User class is typehint on an Argument or Option */
    \Thettler\LaravelConsoleToolkit\ConsoleToolkit::addCast(UserCaster::class, User::class);

    /** Uses the UserCaster everytime the User or MasterUser class is typehint on an Argument or Option */
    \Thettler\LaravelConsoleToolkit\ConsoleToolkit::addCast(UserCaster::class, [User::class, MasterUser::class]);

    /** Uses the UserCaster everytime the callable returns true */
    \Thettler\LaravelConsoleToolkit\ConsoleToolkit::addCast(
        UserCaster::class,
        fn (mixed $value, ReflectionProperty $property): bool  =>  is_subclass_of($property->getType()->getName(), User::class);
    );

    #[Argument(
        validation: ['max:5']
    )]
    protected string $validated;

    #[Argument(
        validation: new \Thettler\LaravelConsoleToolkit\Transfers\Validation(
            rules: ['max:5']
            messages: [
                'max' => 'This is way to much!'
            ]   
        )
    )]
    protected string $validated;

    #[Argument(
        autoAsk: false
    )]
    protected string $dontAsk;

    \Thettler\LaravelConsoleToolkit\ConsoleToolkit::enableAutoAsk(false);
bash
php artisan basic
bash
php artisan basic myValue
# Output:
# myValue
bash
php artisan basic --myOption
# Output
# true
bash
php artisan basic
# Output
# false
bash
php artisan basic --
bash
php artisan basic -Q
bash
php artisan basic --yell
php artisan basic --no-yell
bash
php artisan basic something --alternativeName