consolidation/output-formatters
Flexible output formatting utilities for PHP/Laravel CLI apps. Convert structured data to clean tables, JSON, YAML, and more with consistent styling and easy configuration—ideal for Symfony Console/Drush-style commands needing polished, readable output.
Begin by installing via Composer: composer require consolidation/output-formatters. The core use case is transforming PHP arrays or objects into formatted CLI output (e.g., tables, JSON). Start with FormatterManager, register default formatters (e.g., table, json, csv), then call format($data, $format, $options). For example:
$manager = new FormatterManager();
$manager->addFormatter(new TableFormatter());
$data = [['name' => 'Alice', 'role' => 'Admin']];
echo $manager->format($data, 'table');
The /docs folder (if present in the repo) or FormatterManager source is the best first reference.
FormatterManager into Symfony Console or Robo commands; set format dynamically via --format= option (e.g., --format=json), and apply transforms via options like ['table' => ['header' => false]].FormatterInterface and calling addFormatter(). Ideal for domain-specific output (e.g., Slack blocks, Markdown tables).setFormat() to define a default format context-wide (e.g., json in API-like CLI tools), avoiding repeated format strings.symfony/property-access to flatten into arrays before formatting—avoids in-formatter reflection overhead.symfony/console’s OutputInterface to stream large datasets incrementally (though note the library lacks built-in streaming support).['json' => ['pretty_print' => true]], not ['pretty_print' => true]—a frequent source of silent misformatting.csv formatter fails on non-string scalars; cast all values explicitly ((string) $value) or sanitize in a pre-formatter transform layer.hasFormatter($name) is essential before calling format(), as invalid format names silently produce empty output or fallback to null.format()—errors may be swallowed by the manager’s try/catch block. Log internally instead.How can I help you explore Laravel packages today?