UITrait uses trait:short
Provides utilities for building interactive UI elements like progress bars in Symfony Console-based applications.
Tags
Table of Contents
Properties
- $io : SymfonyStyle|null
- The command writer.
Methods
- batchIOActions() : void
- Executes a batch of IO-aware callback actions using `runIOAction()`.
- createProgressBar() : ProgressBar
- Creates and configures a Symfony Console ProgressBar.
- createProgressBarFromIO() : ProgressBar
- Creates a progress bar using a SymfonyStyle instance.
- getIO() : SymfonyStyle
- Returns the IO writer reference.
- initializeProgressBar() : static
- Applies configuration to a ProgressBar instance.
- progressBarIterator() : Generator<string|int, T>
- Iterate over an iterable collection with a progress bar and optional per-item messages.
- 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
createProgressBar()
Creates and configures a Symfony Console ProgressBar.
public
createProgressBar(OutputInterface $output[, int $max = 0 ][, string $format = 'pretty' ][, int $barWidth = 50 ][, array<string|int, mixed>|null $formatDefinitions = null ]) : ProgressBar
Parameters
- $output : OutputInterface
-
The output interface (usually from a command).
- $max : int = 0
-
The maximum number of steps (0 for indeterminate).
- $format : string = 'pretty'
-
The format name to use (e.g., 'pretty').
- $barWidth : int = 50
-
The width of the progress bar.
- $formatDefinitions : array<string|int, mixed>|null = null
-
Custom format definitions [name => format string].
Return values
ProgressBar —The initialized progress bar instance.
createProgressBarFromIO()
Creates a progress bar using a SymfonyStyle instance.
public
createProgressBarFromIO(SymfonyStyle $io[, int $max = 0 ][, string $format = 'pretty' ][, int $barWidth = 50 ][, array<string|int, mixed>|null $formatDefinitions = null ]) : ProgressBar
Parameters
- $io : SymfonyStyle
-
The SymfonyStyle instance.
- $max : int = 0
-
The maximum number of steps.
- $format : string = 'pretty'
-
The format name to use.
- $barWidth : int = 50
-
The width of the progress bar.
- $formatDefinitions : array<string|int, mixed>|null = null
-
Custom format definitions [name => format string].
Return values
ProgressBar —The initialized progress bar instance.
getIO()
Returns the IO writer reference.
public
getIO(InputInterface $input, OutputInterface $output) : SymfonyStyle
Parameters
- $input : InputInterface
- $output : OutputInterface
Return values
SymfonyStyleinitializeProgressBar()
Applies configuration to a ProgressBar instance.
public
initializeProgressBar(ProgressBar $progressBar[, string $format = 'pretty' ][, int $barWidth = 50 ][, array<string|int, mixed>|null $formatDefinitions = null ]) : static
Parameters
- $progressBar : ProgressBar
-
The progress bar to initialize.
- $format : string = 'pretty'
-
The format name to use (e.g., 'pretty').
- $barWidth : int = 50
-
The width of the progress bar.
- $formatDefinitions : array<string|int, mixed>|null = null
-
Optional array of custom format definitions.
Return values
static —The current trait instance (for method chaining).
progressBarIterator()
Iterate over an iterable collection with a progress bar and optional per-item messages.
public
progressBarIterator(ProgressBar|null $progressBar, iterable<string|int, T> $items[, callable(T): string|string|null $message = null ][, string|null $start = null ][, string|null $finish = null ]) : Generator<string|int, T>
Example usage:
$iterator = $this->progressBarIterator( $progressBar , $items , fn($item) => "Processing {$item->name}") ;
foreach ( $iterator as $item)
{
// Process $item here
}
Parameters
- $progressBar : ProgressBar|null
-
The progress bar reference.
- $items : iterable<string|int, T>
-
Iterable collection of items.
- $message : callable(T): string|string|null = null
-
Optional callback to generate message per item: fn(T): string.
- $start : string|null = null
-
The message when the iteration is starting.
- $finish : string|null = null
-
The message when the iteration is ending.
Return values
Generator<string|int, T> —Generator yielding each item.
runAction()
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.