-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from WendellAdriel/feat/command-attributes
Add Command Attributes
- Loading branch information
Showing
12 changed files
with
408 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace WendellAdriel\Virtue\Commands\Attributes; | ||
|
||
use Attribute; | ||
use Closure; | ||
|
||
#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)] | ||
class Argument | ||
{ | ||
/** | ||
* @param string|int|bool|array<mixed>|float|null $default | ||
* @param array<mixed>|Closure $suggestedValues | ||
*/ | ||
public function __construct( | ||
public string $name, | ||
public bool $required = true, | ||
public bool $array = false, | ||
public string $description = '', | ||
public string|int|bool|array|float|null $default = null, | ||
public array|Closure $suggestedValues = [], | ||
) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace WendellAdriel\Virtue\Commands\Attributes; | ||
|
||
use Attribute; | ||
use Symfony\Component\Console\Input\InputOption; | ||
|
||
#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)] | ||
final class FlagOption extends Option | ||
{ | ||
public function __construct( | ||
public string $name, | ||
public string|array|null $shortcut = null, | ||
public string $description = '', | ||
public bool $negatable = false, | ||
) { | ||
parent::__construct( | ||
name: $name, | ||
shortcut: $shortcut, | ||
mode: $negatable ? InputOption::VALUE_NEGATABLE | InputOption::VALUE_NONE : InputOption::VALUE_NONE, | ||
description: $description, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace WendellAdriel\Virtue\Commands\Attributes; | ||
|
||
use Closure; | ||
use Symfony\Component\Console\Input\InputOption; | ||
|
||
abstract class Option | ||
{ | ||
/** | ||
* @param string|array<mixed>|null $shortcut | ||
* @param string|int|bool|array<mixed>|float|null $default | ||
* @param array<mixed>|Closure $suggestedValues | ||
*/ | ||
public function __construct( | ||
public string $name, | ||
public string|array|null $shortcut = null, | ||
public int $mode = InputOption::VALUE_NONE, | ||
public string $description = '', | ||
public string|bool|int|float|array|null $default = null, | ||
public array|Closure $suggestedValues = [], | ||
) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace WendellAdriel\Virtue\Commands\Attributes; | ||
|
||
use Attribute; | ||
use Closure; | ||
|
||
#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)] | ||
final class OptionalArgument extends Argument | ||
{ | ||
public function __construct( | ||
public string $name, | ||
public bool $array = false, | ||
public string $description = '', | ||
public string|int|bool|array|float|null $default = null, | ||
public array|Closure $suggestedValues = [], | ||
) { | ||
parent::__construct( | ||
name: $name, | ||
required: false, | ||
array: $array, | ||
description: $description, | ||
default: $default, | ||
suggestedValues: $suggestedValues, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace WendellAdriel\Virtue\Commands\Attributes; | ||
|
||
use Attribute; | ||
use Closure; | ||
|
||
#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)] | ||
final class RequiredArgument extends Argument | ||
{ | ||
public function __construct( | ||
public string $name, | ||
public bool $array = false, | ||
public string $description = '', | ||
public array|Closure $suggestedValues = [], | ||
) { | ||
parent::__construct( | ||
name: $name, | ||
array: $array, | ||
description: $description, | ||
suggestedValues: $suggestedValues, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace WendellAdriel\Virtue\Commands\Attributes; | ||
|
||
use Attribute; | ||
use Closure; | ||
use Symfony\Component\Console\Input\InputOption; | ||
|
||
#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)] | ||
final class ValueOption extends Option | ||
{ | ||
public function __construct( | ||
public string $name, | ||
public bool $array = false, | ||
public string|array|null $shortcut = null, | ||
public string $description = '', | ||
public string|bool|int|float|array|null $default = null, | ||
public array|Closure $suggestedValues = [], | ||
) { | ||
parent::__construct( | ||
name: $name, | ||
shortcut: $shortcut, | ||
mode: $array ? InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL : InputOption::VALUE_OPTIONAL, | ||
description: $description, | ||
default: $default, | ||
suggestedValues: $suggestedValues, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace WendellAdriel\Virtue\Commands\Concerns; | ||
|
||
use Illuminate\Support\Collection; | ||
use ReflectionAttribute; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use WendellAdriel\Virtue\Commands\Attributes\Argument; | ||
use WendellAdriel\Virtue\Commands\Attributes\FlagOption; | ||
use WendellAdriel\Virtue\Commands\Attributes\Option; | ||
use WendellAdriel\Virtue\Commands\Attributes\OptionalArgument; | ||
use WendellAdriel\Virtue\Commands\Attributes\RequiredArgument; | ||
use WendellAdriel\Virtue\Commands\Attributes\ValueOption; | ||
use WendellAdriel\Virtue\Support\HasAttributesReflection; | ||
|
||
trait Virtue | ||
{ | ||
use HasAttributesReflection; | ||
|
||
/** | ||
* @return array<InputArgument> | ||
*/ | ||
public function getArguments(): array | ||
{ | ||
$arguments = []; | ||
$generalArguments = $this->buildArgumentsList(Argument::class); | ||
|
||
$requiredArguments = $this->buildArgumentsList(RequiredArgument::class) | ||
->merge($generalArguments->where('mode', InputArgument::REQUIRED)); | ||
|
||
[$arrayArguments, $nonArrayArguments] = $requiredArguments->partition(fn (array $argument) => $argument['mode'] > InputArgument::REQUIRED); | ||
|
||
$optionalArguments = $this->buildArgumentsList(OptionalArgument::class) | ||
->merge($generalArguments->filter(fn (array $argument) => $argument['mode'] === InputArgument::OPTIONAL || $argument['mode'] === InputArgument::IS_ARRAY)); | ||
|
||
foreach ($nonArrayArguments as $argument) { | ||
$arguments[] = $argument; | ||
} | ||
|
||
foreach ($optionalArguments as $argument) { | ||
$arguments[] = $argument; | ||
} | ||
|
||
foreach ($arrayArguments as $argument) { | ||
$arguments[] = $argument; | ||
} | ||
|
||
return $arguments; | ||
} | ||
|
||
/** | ||
* @return array<InputOption> | ||
*/ | ||
public function getOptions(): array | ||
{ | ||
$options = []; | ||
$valueOptions = $this->buildOptionsList(ValueOption::class); | ||
$flagOptions = $this->buildOptionsList(FlagOption::class); | ||
|
||
foreach ($valueOptions as $option) { | ||
$options[] = $option; | ||
} | ||
|
||
foreach ($flagOptions as $option) { | ||
$options[] = $option; | ||
} | ||
|
||
return $options; | ||
} | ||
|
||
/** | ||
* @param class-string $attribute | ||
*/ | ||
private function buildArgumentsList(string $attribute): Collection | ||
{ | ||
return $this->resolveMultipleAttributes($attribute)->map(function (ReflectionAttribute $argumentAttribute) { | ||
/** @var Argument $attribute */ | ||
$attribute = $argumentAttribute->newInstance(); | ||
|
||
return [ | ||
'name' => $attribute->name, | ||
'mode' => $this->resolveMode($attribute), | ||
'description' => $attribute->description, | ||
'default' => $attribute->default, | ||
'suggestedValues' => $attribute->suggestedValues, | ||
]; | ||
}); | ||
} | ||
|
||
private function resolveMode(Argument $attribute): int | ||
{ | ||
return match (true) { | ||
$attribute->required && $attribute->array => InputArgument::IS_ARRAY | InputArgument::REQUIRED, | ||
$attribute->required && ! $attribute->array => InputArgument::REQUIRED, | ||
! $attribute->required && $attribute->array => InputArgument::IS_ARRAY, | ||
! $attribute->required && ! $attribute->array => InputArgument::OPTIONAL, | ||
default => InputArgument::REQUIRED, | ||
}; | ||
} | ||
|
||
/** | ||
* @param class-string $attribute | ||
*/ | ||
private function buildOptionsList(string $attribute): Collection | ||
{ | ||
return $this->resolveMultipleAttributes($attribute)->map(function (ReflectionAttribute $optionAttribute) { | ||
/** @var Option $attribute */ | ||
$attribute = $optionAttribute->newInstance(); | ||
|
||
return [ | ||
'name' => $attribute->name, | ||
'shortcut' => $attribute->shortcut, | ||
'mode' => $attribute->mode, | ||
'description' => $attribute->description, | ||
'default' => $attribute->default, | ||
'suggestedValues' => $attribute->suggestedValues, | ||
]; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace WendellAdriel\Virtue\Support; | ||
|
||
use Illuminate\Support\Collection; | ||
use ReflectionAttribute; | ||
use ReflectionClass; | ||
|
||
trait HasAttributesReflection | ||
{ | ||
/** @var array<class-string,Collection>|null */ | ||
private static ?array $classAttributes = []; | ||
|
||
/** | ||
* @param class-string $attributeClass | ||
*/ | ||
private function resolveSingleAttribute(string $attributeClass): ?ReflectionAttribute | ||
{ | ||
$classAttributes = $this->classAttributes(); | ||
|
||
return $classAttributes->filter(fn (ReflectionAttribute $attribute) => $attribute->getName() === $attributeClass) | ||
->first(); | ||
} | ||
|
||
private function resolveMultipleAttributes(string $attributeClass): Collection | ||
{ | ||
$classAttributes = $this->classAttributes(); | ||
|
||
return $classAttributes->filter(fn (ReflectionAttribute $attribute) => $attribute->getName() === $attributeClass); | ||
} | ||
|
||
private function classAttributes(): Collection | ||
{ | ||
$class = static::class; | ||
if (! array_key_exists($class, self::$classAttributes) || is_null(self::$classAttributes[$class])) { | ||
$reflectionClass = new ReflectionClass(static::class); | ||
self::$classAttributes[$class] = Collection::make($reflectionClass->getAttributes()); | ||
} | ||
|
||
return self::$classAttributes[$class]; | ||
} | ||
} |
Oops, something went wrong.