Download the PHP package zenstruck/console-extra without Composer
On this page you can find all versions of the php package zenstruck/console-extra. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download zenstruck/console-extra
More information about zenstruck/console-extra
Files in zenstruck/console-extra
Package console-extra
Short Description A modular set of features to reduce configuration boilerplate for your Symfony commands.
License MIT
Homepage https://github.com/zenstruck/console-extra
Informations about the package console-extra
zenstruck/console-extra
A modular set of features to reduce configuration boilerplate for your Symfony commands:
Installation
Usage
This library is a set of modular features that can be used separately or in combination.
[!NOTE] To reduce command boilerplate even further, it is recommended to create an abstract base command for your app that enables all the features you desire. Then have all your app's commands extend this.
IO
This is a helper object that extends SymfonyStyle
and implements InputInterface
(so it implements
InputInterface
, OutputInterface
, and StyleInterface
).
On its own, it isn't very special, but it can be auto-injected into Invokable
commands.
InvokableCommand
Extend this class to remove the need for extending Command::execute()
and just inject what your need
into your command's __invoke()
method. The following are parameters that can be auto-injected:
Zenstruck\Console\IO
Symfony\Component\Console\Style\StyleInterface
Symfony\Component\Console\Input\InputInterface
Symfony\Component\Console\Input\OutputInterface
- arguments (parameter name must match argument name or use the
Zenstruck\Console\Attribute\Argument
attribute) - options (parameter name must match option name or use the
Zenstruck\Console\Attribute\Option
attribute)
You can auto-inject the "raw" input/output:
No return type (or void
) implies a 0
status code. You can return an integer if you want to change this:
InvokableServiceCommand
If using the Symfony Framework, you can take InvokableCommand
to the next level by
auto-injecting services into __invoke()
. This allows your commands to behave like
Invokable Service Controllers
(with controller.service_arguments
). Instead of a Request, you inject IO
.
Have your commands extend InvokableServiceCommand
and ensure they are auto-wired/configured.
Inject with DI Attributes
You can use any
DI attribute on
your __invoke()
parameters:
Configure with Attributes
Your commands that extend InvokableServiceCommand
can configure arguments and options with attributes:
Invokable Attributes
Instead of defining at the class level, you can add the Option
/Argument
attributes directly to your
__invoke()
parameters to define and inject arguments/options:
[!NOTE] Option/Argument modes and defaults are detected from the parameter's type-hint/default value and cannot be defined on the attribute.
CommandRunner
A CommandRunner
object is available to simplify running commands anywhere (ie controller):
If the application is available, you can use it to run commands:
If your command is interactive, you can pass inputs:
By default, output is suppressed, you can optionally capture the output:
RunsCommands
You can give your Invokable Commands the ability to run other commands (defined
in the application) by using the RunsCommands
trait. These sub-commands will use the same
output as the parent command.
RunsProcesses
You can give your Invokable Commands the ability to run other processes (symfony/process
required)
by using the RunsProcesses
trait. Standard output from the process is hidden by default but can be shown by
passing -v
to the parent command. Error output is always shown. If the process fails, a \RuntimeException
is thrown.
CommandSummarySubscriber
Add this event subscriber to your Application
's event dispatcher to display a summary after every command is run.
The summary includes the duration of the command and peak memory usage.
If using Symfony, configure it as a service to enable:
[!NOTE] This will display a summary after every registered command runs.
All versions of console-extra with dependencies
symfony/console Version ^6.4|^7.0
symfony/deprecation-contracts Version ^2.2|^3.0
zenstruck/callback Version ^1.4.2