class My_ResourceInjector extends Zend_Controller_Action_Helper_Abstract
{
protected $_resources;
public function __construct(array $resources = array())
{
$ this ->_resources = $resources;
}
public function preDispatch()
{
$bootstrap = $ this ->getBootstrap();
$controller = $ this ->getActionController();
foreach ($ this ->_resources as $name) {
if ($bootstrap->hasResource($name)) {
$controller->$name = $bootstrap->getResource($name);
}
}
}
public function getBootstrap()
{
return $ this ->getFrontController()->getParam( 'bootstrap' );
}
}
* This source code was highlighted with Source Code Highlighter .
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initResourceInjector()
{
Zend_Controller_Action_HelperBroker::addHelper(
new My_ResourceInjector(array(
'db' ,
'layout' ,
'navigation' ,
));
);
}
}
* This source code was highlighted with Source Code Highlighter .
class FooController extends Zend_Controller_Action
{
public function barAction()
{
$ this ->layout->disableLayout();
$model = $ this ->getModel();
$model->setDbAdapter($ this ->db);
$ this ->view->assign(
'model' => $ this ->model,
'navigation' => $ this ->navigation,
);
}
// ...
}
* This source code was highlighted with Source Code Highlighter .
class My_ResourceInjector extends Zend_Controller_Action_Helper_Abstract
{
protected $_resources;
public function preDispatch()
{
$bootstrap = $ this ->getBootstrap();
$controller = $ this ->getActionController();
if (!isset($controller->dependencies)
|| !is_array($controller->dependencies)
) {
return ;
}
foreach ($controller->dependencies as $name) {
if ($bootstrap->hasResource($name)) {
$controller->$name = $bootstrap->getResource($name);
}
}
}
public function getBootstrap()
{
return $ this ->getFrontController()->getParam( 'bootstrap' );
}
}
* This source code was highlighted with Source Code Highlighter .
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initResourceInjector()
{
Zend_Controller_Action_HelperBroker::addHelper(
new My_ResourceInjector();
);
}
}
* This source code was highlighted with Source Code Highlighter .
class FooController extends Zend_Controller_Action
{
public $dependencies = array(
'db' ,
'layout' ,
'navigation' ,
);
public function barAction()
{
$ this ->layout->disableLayout();
$model = $ this ->getModel();
$model->setDbAdapter($ this ->db);
$ this ->view->assign(
'model' => $ this ->model,
'navigation' => $ this ->navigation,
);
}
// ...
}
* This source code was highlighted with Source Code Highlighter .
class My_ResourceInjector extends Zend_Controller_Action_Helper_Abstract
{
protected $_resources;
public function preDispatch()
{
$bootstrap = $ this ->getBootstrap();
$controller = $ this ->getActionController();
if (!isset($controller->dependencies)
|| !is_array($controller->dependencies)
) {
return ;
}
foreach ($controller->dependencies as $name) {
if (!$bootstrap->hasResource($name)) {
throw new DomainException( "Unable to find dependency by name '$name'" );
}
$controller->$name = $bootstrap->getResource($name);
}
}
public function getBootstrap()
{
return $ this ->getFrontController()->getParam( 'bootstrap' );
}
}
* This source code was highlighted with Source Code Highlighter .
Source: https://habr.com/ru/post/88426/