From b38a3586fd6b4b73640daf12ed4222d246823ff0 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Fri, 14 Oct 2011 17:02:43 -0500 Subject: [PATCH] Updated to use Zend\Mvc\Bootstrap - Moved view initialization to Application\Module - combined view initialization and view listener registration into a single event listener --- configs/application.config.php | 1 - modules/Application/Module.php | 44 +++++++---- modules/Application/src/Application/Bootstrap.php | 84 --------------------- public/index.php | 3 +- 4 files changed, 30 insertions(+), 102 deletions(-) delete mode 100644 modules/Application/src/Application/Bootstrap.php diff --git a/configs/application.config.php b/configs/application.config.php index e280ee8..daf929f 100644 --- a/configs/application.config.php +++ b/configs/application.config.php @@ -1,6 +1,5 @@ 'Application\Bootstrap', 'module_paths' => array( realpath(__DIR__ . '/../modules'), ), diff --git a/modules/Application/Module.php b/modules/Application/Module.php index bb8713e..df4dc5f 100644 --- a/modules/Application/Module.php +++ b/modules/Application/Module.php @@ -5,19 +5,18 @@ namespace Application; use InvalidArgumentException, Zend\Module\Manager, Zend\Config\Config, - Zend\Di\Locator, - Zend\EventManager\EventCollection, - Zend\EventManager\StaticEventCollection; + Zend\EventManager\StaticEventManager; class Module { - protected $appListeners = array(); - protected $staticListeners = array(); + protected $view; protected $viewListener; public function init(Manager $moduleManager) { $this->initAutoloader($moduleManager->getOptions()->getApplicationEnv()); + $events = StaticEventManager::getInstance(); + $events->attach('bootstrap', 'bootstrap', array($this, 'initializeView'), 100); } protected function initAutoloader($env = null) @@ -30,18 +29,15 @@ class Module return new Config(include __DIR__ . '/configs/module.config.php'); } - public function registerApplicationListeners(EventCollection $events, Locator $locator, Config $config) + public function initializeView($e) { - $view = $locator->get('view'); - $viewListener = $this->getViewListener($view, $config); - $events->attachAggregate($viewListener); - } - - public function registerStaticListeners(StaticEventCollection $events, Locator $locator, Config $config) - { - $view = $locator->get('view'); + $app = $e->getParam('application'); + $locator = $app->getLocator(); + $config = $e->getParam('modules')->getMergedConfig(); + $view = $this->getView($app); $viewListener = $this->getViewListener($view, $config); - + $app->events()->attachAggregate($viewListener); + $events = StaticEventManager::getInstance(); $viewListener->registerStaticListeners($events, $locator); } @@ -57,4 +53,22 @@ class Module $this->viewListener = $viewListener; return $viewListener; } + + protected function getView($app) + { + if ($this->view) { + return $this->view; + } + + $di = $app->getLocator(); + $view = $di->get('view'); + $url = $view->plugin('url'); + $url->setRouter($app->getRouter()); + + $view->plugin('headTitle')->setSeparator(' - ') + ->setAutoEscape(false) + ->append('Application'); + $this->view = $view; + return $view; + } } diff --git a/modules/Application/src/Application/Bootstrap.php b/modules/Application/src/Application/Bootstrap.php deleted file mode 100644 index ae73d9e..0000000 --- a/modules/Application/src/Application/Bootstrap.php +++ /dev/null @@ -1,84 +0,0 @@ -modules = $modules; - $this->config = $modules->getMergedConfig(); - } - - public function bootstrap(Application $app) - { - $this->setupLocator($app); - $this->setupRoutes($app); - $this->setupEvents($app); - } - - protected function setupLocator(Application $app) - { - $di = new Di; - $di->instanceManager()->addTypePreference('Zend\Di\Locator', $di); - - $config = new DiConfiguration($this->config->di); - $config->configure($di); - - $app->setLocator($di); - } - - protected function setupRoutes(Application $app) - { - $router = new Router(); - $router->addRoutes($this->config->routes); - $app->setRouter($router); - } - - protected function setupEvents(Application $app) - { - $view = $this->getView($app); - $locator = $app->getLocator(); - $events = $app->events(); - $staticEvents = StaticEventManager::getInstance(); - - foreach ($this->modules->getLoadedModules() as $name => $module) { - if (method_exists($module, 'registerApplicationListeners')) { - $module->registerApplicationListeners($events, $locator, $this->config); - } - - if (method_exists($module, 'registerStaticListeners')) { - $module->registerStaticListeners($staticEvents, $locator, $this->config); - } - } - } - - protected function getView($app) - { - $di = $app->getLocator(); - $view = $di->get('view'); - $url = $view->plugin('url'); - $url->setRouter($app->getRouter()); - - $view->plugin('headTitle')->setSeparator(' - ') - ->setAutoEscape(false) - ->append('Application'); - return $view; - } -} diff --git a/public/index.php b/public/index.php index cd83a90..edf3990 100644 --- a/public/index.php +++ b/public/index.php @@ -23,8 +23,7 @@ $moduleManager = new Zend\Module\Manager( ); // Create application, bootstrap, and run -$bootstrapClass = $appConfig['bootstrap_class']; -$bootstrap = new $bootstrapClass($moduleManager); +$bootstrap = new Zend\Mvc\Bootstrap($moduleManager); $application = new Zend\Mvc\Application; $bootstrap->bootstrap($application); $application->run()->send(); -- 1.7.10.4