Updated to use Zend\Mvc\Bootstrap
[zf2.biz/galerie.git] / modules / Application / Module.php
1 <?php
2
3 namespace Application;
4
5 use InvalidArgumentException,
6     Zend\Module\Manager,
7     Zend\Config\Config,
8     Zend\EventManager\StaticEventManager;
9
10 class Module
11 {
12     protected $view;
13     protected $viewListener;
14
15     public function init(Manager $moduleManager)
16     {
17         $this->initAutoloader($moduleManager->getOptions()->getApplicationEnv());
18         $events = StaticEventManager::getInstance();
19         $events->attach('bootstrap', 'bootstrap', array($this, 'initializeView'), 100);
20     }
21
22     protected function initAutoloader($env = null)
23     {
24         require __DIR__ . '/autoload_register.php';
25     }
26
27     public function getConfig()
28     {
29         return new Config(include __DIR__ . '/configs/module.config.php');
30     }
31     
32     public function initializeView($e)
33     {
34         $app          = $e->getParam('application');
35         $locator      = $app->getLocator();
36         $config       = $e->getParam('modules')->getMergedConfig();
37         $view         = $this->getView($app);
38         $viewListener = $this->getViewListener($view, $config);
39         $app->events()->attachAggregate($viewListener);
40         $events       = StaticEventManager::getInstance();
41         $viewListener->registerStaticListeners($events, $locator);
42     }
43
44     protected function getViewListener($view, $config)
45     {
46         if ($this->viewListener instanceof View\Listener) {
47             return $this->viewListener;
48         }
49
50         $viewListener       = new View\Listener($view, $config->layout);
51         $viewListener->setDisplayExceptionsFlag($config->display_exceptions);
52
53         $this->viewListener = $viewListener;
54         return $viewListener;
55     }
56
57     protected function getView($app)
58     {
59         if ($this->view) {
60             return $this->view;
61         }
62
63         $di     = $app->getLocator();
64         $view   = $di->get('view');
65         $url    = $view->plugin('url');
66         $url->setRouter($app->getRouter());
67
68         $view->plugin('headTitle')->setSeparator(' - ')
69                                   ->setAutoEscape(false)
70                                   ->append('Application');
71         $this->view = $view;
72         return $view;
73     }
74 }