Oihana PHP Commands

PassphraseTrait uses trait:short

Trait to manage a passphrase for console commands.

Provides methods to handle an internal passphrase value, including prompting the user interactively if the passphrase is not provided and optionally throwing an exception if the passphrase is missing.

This trait requires the IOTrait to provide SymfonyStyle I/O helpers.

Usage Example

use oihana\commands\traits\PassphraseTrait;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class MyCommand extends Command
{
    use PassphraseTrait;

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        try
        {
            $passphrase = $this->getPassPhrase($input, $output) ;
        }
        catch ( MissingPassphraseException $e )
        {
            $output->writeln('<error>' . $e->getMessage() . '</error>');
            return 1 ;
        }

        // Use $passphrase for secured operations...

        return 0;
    }
}
Tags
author

Marc Alcaraz

since
1.0.0

Table of Contents

Properties

$io  : SymfonyStyle|null
The command writer.
$passphrase  : string|null
The internal passphrase value.

Methods

batchIOActions()  : void
Executes a batch of IO-aware callback actions using `runIOAction()`.
getIO()  : SymfonyStyle
Returns the IO writer reference.
initializePassphrase()  : void
Initialize the internal passphrase value.
runAction()  : mixed
Executes a callback within a styled Symfony Console context (IO).
runIOAction()  : mixed
Executes a callback within a styled Symfony Console context (IO).
getPassPhrase()  : string|null
Returns the passphrase. if the command is interactive a question is asked to the command user to prompt the passphrase.

Properties

$io

The command writer.

public SymfonyStyle|null $io = null

$passphrase

The internal passphrase value.

protected string|null $passphrase = 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): required
  • title (string): optional
  • finish (string): optional
  • endLines (int): optional, defaults to 2
$io : SymfonyStyle|null = null

Optional SymfonyStyle instance for styled output.

$numbering : bool = false
Tags
example
$this->batchIOActions
([
    [
        'callback'  => fn(SymfonyStyle $io) => $this->initializeDb($io),
        'title'     => 'Step 1: Database Initialization',
        'finish'    => '✅ Done.',
        'endLines'  => 1,
    ],
    [
        'callback'  => fn(SymfonyStyle $io) => $this->migrateData($io),
        'title'     => 'Step 2: Data Migration',
    ],
]);

getIO()

Returns the IO writer reference.

public getIO(InputInterface $input, OutputInterface $output) : SymfonyStyle
Parameters
$input : InputInterface
$output : OutputInterface
Return values
SymfonyStyle

initializePassphrase()

Initialize the internal passphrase value.

public initializePassphrase([array<string|int, mixed> $init = [] ]) : void
Parameters
$init : array<string|int, mixed> = []

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
example
$this->runAction
(
    callback : fn( ?SymfonyStyle $io ) => $this->wordPressInitializeDatabase( verbose:$verbose ) ,
    input    : $input ,
    output   : $output ,
    title    : '01. Initialize the Mysql Database' ,
    finish   : '🗄️ The WordPress database is ready.'
);
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
example
$this->runIOAction(
    callback: fn( ?SymfonyStyle $io) => $this->wordPressInitializeDatabase(verbose: $verbose),
    io: $io,
    title: '01. Initialize the Mysql Database',
    finish: '🗄️ The WordPress database is ready.'
);
Return values
mixed

The return value from the callback.

getPassPhrase()

Returns the passphrase. if the command is interactive a question is asked to the command user to prompt the passphrase.

protected getPassPhrase(InputInterface $input, OutputInterface $output[, bool $throwable = true ]) : string|null
Parameters
$input : InputInterface
$output : OutputInterface
$throwable : bool = true

Indicates if the method throws a MissingPassphraseException if the passphrase is null.

Tags
throws
MissingPassphraseException
Return values
string|null

Returns the passphrase.


        
On this page

Search results