/** * Register the services. * * @throws Exception * @return void */ public function registerServices() { if (empty($this->_moduleName)) { $class = new \ReflectionClass($this); throw new \Exception('Bootstrap has no module name: ' . $class->getFileName()); } $di = $this->getDI(); $config = $this->getConfig(); $eventsManager = $this->getEventsManager(); $moduleDirectory = $this->getModuleDirectory(); /*************************************************/ // Initialize view. /*************************************************/ $di->set('view', function () use($di, $config, $moduleDirectory, $eventsManager) { return View::factory($di, $config, $moduleDirectory . '/View/', $eventsManager); }); /*************************************************/ // Initialize dispatcher. /*************************************************/ $eventsManager->attach('dispatch:beforeException', new DispatchErrorHandler()); // Create dispatcher. $dispatcher = new Dispatcher(); $dispatcher->setEventsManager($eventsManager); $di->set('dispatcher', $dispatcher); }
/** * Create view instance. * If no events manager provided - events would not be attached. * * @param DIBehaviour $di DI. * @param Config $config Configuration. * @param string|null $viewsDirectory Views directory location. * @param Manager|null $em Events manager. * * @return View */ public static function factory($di, $config, $viewsDirectory = null, $em = null) { $view = new View(); $volt = new Volt($view, $di); $volt->setOptions(["compiledPath" => $config->application->view->compiledPath, "compiledExtension" => $config->application->view->compiledExtension, 'compiledSeparator' => $config->application->view->compiledSeparator, 'compileAlways' => $config->application->debug && $config->application->view->compileAlways]); $compiler = $volt->getCompiler(); $compiler->addExtension(new Extension()); $view->registerEngines([".volt" => $volt])->setRenderLevel(View::LEVEL_ACTION_VIEW)->restoreViewDir(); if (!$viewsDirectory) { $view->setViewsDir($viewsDirectory); } // Attach a listener for type "view". if ($em) { $em->attach("view", function ($event, $view) use($di, $config) { if ($config->application->profiler && $di->has('profiler')) { if ($event->getType() == 'beforeRender') { $di->get('profiler')->start(); } if ($event->getType() == 'afterRender') { $di->get('profiler')->stop($view->getActiveRenderPath(), 'view'); } } if ($event->getType() == 'notFoundView') { throw new Exception('View not found - "' . $view->getActiveRenderPath() . '"'); } }); $view->setEventsManager($em); } return $view; }
/** * Initialize view. * * @param DIBehaviour|DI $di Dependency Injection. * @param Config $config Config object. * @param EventsManager $eventsManager Event manager. * * @return void */ protected function _initView($di, $config, $eventsManager) { /*************************************************/ // Initialize view. /*************************************************/ $di->setShared('view', View::factory($di, $config, null, $eventsManager)); }