fyre/command

A command library.

v4.0.4 2024-06-29 06:53 UTC

This package is auto-updated.

Last update: 2024-08-29 07:13:41 UTC


README

FyreCommand is a free, open-source CLI command library for PHP.

Table Of Contents

Installation

Using Composer

composer require fyre/command

In PHP:

use Fyre\Command\CommandRunner;

Methods

Add Namespace

Add a namespace for loading commands.

  • $namespace is a string representing the namespace.
CommandRunner::addNamespace($namespace);

All

Get all available commands.

$commands = CommandRunner::all();

This method will return an array where the key is the command alias, and the value is an instance of the command.

Clear

Clear all namespaces and loaded commands.

CommandRunner::clear();

Get Namespaces

Get the namespaces.

$namespaces = CommandRunner::getNamespaces();

Handle

Handle an argv Command.

  • $argv is an array containing the CLI arguments.
$code = CommandRunner::handle($argv);

Has Command

Check if a command exists.

  • $alias is a string representing the command alias.
$hasCommand = CommandRunner::hasCommand($alias);

Has Namespace

Check if a namespace exists.

  • $namespace is a string representing the namespace.
$hasNamespace = CommandRunner::hasNamespace($namespace);

Remove Namespace

Remove a namespace.

  • $namespace is a string representing the namespace.
$removed = CommandRunner::removeNamespace($namespace);

Run

Run a Command.

  • $alias is a string representing the command alias.
  • $arguments is an array containing arguments for the command, and will default to [].
$code = CommandRunner::run($alias, $arguments);

Commands

Custom commands can be created by extending \Fyre\Command\Command, suffixing the class name with "Command", and ensuring the run method is implemented.

Alias

Get the command alias.

$alias = $command->getAlias();

The alias can be set by defining the $alias property on the command, otherwise the class name will be used by default.

Get Description

Get the command description.

$description = $command->getDescription();

The description can be set by defining the $description property on the command.

Get Name

Get the command name.

$name = $command->getName();

The name can be set by defining the $name property on the command, otherwise the class name will be used by default.

Run

Run the command.

  • $arguments is an array containing the command arguments.
$code = $command->run($arguments);

This method should return an integer representing the command exit code. The class constants Command::CODE_SUCCESS and Command::CODE_ERROR can be used.