helper functions for building a zf2 CLI app

v2.1.3 2016-07-06 12:26 UTC

This package is not auto-updated.

Last update: 2024-12-21 15:13:47 UTC


README

Build Status

helper functions for building a zf2 CLI app

Installation

Composer:

{
  "require": {
    "yalesov/zf2-cli": "2.*"
  }
}

Copy config/cli.local.php.dist into (app root)/config/autoload/cli.local.php, and edit configs as described below in Setup templates.

Usage

Get an instance of cli

Use service locator.

$cli = $locator->get('cli');
$cli = $locator->get('Yalesov\Cli\Cli');

Or just instantiate one manually.

$cli = new \Yalesov\Cli\Cli;

Templated-text

Often you need to display similarly-formatted text in a CLI app, e.g. section titles.

Setup templates

Add a template named section, which outputs a blue string ## %s ## (%s replaced by actual text).

$cli->addTemplate('section', array(
  'template' => '## %s ##',
  'color'    => 'BLUE',
));
  • template: (optional), %s as text placeholder; not set = '%s' (plain text)
  • color: (optional), a \Zend\Console\ColorInterface defined constant name; not set = default / normal color

You can also inject templates through the config file:

return array(
  'di' => array(
    'instance' => array(
      'alias' => array(
        'cli' => 'Yalesov\Cli\Cli',
      ),
      'cli' => array(
        'parameters' => array(
          'templates' => array (
            'section' => array(
              'template' => '## %s ##',
              'color'    => 'YELLOW',
            ),
            'task' => array (
              'template' => '- %s -',
              'color'    => 'BLUE',
            ),
            'module' => array(
              'template' => '[ %s ]',
              'color'    => 'GREEN',
            ),
          ),
        ),
      ),
    ),
  ),
);

This will setup the templates section, task, and module.

Output text

$cli->write('foo', 'section');

This will output the string foo using the section template. In the above example setup, this will output ## foo ##, with yellow color.

You can also capture the output string instead of printing it directly:

$string = $cli->write('foo', 'section', false);