ctrl / console-helpers
A set of console helpers for the Symfony2 Console Component
Requires
- php: >=5.4.0
- league/csv: 5.*
- symfony/filesystem: ~2.4
Requires (Dev)
- symfony/console: ~2.4
This package is not auto-updated.
Last update: 2024-10-26 15:30:51 UTC
README
A collection of helpers for the Symfony Console Component.
Installation
"require": { "ctrl/console-helpers": "~1.0@dev" }
The Helpers
TableGeneratorHelper
Usage
Register the helper:
/** @var \Symfony\Component\Console\Application $app */ $app->getHelperSet()->set(new \Ctrl\Console\Helper\TableGeneratorHelper());
Generate a table:
public function execute(InputInterface $input, OutputInterface $output) { // Retrieve the Helper from the HelperSet $tableGenerator = $this->getHelperSet()->get('table_generator'); /** @var \Traversable $data */ $data = getATraversable(); // Pass $output to the Generator to render the table immediately. $tableGenerator->generate($data, $output); // Or, don't, and the generator will return the table instead. $table = $tableGenerator->generate($data); $table->render($output); }
Mapper
You can pass a callable
in the generator's 3rd argument to map each row before adding it to the table.
public function execute(InputInterface $input, OutputInterface $output) { // ... $mapper = function($row) { return [ 'a', 'b', 'c' ]; }; // Apply the mapper to the results $tableGenerator->generate($data, $output, $mapper); /* Output: +---+---+---+ | 0 | 1 | 2 | +---+---+---+ | a | b | c | | a | b | c | | a | b | c | | a | b | c | +---+---+---+ */ }
CsvGeneratorHelper
Usage
Register the helper:
/** @var \Symfony\Component\Console\Application $app */ $app->getHelperSet()->set(new \Ctrl\Console\Helper\CsvGeneratorHelper());
Configure a to-csv
option in your command Definition to provide the csv filename as an argument:
public function configure() { $this->setDefinition([ new InputOption('to-csv', null, InputOption::VALUE_OPTIONAL, 'The CSV filename') ]);
Then, call the generator during execute
:
public function execute(InputInterface $input, OutputInterface $output) { // Retrieve the Helper from the HelperSet $csvGenerator = $this->getHelperSet()->get('csv_generator'); /** @var \Traversable $data */ $data = getATraversable(); // Generate the CSV. The user will be prompted for the filename if it has not yet been provided. $csvGenerator->generate($data, $output); }
Mapper
The CsvGeneratorHelper
also includes the ability to apply a map to each row before being inserted. Check the Mapper section for the TableGeneratorHelper
for more details.
Filename
If you want to specify a filename at generation time, or if you are working in a non-interactive mode, you can pass
the filename as the 4th parameter of the generate
method.
public function execute(InputInterface $input, OutputInterface $output) { // ... // Generate the CSV with a specific filename. $csvGenerator->generate($data, $output, null, '/path/to/my.csv'); }