ZF-Console: PHP microframework for console applications

by Mateusz Tymek — on Zend Framework, PHP

As you may know, folks from ZF2/Apigility projects keep bringing interesting utility modules, enriching ZF2 ecosystem. Today I discovered small framework/helper for writing console applications: ZF-Console. And, to my surprise, it was based on my own pull request from last year! I was really proud to see that I brought something useful to the community.

Creating console application

At the bare minimum you will need single file defining console routes and actions to be executed. If you are familiar with Silex or other PHP microframeworks, you'll feel like home. Here's an example application that will print "Hello, World!" message, allowing optional parameter to be passed and printed instead of "World":

use Zend\Console\Adapter\AdapterInterface;
use Zend\Console\ColorInterface;
use Zend\Console\Console;
use ZF\Console\Application;
use ZF\Console\Route;

$application = new Application(
"ZF-Console Skeleton",     // Application name
"1.0",                     // Application version
[                          // Route definition
    [   // First route
    // Example usage: $ php app.php hello Mat
    'name' => 'hello [<name>]',
    'description' => 'Show welcome message',
    'short_description' => 'Show welcome message',
    'defaults' => [
        'name' => 'World',
    ],
    'handler' => function(Route $route, AdapterInterface $console) {
        $name = $route->getMatchedParam('name');
        $console->write("Hello, ");
        $console->write("$name", ColorInterface::LIGHT_GREEN);
        $console->writeLine("!");
    },
    ]
],
Console::getInstance()
);

$exit = $application->run();
exit($exit);

Try to run it by typping in console:

$ php app.php

By default ZF-Console will print usage information, and provide a few handy commands:

ZF-Console Skeleton, version 1.0

Available commands:

autocomplete   Command autocompletion setup
hello [<name>] Show welcome message
help           Get help for individual commands
version        Display the version of the script
Now try using "hello" command:
$ php app.php hello Mat
Hello, Mat!
Cool, isn't it?

ZF-Console Skeleton Application

While putting everything in single file is nice if you want to quickly prototype something, it will quickly become one big mess in real life application. In order to clean it up (and make it testable), we'll split it into more files.

handler parameter from route definition can be anything callable, which allows you to put controller code in a separate class.

I suggest the following structure:

bin/app.php - entry point / "front controller":

use Zend\Console\Console;
use ZF\Console\Application;

chdir(dirname(__DIR__));

include 'vendor/autoload.php';

$application = new Application(
	"ZF-Console Skeleton",
	"1.0",
	include 'config/routes.php',
	Console::getInstance()
);

$exit = $application->run();
exit($exit);

config/routes.php - route definition:

return [
  [
  'name' => 'hello [<name>]',
  'description' => 'Show welcome message',
  'short_description' => 'Show welcome message',
  'defaults' => [
      'name' => 'World',
  ],
  'handler' => 'Application\Command\Hello',
  ]
];

src/Application/Command/Hello.php - controller:

namespace Application\Command;

use Zend\Console\Adapter\AdapterInterface;
use Zend\Console\ColorInterface;
use ZF\Console\Route;

class Hello
{
	public function __invoke(Route $route, AdapterInterface $console)
	{
	    $name = $route->getMatchedParam('name');
	    $console->write("Hello, ");
	    $console->write("$name", ColorInterface::LIGHT_GREEN);
	    $console->writeLine("!");
	}
}

As you see, ZF-Console is easy to use and still allows you to unleash full power of ZF2 console routes. Besides what was mentioned here, it supports filters and validators, provides easy way to enable shell autocompletion, handles exceptions, and more. This makes it a good alternative for Symfony's Console, that better integrates with ZF2 ecosystem.

Example on Github

I prepared simple skeleton that may be used as a base for ZF-Console applications. You can find it on Github - just click here.

See also:


comments powered by Disqus