tworzenieweb/zf-bc-view-helpers

A small tool library for making B/C of ZF2 helpers to ZF1

0.0.1 2016-04-14 06:29 UTC

This package is auto-updated.

Last update: 2022-07-11 23:21:13 UTC


README

Code Climate Test Coverage Build Status Scrutinizer Code Quality

Zend Framework ViewHelpers BC Compatibility

This is a small project to help making cross-version view helpers. So you can register the same helper class for both ZF1 and ZF2 using additional getBCHelper method.

See tests for example code use

class JobleadsHelper extends AbstractBCViewHelper
{
    const HELPER_ALIAS = 'jobleads';

    /**
     * @return string
     */
    public function __invoke()
    {
        return $this->doSomeLogic();
    }

    /**
     * @return string
     */
    private function doSomeLogic()
    {
        return 'foo';
    }

    /**
     * @inheritdoc
     */
    public function getName()
    {
        return self::HELPER_ALIAS;
    }
}
$jobleadsHelper = new JobleadsHelper();

// for ZF1
$view = new Zend_View();
$name = $jobleadsHelper->getName();
$helper = $jobleadsHelper->getBCHelper();
$view->registerHelper($helper, $name);

$view->{$name}(); // will execute a helper or just $view->jobleads();

// for ZF2
$renderer = new PhpRenderer();
$helpers = new HelperPluginManager();
$helpers->setInvokableClass($name, get_class($jobleadsHelper));
$renderer->setHelperPluginManager($helpers);

$renderer->{$name}(); // will execute a helper or just $renderer->jobleads();

Of course you can use factory class for instantiating the helper and passing necessary dependancies.