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

version 1.0.0 of zend framework 2 locator generator released

I happy to announce the release of 1.0.0 of bazzlines zend framework 2 locator generator module for php.
This module should easy up the usage of the locator generator component in the zend framework 2 in a zend framework 2 application.

How ca I use it?


# generate one locator
php public/index.php locator generate 

# generate all available locators
php public/index.php locator generate

How can I install it?

with packagist


composer require net_bazzline/zf_locator_generator:dev-master

manuel


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

Zend Framework 2 - Use Own View Helpers In A Controller

Assuming you had created your view helper called "MyViewHelper", placed in in "Application\src\Application\View\Helper" and added it as "invokable" in your "module.config.php" (section "view_helpers" => "invokables").

After adding it to your "module.config.php", you can use it in your templates and everything is working as expected.
But what happens when you need that special view helper in your controller?

First idea would be to code something like the following lines in your controller.

$this->getServiceLocator()->get('viewhelpermanager')->get('MyViewHelper');

To bad, this won't work. Do you want to debug it? just add the following line at the beginning of "AbstractPluginManager::get()" and search for your "MyViewHelper" call.
echo var_export(array('name'=>$name, 'hasName' => $this->has($name), 'autoloadAddInvokableClass' => $this->autoAddInvokableClass, 'class_exists'=>class_exists($name)), true);

"class_exists" will return a false since your alias is not available at this point. How to solve this problem? Simple adapt your view helper call the following way.
$this->getServiceLocator()->get('viewhelpermanager')->get('\Application\View\Helper\MyViewHelper');