Move some of the view related code from the Module class to the layout view script
[zf2.biz/galerie.git] / module / Application / Module.php
1 <?php
2
3 namespace Application;
4
5 use Zend\Module\Manager,
6     Zend\EventManager\StaticEventManager,
7     Zend\Module\Consumer\AutoloaderProvider;
8
9 class Module implements AutoloaderProvider
10 {
11     protected $view;
12     protected $viewListener;
13
14     public function init(Manager $moduleManager)
15     {
16         $events = StaticEventManager::getInstance();
17         $events->attach('bootstrap', 'bootstrap', array($this, 'initializeView'), 100);
18     }
19
20     public function getAutoloaderConfig()
21     {
22         return array(
23             'Zend\Loader\ClassMapAutoloader' => array(
24                 __DIR__ . '/autoload_classmap.php',
25             ),
26             'Zend\Loader\StandardAutoloader' => array(
27                 'namespaces' => array(
28                     __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
29                 ),
30             ),
31         );
32     }
33
34     public function getConfig()
35     {
36         return include __DIR__ . '/config/module.config.php';
37     }
38     
39     public function initializeView($e)
40     {
41         $app          = $e->getParam('application');
42         $locator      = $app->getLocator();
43         $config       = $e->getParam('config');
44         $view         = $this->getView($app);
45         $viewListener = $this->getViewListener($view, $config);
46         $app->events()->attachAggregate($viewListener);
47         $events       = StaticEventManager::getInstance();
48         $viewListener->registerStaticListeners($events, $locator);
49     }
50
51     protected function getViewListener($view, $config)
52     {
53         if ($this->viewListener instanceof View\Listener) {
54             return $this->viewListener;
55         }
56
57         $viewListener       = new View\Listener($view, $config->layout);
58         $viewListener->setDisplayExceptionsFlag($config->display_exceptions);
59
60         $this->viewListener = $viewListener;
61         return $viewListener;
62     }
63
64     protected function getView($app)
65     {
66         if ($this->view) {
67             return $this->view;
68         }
69
70         $locator = $app->getLocator();
71         $view    = $locator->get('view');
72
73         // Set up view helpers        
74         $view->plugin('url')->setRouter($app->getRouter());
75         $view->doctype()->setDoctype('HTML5');
76
77         $basePath = $app->getRequest()->getBaseUrl();
78         $view->plugin('basePath')->setBasePath($basePath);
79
80         $this->view = $view;
81         return $view;
82     }
83 }