示例#1
0
 /**
  * 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;
 }