2017 © Pedro Peláez
 

library console-helpers

A set of console helpers for the Symfony2 Console Component

image

ctrl/console-helpers

A set of console helpers for the Symfony2 Console Component

  • Monday, May 12, 2014
  • by mcrumm
  • Repository
  • 3 Watchers
  • 1 Stars
  • 2 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 6 Versions
  • 0 % Grown

The README.md

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');
}

The Versions

12/05 2014

dev-master

9999999-dev

A set of console helpers for the Symfony2 Console Component

  Sources   Download

MIT

The Requires

 

The Development Requires

12/05 2014

v0.1.3

0.1.3.0

A set of console helpers for the Symfony2 Console Component

  Sources   Download

MIT

The Requires

 

The Development Requires

12/05 2014

dev-develop

dev-develop

A set of console helpers for the Symfony2 Console Component

  Sources   Download

MIT

The Requires

 

The Development Requires

11/05 2014

v0.1.2

0.1.2.0

A set of console helpers for the Symfony2 Console Component

  Sources   Download

MIT

The Requires

 

The Development Requires

11/05 2014

v0.1.1

0.1.1.0

A set of console helpers for the Symfony2 Console Component

  Sources   Download

MIT

The Requires

 

The Development Requires

10/05 2014

v0.1.0

0.1.0.0

A set of console helpers for the Symfony2 Console Component

  Sources   Download

MIT

The Requires

 

The Development Requires