Console macros - markstory/cakephp GitHub Wiki

Creating reusable blocks of console output code is not easy right now, and with a few improvements we can provide a simple, extensible API for building console output macros. CLImate is a good example of how I think this could be approached.

Console Macro classes

Console macros would be simple classes that implement an output method. The arguments to this method would depend on the macro object, and no interface would be enforced. Macro classes would be accessed from the ConsoleIo instance that all shells have access to:

// In a shell or task
$this->_io->macro('table', $headers, $rows);

Under the hood the macro does the following:

  • Uses the ConsoleMacroRegistry to locate the macro class.
  • The class of the macro would be expected to be App\Shell\Macro\${name}Macro.
  • An instance would be created with the current ConsoleIo instance and added to the registry.
  • The instance's output() method would be called.

Built in Macros

We could provide a small selection of macros to start with and expand as the community needs more. To start with I propose:

  • Table - Create well formatted tables. This is already needed in the RouteShell.
  • Progress - Create single line progress bars.

Macro Conventions

  • Macros would live in App\Shell\Macro and have the Macro suffix.
  • Macros need to implement output($args)
  • Macros get the ConsoleIo instance in their constructor.