Phosphorum\Bootstrap::initView PHP Method

initView() protected method

Setting up the view component.
protected initView ( )
    protected function initView()
    {
        $this->di->set('view', function () {
            /** @var DiInterface $this */
            $config = $this->getShared('config');
            $em = $this->getShared('eventsManager');
            $view = new View();
            $view->registerEngines(['.volt' => function ($view, $di) {
                /** @var DiInterface $this */
                $config = $this->getShared('config');
                $volt = new VoltEngine($view, $di);
                $options = ['compiledPath' => function ($templatePath) {
                    /** @var DiInterface $this */
                    $config = $this->getShared('config')->get('volt')->toArray();
                    $filename = str_replace(['\\', '/'], $config['separator'], trim(substr($templatePath, strlen(BASE_DIR)), '\\/'));
                    $filename = basename($filename, '.volt') . $config['compiledExt'];
                    $cacheDir = rtrim($config['cacheDir'], '\\/') . DIRECTORY_SEPARATOR;
                    if (!is_dir($cacheDir)) {
                        @mkdir($cacheDir, 0755, true);
                    }
                    return rtrim($config['cacheDir'], '\\/') . DIRECTORY_SEPARATOR . $filename;
                }, 'compileAlways' => boolval($config->get('volt')->forceCompile)];
                $volt->setOptions($options);
                $volt->getCompiler()->addFunction('number_format', function ($resolvedArgs) {
                    return 'number_format(' . $resolvedArgs . ')';
                })->addFunction('chr', function ($resolvedArgs) {
                    return 'chr(' . $resolvedArgs . ')';
                });
                return $volt;
            }, '.phtml' => Php::class]);
            $view->setViewsDir($config->get('application')->viewsDir);
            $that = $this;
            $em->attach('view', function ($event, $view) use($that, $config) {
                /**
                 * @var LoggerInterface $logger
                 * @var View $view
                 * @var Event $event
                 * @var DiInterface $that
                 */
                $logger = $that->get('logger');
                $logger->debug(sprintf('Event %s. Path: %s', $event->getType(), $view->getActiveRenderPath()));
                if ('notFoundView' == $event->getType()) {
                    $message = sprintf('View not found: %s', $view->getActiveRenderPath());
                    $logger->error($message);
                    throw new ViewException($message);
                }
            });
            $view->setEventsManager($em);
            return $view;
        });
    }