IOTrait
Represents a terminal command within the Symfony application.
Tags
Table of Contents
Properties
- $io : SymfonyStyle|null
- The command writer.
Methods
- batchIOActions() : void
- Executes a batch of IO-aware callback actions using `runIOAction()`.
- getIO() : SymfonyStyle
- Returns the IO writer reference.
- runAction() : mixed
- Executes a callback within a styled Symfony Console context (IO).
- runIOAction() : mixed
- Executes a callback within a styled Symfony Console context (IO).
Properties
$io
The command writer.
public
SymfonyStyle|null
$io
= null
Methods
batchIOActions()
Executes a batch of IO-aware callback actions using `runIOAction()`.
public
batchIOActions([array<string|int, mixed> $actions = [] ][, SymfonyStyle|null $io = null ][, bool $numbering = false ]) : void
This method takes an array of associative arrays (actions), each containing:
callback
(callable, required): the function to execute.title
(string, optional): a section title to print before the callback.finish
(string, optional): a message to print after the callback completes.endLines
(int, optional): number of newlines to append at the end (default: 2).
The provided $io
instance is passed to all actions for styled output.
If no callback
is provided in an action, that entry is skipped.
Parameters
- $actions : array<string|int, mixed> = []
-
An array of actions to run, each as an associative array with keys:
callback
(callable): requiredtitle
(string): optionalfinish
(string): optionalendLines
(int): optional, defaults to 2
- $io : SymfonyStyle|null = null
-
Optional SymfonyStyle instance for styled output.
- $numbering : bool = false
Tags
getIO()
Returns the IO writer reference.
public
getIO(InputInterface $input, OutputInterface $output) : SymfonyStyle
Parameters
- $input : InputInterface
- $output : OutputInterface
Return values
SymfonyStylerunAction()
Executes a callback within a styled Symfony Console context (IO).
public
runAction(callable $callback[, InputInterface|null $input = null ][, OutputInterface|null $output = null ][, string $title = '' ][, string $finish = '' ][, int $endLines = 1 ][, callable|null $getIO = null ]) : mixed
This method ensures consistent output formatting by wrapping the given callback in a SymfonyStyle context.
If both $input
and $output
are provided, it automatically
creates or retrieves a SymfonyStyle instance using either the internal getIO()
method
or a custom $getIO
callable. It optionally renders a section title before execution,
a closing message after execution, and appends a number of newlines at the end.
If $input
or $output
are null, the method simply invokes the callback without styling.
Parameters
- $callback : callable
-
The main logic to execute. If IO is available, the callable should accept a SymfonyStyle argument.
- $input : InputInterface|null = null
-
Optional Symfony Console input interface.
- $output : OutputInterface|null = null
-
Optional Symfony Console output interface.
- $title : string = ''
-
Optional title printed as a section header before executing the callback.
- $finish : string = ''
-
Optional closing message printed after executing the callback.
- $endLines : int = 1
-
Number of new lines to append after the action (default: 2).
- $getIO : callable|null = null
-
Optional callable to retrieve a custom SymfonyStyle instance (receives $input, $output).
Tags
Return values
mixed —The return value from the callback.
runIOAction()
Executes a callback within a styled Symfony Console context (IO).
public
runIOAction(callable $callback[, SymfonyStyle|null $io = null ][, string $title = '' ][, string $finish = '' ][, int $endLines = 2 ]) : mixed
This method runs the given callback, passing the provided SymfonyStyle
instance
to it for consistent console output formatting. It optionally displays a section title
before executing the callback and a finishing message afterward.
Finally, it appends a specified number of new lines.
If no title is given, no section header is printed. If no finish message is given, no closing message is printed.
Parameters
- $callback : callable
-
The callable to execute. Receives the
SymfonyStyle
instance as an argument. - $io : SymfonyStyle|null = null
-
The
SymfonyStyle
instance used for console output. - $title : string = ''
-
Optional section title displayed before running the callback.
- $finish : string = ''
-
Optional finishing message displayed after the callback completes.
- $endLines : int = 2
-
Number of new lines to append after the finishing message (default is 2).
Tags
Return values
mixed —The return value from the callback.