Updates and cleanup
[zf2.biz/application_blanche.git] / modules / Application / src / Application / Bootstrap.php
1 <?php
2 namespace Application;
3
4 use Zend\Di\Configuration as DiConfiguration,
5     Zend\Di\Di,
6     Zend\EventManager\StaticEventManager,
7     Zend\Module\Manager as ModuleManager,
8     Zend\Mvc\Application,
9     Zend\Mvc\Router\Http\TreeRouteStack as Router;
10
11 class Bootstrap
12 {
13     /**
14      * @var \Zend\Config\Config
15      */
16     protected $config;
17
18     /**
19      * @var ModuleManager
20      */
21     protected $modules;
22
23     public function __construct(ModuleManager $modules)
24     {
25         $this->modules = $modules; 
26         $this->config  = $modules->getMergedConfig();
27     }
28
29     public function bootstrap(Application $app)
30     {
31         $this->setupLocator($app);
32         $this->setupRoutes($app);
33         $this->setupEvents($app);
34     }
35
36     protected function setupLocator(Application $app)
37     {
38         $di = new Di;
39         $di->instanceManager()->addTypePreference('Zend\Di\Locator', $di);
40
41         $config = new DiConfiguration($this->config->di);
42         $config->configure($di);
43
44         $app->setLocator($di);
45     }
46
47     protected function setupRoutes(Application $app)
48     {
49         $router = new Router();
50         $router->addRoutes($this->config->routes);
51         $app->setRouter($router);
52     }
53
54     protected function setupEvents(Application $app)
55     {
56         $view         = $this->getView($app);
57         $locator      = $app->getLocator();
58         $events       = $app->events();
59         $staticEvents = StaticEventManager::getInstance();
60
61         foreach ($this->modules->getLoadedModules() as $name => $module) {
62             if (method_exists($module, 'registerApplicationListeners')) {
63                 $module->registerApplicationListeners($events, $locator, $this->config);
64             }
65
66             if (method_exists($module, 'registerStaticListeners')) {
67                 $module->registerStaticListeners($staticEvents, $locator, $this->config);
68             }
69         }
70     }
71
72     protected function getView($app)
73     {
74         $di     = $app->getLocator();
75         $view   = $di->get('view');
76         $url    = $view->plugin('url');
77         $url->setRouter($app->getRouter());
78
79         $view->plugin('headTitle')->setSeparator(' - ')
80                                   ->setAutoEscape(false)
81                                   ->append('Application');
82         return $view;
83     }
84 }