Skip to content

Instantly share code, notes, and snippets.

@pepe84
Created July 14, 2011 08:49
Show Gist options
  • Save pepe84/1082118 to your computer and use it in GitHub Desktop.
Save pepe84/1082118 to your computer and use it in GitHub Desktop.
PHP Zend: Modules bootstrap
[production]
...
; Autoloader Options
autoloaderNamespaces[] = "My"
autoloaderNamespaces[] = "PHPTAL"
; Layout
resources.layout.layout = "default"
resources.layout.layoutPath = APPLICATION_PATH "/layouts"
; PHPTAL's configuration
parameters.phptal.destination = APPLICATION_ROOT "/data/tmp"
parameters.phptal.forceReparse = false
; Custom controller plugins to setup
resources.frontController.plugins[] = "My_Zend_Controller_Plugin_Modules"
; Modules customization
modules.admin.resources.layout.layout = "simple"
modules.web.parameters.phptal.forceReparse = true
...
<?php
/**
* Plugin to automatically init modules configuration
*
* @category Zend
* @package Zend_Controller
* @subpackage Plugin
* @author Jose Luis Jiménez Castelltort <jljc.work@gmail.com>
* @version $Id$
*/
class My_Zend_Controller_Plugin_Modules
extends Zend_Controller_Plugin_Abstract
{
public function routeShutdown(Zend_Controller_Request_Abstract $request)
{
$module = $request->getModuleName();
$boot = Zend_Controller_Front::getInstance()->getParam('bootstrap');
$opts = $boot->getOptions();
if (isset($opts['modules']) && isset($opts['modules'][$module])) {
foreach ($opts['modules'][$module] as $key => $config) {
// Initialize module configurations
$init = '_init' . ucfirst($key);
if (method_exists($this, $init)) {
$this->{$init}($boot, $config);
}
}
}
}
/**
*
* @param Zend_Application_Bootstrap_Bootstrap $boot
* @param array $config
*/
protected function _initResources($boot, $config)
{
foreach ($config as $r => $opts) {
// Get new or existing resource
$resource = $boot->getPluginResource($r);
if (null !== $resource) {
// Create or update?
if ($resource->getBootstrap() !== null) {
// Create a new resource to override initializations
$class = get_class($resource);
$new = new $class($resource->getOptions());
$new->setBootstrap($resource->getBootstrap());
$resource = $new;
}
// Merge resource options and initialize it
$resource->setOptions($opts);
$resource->init();
// Update bootstrap container
$resourceName = strtolower($r);
$boot->getContainer()->{$resourceName} = $resource;
}
}
}
/**
*
* @param Zend_Application_Bootstrap_Bootstrap $boot
* @param array $config
*/
protected function _initParameters($boot, $config)
{
// Merge parameters option
$boot->setOptions(array(
'parameters' => $config,
));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment