FyreCommand is a free, open-source CLI command library for PHP.
Using Composer
composer require fyre/command
In PHP:
use Fyre\Command\CommandRunner;$containeris a Container.$inflectoris an Inflector.$loaderis a Loader.$iois a Console.$eventManageris an EventManager.$typeParseris an TypeParser.
$runner = new CommandRunner($container, $inflector, $loader, $io, $eventManager, $typeParser);Autoloading
It is recommended to bind the CommandRunner to the Container as a singleton.
$container->singleton(CommandRunner::class);Any dependencies will be injected automatically when loading from the Container.
$runner = $container->use(CommandRunner::class);Add Namespace
Add a namespace for loading commands.
$namespaceis a string representing the namespace.
$runner->addNamespace($namespace);All
Get all available commands.
$commands = $runner->all();Clear
Clear all namespaces and loaded commands.
$runner->clear();Get Namespaces
Get the namespaces.
$namespaces = $runner->getNamespaces();Handle
Handle an argv Command.
$argvis an array containing the CLI arguments.
$code = $runner->handle($argv);Has Command
Determine whether a command exists.
$aliasis a string representing the command alias.
$hasCommand = $runner->hasCommand($alias);Has Namespace
Determine whether a namespace exists.
$namespaceis a string representing the namespace.
$hasNamespace = $runner->hasNamespace($namespace);Remove Namespace
Remove a namespace.
$namespaceis a string representing the namespace.
$runner->removeNamespace($namespace);Run
Run a Command.
$aliasis a string representing the command alias.$argumentsis an array containing arguments for the command, and will default to [].
$code = $runner->run($alias, $arguments);Command options will be parsed from the provided arguments.
Custom commands can be created by extending \Fyre\Command\Command, suffixing the class name with "Command", and ensuring a run method is implemented.
Any dependencies will be resolved automatically from the Container.
The run method should return an integer representing the command exit code. The class constants Command::CODE_SUCCESS and Command::CODE_ERROR can be used.
You can define $alias and $description properties on the command. If no $alias is a provided, the command class name will be used (converted to snake_case).
You can also define an $options array on your custom commands, which will be used by the CommandRunner to parse the arguments and prompt for input if required.
promptis a string representing the prompt text, and will default to "".valuesis an array containing the values, and will default to null.requiredis a boolean indicating whether a value must be provided, and will default to false.asis a string representing the value type, and will default to null.defaultis the default value, and will default to true.
protected array $options = [
'name' => [
'prompt' => 'What is your name?',
'required' => true,
],
'color' => [
'prompt' => 'What is your favorite color?',
'values' => [
'red',
'green',
'blue',
],
],
'confirmed' => [
'prompt' => 'Do you want to continue?',
'as' => 'boolean',
'required' => true,
],
];
public function run(string $name, string|null $color, bool $confirmed): int;If an option is marked as required and not provided as an argument, the CommandRunner will prompt for the value, otherwise the default value will be used.