PHP code example of ctrl / console-helpers

1. Go to this page and download the library: Download ctrl/console-helpers library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ctrl / console-helpers example snippets


/** @var \Symfony\Component\Console\Application $app */
$app->getHelperSet()->set(new \Ctrl\Console\Helper\TableGeneratorHelper());

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

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 |
    +---+---+---+
    */
}

/** @var \Symfony\Component\Console\Application $app */
$app->getHelperSet()->set(new \Ctrl\Console\Helper\CsvGeneratorHelper());

public function configure()
{
    $this->setDefinition([
        new InputOption('to-csv', null, InputOption::VALUE_OPTIONAL, 'The CSV filename')
    ]);

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

public function execute(InputInterface $input, OutputInterface $output)
{
    // ...

    // Generate the CSV with a specific filename.
    $csvGenerator->generate($data, $output, null, '/path/to/my.csv');
}