9dee84c8687de0ba547e2ea8e1a0b3e06d4dfeec
[zf2.biz/galerie.git] / modules / Application / Module.php
1 <?php
2
3 namespace Application;
4
5 use Zend\Module\Manager,
6     Zend\Config\Config,
7     Zend\EventManager\StaticEventManager,
8     Zend\Loader\AutoloaderFactory;
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         AutoloaderFactory::factory(array(
25             'Zend\Loader\ClassMapAutoloader' => array(
26                 __DIR__ . '/autoload_classmap.php',
27             ),
28             'Zend\Loader\StandardAutoloader' => array(
29                 'namespaces' => array(
30                     __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
31                 ),
32             ),
33         ));
34     }
35
36     public function getConfig()
37     {
38         return new Config(include __DIR__ . '/configs/module.config.php');
39     }
40     
41     public function initializeView($e)
42     {
43         $app          = $e->getParam('application');
44         $locator      = $app->getLocator();
45         $config       = $e->getParam('modules')->getMergedConfig();
46         $view         = $this->getView($app);
47         $viewListener = $this->getViewListener($view, $config);
48         $app->events()->attachAggregate($viewListener);
49         $events       = StaticEventManager::getInstance();
50         $viewListener->registerStaticListeners($events, $locator);
51     }
52
53     protected function getViewListener($view, $config)
54     {
55         if ($this->viewListener instanceof View\Listener) {
56             return $this->viewListener;
57         }
58
59         $viewListener       = new View\Listener($view, $config->layout);
60         $viewListener->setDisplayExceptionsFlag($config->display_exceptions);
61
62         $this->viewListener = $viewListener;
63         return $viewListener;
64     }
65
66     protected function getView($app)
67     {
68         if ($this->view) {
69             return $this->view;
70         }
71
72         $di     = $app->getLocator();
73         $view   = $di->get('view');
74         $url    = $view->plugin('url');
75         $url->setRouter($app->getRouter());
76
77         $view->plugin('headTitle')->setSeparator(' - ')
78                                   ->setAutoEscape(false)
79                                   ->append('Application');
80         $this->view = $view;
81         return $view;
82     }
83 }