English | 中文文档
super-kernel/command registers the Symfony Console commands that classes declare with its #[Command] Attribute, and creates each one through the container only when it is used.
composer require super-kernel/commandCommands run in the application of super-kernel/framework, which needs the container and the other packages it lists; super-kernel/skeleton installs all of them. The commands are found in the metadata that skernel build generates, so a project needs SKERNEL to build it.
- Commands declared with
#[Command]on a class that extends Symfony'sCommand, with a name, a description, aliases, and a hidden flag. - Constructor injection into commands, because the container creates them.
- Lazy commands: listing commands and suggesting names create none of them.
- Rejection of empty names, invalid aliases, duplicate names, and classes that are not commands.
- The command loader installed in the application when it starts.
It does not:
- register commands declared with Symfony's
#[AsCommand]; - register methods as commands;
- create the application or run commands;
super-kernel/frameworkand Symfony Console do.
Declare a command. Save it as src/Command/HelloCommand.php:
<?php
declare(strict_types=1);
namespace Src\Command;
use SuperKernel\Command\Attribute\Command;
use Symfony\Component\Console\Command\Command as SymfonyCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
#[Command(name: 'app:hello', description: 'Print a greeting.', aliases: ['hello'])]
final class HelloCommand extends SymfonyCommand
{
protected function execute(InputInterface $input, OutputInterface $output): int
{
$output->writeln('Hello');
return self::SUCCESS;
}
}Build the project and run the command by its alias:
skernel build
php bin/bootstrap.php helloIt prints Hello.
| Name | Kind | Purpose |
|---|---|---|
SuperKernel\Command\Attribute\Command |
Attribute | Registers the annotated Symfony command under a name, with an optional description, aliases, and hidden flag. |
Symfony\Component\Console\CommandLoader\CommandLoaderInterface |
Interface | Provided by the container; loads the registered commands lazily. |
- Only
#[Command]on a class registers a command; a class with only Symfony's#[AsCommand]is not registered. - A command is created through the container when Symfony initializes it to run it, show its help, or complete it; a failure to create it is reported then, and the run ends with a non-zero status.
- An empty name, aliases that are not a list of non-empty strings, a name or alias used twice, or a class that does not extend Symfony's
CommandraisesInvalidArgumentExceptionwhen the loader is created, which failsskernel buildand the application start. Duplicates name both classes:Command name "<name>" is declared by both <class> and <class>. - Commands are found in the build metadata: run
skernel buildagain after adding, removing, or changing a command, or start withskernel run, which builds first.
The developer documentation describes the declaration rules, injecting services into commands, built-in and tool commands, and every error message: SuperKernel Command.
Released under the MIT License. See LICENSE.