A package adds commands to Crest in its composer.json:
"extra": {
"crest": {
"commands": {
"demo:greet": "Vendor\\Demo\\GreetCommand"
}
}
}Crest reads this block from each package that is installed with the Crest that runs, and from its root project. In a project, the global Crest passes the listing and the project commands to the project Crest (see The global Crest and the project Crest). Thus only the packages of the project count. Outside a project, the packages of the global Crest count. These commands cannot replace a command or an alias of Crest, and the root project replaces a package command with the same name. crest make:command writes a command and prints its block (see Command reference). A package command works in both flavors, and make:command gives the same output in both.
An unknown command gives a hint when Crest knows the package that provides its prefix:
crest: unknown command 'migration:run'; provided by phalcon/migrationsThere is no hint when a registered command has that prefix: the package is installed, and only the name is wrong.
A command extends Crest\Console\Command\Command and declares two methods:
use Crest\Console\Command\Command;
use Crest\Console\Input;
use Crest\Console\Output;
use Crest\Console\Parsing\Definition;
final class GreetCommand extends Command
{
public function define(): Definition
{
return Definition::for('demo:greet', 'Greet someone')
->argument('name', true, 'Who to greet')
->option('shout', 'Use capital letters');
}
public function handle(Input $input, Output $output): int
{
$output->line('Hello ' . $input->argumentString('name'));
return 0;
}
}Crest makes the command with new $class(), so the constructor must work without arguments. Crest skips a class that is not a Command, or that new $class() cannot create. Each command also gets the global options (--config, --directory, --trace, --help, --quiet, --no-interaction; see Global options).
These types are the API for a package. Each type has @api in its docblock:
| Type | Use |
|---|---|
Crest\Console\Command\Command |
the base class of a command |
Crest\Console\Parsing\Definition |
for(), argument() and option() declare the arguments and options |
Crest\Console\Input |
the values of the call |
Crest\Console\Output |
line(), success(), error(), write(), table(), ask() and choice() |
Crest\Console\Exceptions\Exception |
throw it to stop with one clean error line |
Crest\Command\Project |
Project::context($input) gives the project of the call |
Crest\Project\ProjectContext |
root(), path($key) and namespaceFor($key) |
Project::context() finds the project as the other commands do (see The project file). Without crest.php, the command stops with the crest init hint.
A public element with @internal in its docblock is not part of the API, and it can change in any release. Tools that read the tag, for example an IDE or a rule of a static analyzer, can report its use from another package. Do not extend Crest\Command\ProjectCommand: it is the base of the commands of Crest, and it changes with them. Do not use Crest\Project\Config: Project::context() gives the project as a ProjectContext.