Skip to content

version 1.0.0 of zend framework 2 console helper released

I happy to announce the release of 1.0.0 of bazzlines zend framework 2 console helper module for php.
This module should easy up implementing console commands supporting POSIX Signal Handling.
Furthermore, there are some simple but useful methods implemented:

  • getConsole()
  • getParameter($name)
  • getRequest()
  • hasBooleanParameter($shortName = '', $longName = '')
  • hasParameter($name)
  • throwExceptionIfNotCalledInsideAnCliEnvironment()

How can I use it?


namespace MyModule\Controller\Console;

use Exception;

class IndexController extends AbstractConsoleController
{
    public function indexAction()
    {
        try {
            $this->throwExceptionIfNotCalledInsideAnCliEnvironment();

            $this->attachSignalHandler($this);

            //some example items
            //  simple think about a lot of items that indicates longer
            //  processing runtime
            $items = array('one', 'two', 'three', 'four');

            //use implemented method to react on signal handling
            $this->processItems(
                $items,             //big list of items
                $this,              //current object
                'processItem',      //method that should be called for each item
                $arguments = array( //additional arguments for method 'processItem' (if needed)
                    'foo',
                    'bar'
                )
            );
        } catch (Exception $exception) {
            $this->handleException($exception);
        }
    }

    /**
     * must be protected since it will be called from the parent
     *
     * @param string $item
     * @param string $stringOne
     * @param string $stringTwo
     */
    protected function processItem($item, $stringOne, $stringTwo)
    {
        $console = $this->getConsole();
        $console->writeLine(
            'this is item "' . $item .
            '" with string one "' . $stringOne . '"' .
            '" and string two "' . $stringTwo . '"'
        );
    }

    /**
     * @return boolean
     */
    private function beVerbose()
    {
        return $this->hasBooleanParameter('v', 'verbose');
    }
}

How can I install it?

with packagist


composer require net_bazzline/zf_console_helper:dev-master

manuel


mkdir -p vendor/net_bazzline/zf_console_helper
cd vendor/net_bazzline/zf_console_helper
git clone https://github.com/zf_console_helper

Process Fork Manager for PHP

I'm happy to announce the release of version 1.0.0 from the componen Process Fork Manager for PHP.

What is it good for?

  • Provides OOP style for creating and observing parallel tasks
  • Comes with a TaskInterface to easy up implementing own tasks
  • Comes with thread, memory and time limit management
  • Can be extended by using the build in event dispatcher
  • Is shipped with a lot of examples
  • Supports POSIX signal handling

How can i use it?


$factory = \Net\Bazzline\Component\ProcessForkManager\ForkManagerFactory();
$manager = $factory->create();

/** @var \Net\Bazzline\Component\ProcessForkManager\TaskInterface $task */
$task = new \My\Task();

$manager->addTask($task);
$manager->execute();

How can i install it?

Manuel


mkdir -p vendor/net_bazzline/php_component_process_fork_manager
cd vendor/net_bazzline/php_component_process_fork_manager
git clone https://github.com/bazzline/php_component_process_fork_manager

With packagist


"net_bazzline/php_component_process_fork_manager": "1.0.0"

POSIX signal handling in a php zend framework 2 console controller action call

I played around with php console applications for a while now. The projects are getting bigger and sometimes, the load on the system or the runtime itself increases a lot.
At night, it made "click" and I asked myself "Are you nuts or why aren't you simple using POSIX signals for your php console applications (like normal people are doing it)?". Thanks to the book Signaling PHP, I could "kickstart" into the topic and created a simple example script.

While solving this problem and migrating some own scripts, I wanted to implement the signal handling in a bigger zend framework 2 project.
Quickly done, I copy/pasted/adapted my example code and thought "this is it". To bad, I need to take one pitfall. I could not debug it right know, but zf2 needs to have the "signal handler method" public. The following code is the final outcome of this adaptation session.


class MyController extends AbstractCliActionController
{
    //AbstractCliActionController extends zend AbstractActionController
    //...usefull controller code
    /**
     * @param AbstractCliActionController $object
     * @param string $method
     */
    protected function attachSignalHandler(AbstractCliActionController $object, $method = 'defaultSignalHandler')
    {
        declare(ticks = 10);

        pcntl_signal(SIGHUP,    array($object, $method));
        pcntl_signal(SIGINT,    array($object, $method));
        pcntl_signal(SIGUSR1,   array($object, $method));
        pcntl_signal(SIGUSR2,   array($object, $method));
        pcntl_signal(SIGQUIT,   array($object, $method));
        pcntl_signal(SIGILL,    array($object, $method));
        pcntl_signal(SIGABRT,   array($object, $method));
        pcntl_signal(SIGFPE,    array($object, $method));
        pcntl_signal(SIGSEGV,   array($object, $method));
        pcntl_signal(SIGPIPE,   array($object, $method));
        pcntl_signal(SIGALRM,   array($object, $method));
        pcntl_signal(SIGCONT,   array($object, $method));
        pcntl_signal(SIGTSTP,   array($object, $method));
        pcntl_signal(SIGTTIN,   array($object, $method));
        pcntl_signal(SIGTTOU,   array($object, $method));
    }

    /**
     * @param $signal
     */
    public function defaultSignalHandler($signal)
    {
        echo 'caught signal "' . $signal . '"' . PHP_EOL;
    }
}

You can easily extend the method "defaultSignalHandler" to whatever you need (push something to a log, flip a flag to stop execution etc) and thats it. Injoy your work :-).

Thanks to the nice irc freenode channel #zftalk, I can provide a second implementation to prove my example to be right.