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()
{
}
}
...
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;
...
}
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
...
}
#[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';
}
#[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
}
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);
);