Custom Module Bootstrap Class

In my Zend Framework application I wanted a bootstrap class for every module. Each bootstrap class will instantiate confirguration specific for each module. You do not have to add a bootstrap class to a module if no confirguration is required for that module.

I first looked at Zend_Application_Resource_Modules, but it loads all the bootstrap files for each module in my application. This is not what I wanted to achive.

Here is my attempt to achieve my goal

Create a controller plugin. I called it Moo_Controller_Plugin_Init

/**
 *
 * @copyright  2009 Mohammed Alsharaf
 * @author      Mohamed Alsharaf 
 * @category   Moo
 * @package    Moo_Controller
 * @copyright  Copyright (c) 2009-2010 Mohammed Alsharaf.
 * @license      http://framework.zend.com/license/new-bsd
 * @version      Release: 0.0.2
 * @link           http://jamandcheese-on-phptoast.com/
 */
class Moo_Controller_Plugin_Init extends Zend_Controller_Plugin_Abstract
{
	public function preDispatch(
            Zend_Controller_Request_Abstract $request) 
    {
            // include bootstrap if it's exists
            $bootstrap = APPLICATION_PATH  . 'modules/' . $module . '/Bootstrap.php';
            if(!file_exists($bootstrap)) {
                return false;
            }
            require_once $bootstrap;

            // format module name
            $module = $this->_formatModuleName($module);
            // instantiate bootstrap and pass any object/data you want
            $class = $module . '_Bootstrap';
            $moduleBootstrap = new $class();
            $moduleBootstrap->setRequest($this->getRequest())
            ->setResponse($this->getResponse())
            ->init();
	}
    protected function _formateModuleName($module) 
    { 
            if(strpos($module, '-') !== false) {
                $filter = new Zend_Filter_Word_DashToCamelCase();
                $module = $filter->filter($module);
            } elseif(strpos($module, '_') !== false) {
                $filter = new Zend_Filter_Word_UnderscoreToCamelCase();
                $module = $filter->filter($module);
            } else {
                $module = ucfirst($module);
            }
            return $module;
    }
}

Register the controller plugin in front controller. This will depend on how you have the configuration setup. You can either add the plugin from the application.ini file by adding

resources.frontController.plugins.init = "Moo_Controller_Plugin_Init"

Or by calling the method “registerPlugin” in Zend_Controller_Front.

$front = Zend_Controller_Front::getInstance();
$front->registerPlugin(new Moo_Controller_Plugin_Init, 1);

Each module bootstrap class will extend an abstract class for common methods.

/**
 *
 * @copyright  2009 Mohammed Alsharaf
 * @author     Mohamed Alsharaf 
 * @category   Moo
 * @package    Moo_Application
 * @copyright  Copyright (c) 2009-2010 Mohammed Alsharaf.
 * @license    http://framework.zend.com/license/new-bsd
 * @version    Release: 0.0.1
 * @link       http://jamandcheese-on-phptoast.com/
 */
abstract class Moo_Application_Module_BootstrapAbstract
{
    /**
     * @var Zend_Controller_Request_Abstract
     */
    protected $_request;

    /**
     * @var Zend_Controller_Response_Abstract
     */
    protected $_response;

    /**
     * Set request object
     *
     * @param Zend_Controller_Request_Abstract $request
     * @return Moo_Application_Module_BootstrapAbstract
     */
    public function setRequest(Zend_Controller_Request_Abstract $request)
    {
        $this->_request = $request;
        return $this;
    }

    /**
     * Get request object
     *
     * @return Zend_Controller_Request_Abstract $request
     */
    public function getRequest()
    {
        return $this->_request;
    }

    /**
     * Set response object
     *
     * @param Zend_Controller_Response_Abstract $response
     * @return Moo_Application_Module_BootstrapAbstract
     */
    public function setResponse(Zend_Controller_Response_Abstract $response)
    {
        $this->_response = $response;
        return $this;
    }

    /**
     * Get response object
     *
     * @return Zend_Controller_Response_Abstract $response
     */
    public function getResponse()
    {
        return $this->_response;
    }
}

The bootstrap class looks like

/**
 *
 * @copyright  2009 Mohammed Alsharaf
 * @author     Mohamed Alsharaf
 * @copyright  Copyright (c) 2009-2010 Mohammed Alsharaf.
 * @license    http://framework.zend.com/license/new-bsd
 * @version    Release: 0.0.1
 * @link       http://jamandcheese-on-phptoast.com/
 */
class Admin_Bootstrap extends Moo_Application_Module_BootstrapAbstract
{

	public function init()
	{
		// add your code here.
	}
}

Any comments appreciated

Leave A Reply

Comments

Cyril (May 04, 2010)

You does not show your class “Moo_Options” and two line after you write :
require_once $coreBootstrap; and i think it is
$bootstrap

mohammed (May 04, 2010)

Thanks Cyril

I have fixed the code.

Tags