<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
thettler / laravel-command-attribute-syntax example snippets
namespace App\Console\Commands;
use Thettler\LaravelCommandAttributeSyntax\Attributes\ArtisanCommand;
use Thettler\LaravelCommandAttributeSyntax\Command;
#[ArtisanCommand(
name: 'basic',
)]
class BasicCommand extends Command
{
public function handle()
{
return 1;
}
}
namespace App\Console\Commands;
use Illuminate\Console\Command;
class BasicCommand extends Command
{
protected $signature = 'basic';
public function handle()
{
return 1;
}
}
#[CommandAttribute(
name: 'basic',
)]
class BasicCommand extends Command
{
#[Option(
name: 'alternativeName'
)]
protected bool $myOption;
public function handle(){
dump($this->myOption);
dump($this->option('alternativeName'))
}
}
#[CommandAttribute(
name: 'basic',
)]
class BasicCommand extends Command
{
#[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';
}
namespace Thettler\LaravelCommandAttributeSyntax\Casts;
use Thettler\LaravelCommandAttributeSyntax\Contracts\CastInterface;
class UserCast implements CastInterface
{
public static function match(string $typeName, mixed $value): bool
{
return $typeName === User::class;
}
public function cast(mixed $value, string $typeName): User
{
return User::find($value);
}
}
#[CommandAttribute(name: 'userName')]
class UserNameCommand extends \Thettler\LaravelCommandAttributeSyntax\Command
{
#[Argument]
protected User $user;
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$this->line($this->user->name);
}
}