plumphp/plum-console

Integrates the Symfony Console component into Plum.

v0.5 2016-03-01 12:59 UTC

This package is auto-updated.

Last update: 2024-10-20 09:58:08 UTC


README

Plum

PlumConsole integrates the Symfony Console component into Plum. Plum is a data processing pipeline for PHP.

Build Status Windows Build status Scrutinizer Code Quality Code Coverage StyleCI

Developed by Florian Eckerstorfer in Vienna, Europe.

Installation

You can install Plum using Composer.

$ composer require plumphp/plum-console

Usage

Please refer to the Plum documentation for more information about Plum in general.

PlumConsole currently contains two writers: ConsoleProgressWriter and ConsoleTableWriter. Both are intended to be used in an application that use the Symfony Console component. In addition it provides ExceptionFormatter, which helps you printing nice error messages to users.

ConsoleProgressWriter

Plum\PlumConsole\ConsoleProgressWriter displays the progress of a workflow in the console.

use Plum\PlumConsole\ConsoleProgressWriter;
use Symfony\Component\Console\Helper\ProgressBar;

// ...
// $output is an instance of Symfony\Component\Console\Output\OutputInterface
// $reader is an instance of Plum\Plum\Reader\ReaderInterface

$writer = new ConsoleProgressWriter(new ProgressBar($output, $reader->count()));

ConsoleTableWriter

Plum\PlumConsole\ConsoleTableWriter outputs the processed data as table in the console.

use Plum\PlumConsole\ConsoleTableWriter;
use Symfony\Component\Console\Helper\Table;

// ...
// $output is an instance of Symfony\Component\Console\Output\OutputInterface

$writer = new ConsoleTableWriter(new Table($output));

// ConsoleTableWriter can automatically detect and set the headers
$writer->autoDetectHeader();

// Headers can also be set manually. This is required when the item is an object
$writer->setHeader(['Country', 'City']);

ExceptionFormatter

Plum does offer the option to catch exceptions. When this option is active the workflow can resume processing even if an item is causing errors. However, you have to manually output exceptions, which can be a tedious process. Plum\PlumConsole\ExceptionFormatter can help you printing exceptions.

The granularity of the information can be controlled using the --verbose flag of Symfony Console. By default, the exception messages will be printed when the application is invoked using --verbose or -v and the messages and stack traces are printed when using -vv.

use Plum\Plum\Workflow;
use Plum\PlumConsole\ExceptionFormatter;

$workflow = Workflow(['resumeOnError' => true]);
// Build workflow
$result = $workflow->process($reader);

// $output is an instance of Symfony\Component\Console\Output\OutputInterface

$formatter = new ExceptionFormatter($output);
$formatter->outputExceptions($result);

The formatter offers options to configure both the granularity when messages and stack traces are shown and let you configure how they are printed. The following example shows all available options and the default values. Please note, that messageTemplate and traceTemplate are being printed using sprintf().

use Plum\PlumConsole\ExceptionFormatter;
use Symfony\Component\Console\Output\OutputInterface;

// $output is an instance of Symfony\Component\Console\Output\OutputInterface

$formatter = new ExceptionFormatter($output, [
    'minMessageVerbosity' => OutputInterface::VERBOSITY_VERBOSE,
    'minTraceVerbosity'   => OutputInterface::VERBOSITY_VERY_VERBOSE,
    'messageTemplate'     => '<error>%s</error>',
    'traceTemplate'       => '%s',
]);

Change Log

Version 0.5 (1 March 2016)

  • Add support for Symfony 3

Version 0.4 (24 October 2015)

  • #2 Add helper to print nice exception messages
  • #3 ConsoleTableWriter can now write object items

Version 0.3 (15 May 2015)

  • Add ConsoleTableWriter

Version 0.2.1 (28 April 2015)

  • Fix Plum version

Version 0.2 (21 April 2015)

  • Fix dependency to Plum

Version 0.1 (24 March 2014)

  • Initial release
  • Works with Plum v0.1

License

The MIT license applies to plumphp/plum-console. For the full copyright and license information, please view the LICENSE file distributed with this source code.