console-helpers
A collection of helpers for the Symfony Console Component., (*1)
Installation
"require": {
"ctrl/console-helpers": "~1.0@dev"
}
The Helpers
TableGeneratorHelper
Usage
Register the helper:, (*2)
/** @var \Symfony\Component\Console\Application $app */
$app->getHelperSet()->set(new \Ctrl\Console\Helper\TableGeneratorHelper());
Generate a table:, (*3)
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., (*4)
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:, (*5)
/** @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:, (*6)
public function configure()
{
$this->setDefinition([
new InputOption('to-csv', null, InputOption::VALUE_OPTIONAL, 'The CSV filename')
]);
Then, call the generator during execute
:, (*7)
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., (*8)
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., (*9)
public function execute(InputInterface $input, OutputInterface $output)
{
// ...
// Generate the CSV with a specific filename.
$csvGenerator->generate($data, $output, null, '/path/to/my.csv');
}