Update zf2 submoudle and Di configuration
[zf2.biz/galerie.git] / modules / Application / src / Application / Bootstrap.php
1 <?php
2 namespace Application;
3
4 use Zend\Config\Config,
5     Zend\Di\Configuration,
6     Zend\Di\Definition,
7     Zend\Di\DefinitionList,
8     Zend\Di\Di,
9     Zend\EventManager\StaticEventManager,
10     Zend\Module\Manager as ModuleManager,
11     Zend\Mvc\Application;
12
13 class Bootstrap
14 {
15     protected $config;
16     protected $modules;
17
18     public function __construct(Config $config, ModuleManager $modules)
19     {
20         $this->config  = $config;
21         $this->modules = $modules; 
22     }
23
24     public function bootstrap(Application $app)
25     {
26         $this->setupLocator($app);
27         $this->setupRoutes($app);
28         $this->setupEvents($app);
29     }
30
31     protected function setupLocator(Application $app)
32     {
33         $di = new Di;
34         $di->instanceManager()->addTypePreference('Zend\Di\Locator', $di);
35
36         $config = new Configuration($this->config->di);
37         $config->configure($di);
38
39         $app->setLocator($di);
40     }
41
42     protected function setupRoutes(Application $app)
43     {
44         $router = $app->getLocator()->get('Zend\Mvc\Router\SimpleRouteStack');
45         foreach ($this->config->routes as $name => $config) {
46             $class   = $config->type;
47             $options = $config->options;
48             $route   = new $class($options);
49             $router->addRoute($name, $route);
50         }
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 }