public function attachView($view)
{
if (!$this->shouldCollect('view', true) || isset($this->collectors['views'])) {
return;
}
// You can add only One View instance
if (is_string($view)) {
$view = $this->di[$view];
}
$engins = $view->getRegisteredEngines();
if (isset($engins['.volt'])) {
$volt = $engins['.volt'];
if (is_object($volt)) {
if ($volt instanceof \Closure) {
$volt = $volt($view, $this->di);
}
} elseif (is_string($volt)) {
if (class_exists($volt)) {
$volt = new Volt($view, $this->di);
} elseif ($this->di->has($volt)) {
$volt = $this->di->getShared($volt, array($view, $this->di));
}
}
$engins['.volt'] = $volt;
$view->registerEngines($engins);
$volt->getCompiler()->addExtension(new VoltFunctions($this->di));
}
$viewProfiler = new Registry();
$viewProfiler->templates = array();
$viewProfiler->engines = $view->getRegisteredEngines();
$config = $this->config;
$eventsManager = $view->getEventsManager();
if (!is_object($eventsManager)) {
$eventsManager = new Manager();
}
$eventsManager->attach('view:beforeRender', function ($event, $view) use($viewProfiler) {
$viewProfiler->startRender = microtime(true);
});
$eventsManager->attach('view:afterRender', function ($event, $view) use($viewProfiler, $config) {
$viewProfiler->stopRender = microtime(true);
if ($config->options->views->get('data', false)) {
$viewProfiler->params = $view->getParamsToView();
} else {
$viewProfiler->params = null;
}
});
$eventsManager->attach('view:beforeRenderView', function ($event, $view) use($viewProfiler) {
$viewFilePath = $view->getActiveRenderPath();
if (Version::getId() >= 2000140) {
if (!$view instanceof \Phalcon\Mvc\ViewInterface && $view instanceof \Phalcon\Mvc\ViewBaseInterface) {
$viewFilePath = realpath($view->getViewsDir()) . DIRECTORY_SEPARATOR . $viewFilePath;
}
} elseif ($view instanceof Simple) {
$viewFilePath = realpath($view->getViewsDir()) . DIRECTORY_SEPARATOR . $viewFilePath;
}
$templates = $viewProfiler->templates;
$templates[$viewFilePath]['startTime'] = microtime(true);
$viewProfiler->templates = $templates;
});
$eventsManager->attach('view:afterRenderView', function ($event, $view) use($viewProfiler) {
$viewFilePath = $view->getActiveRenderPath();
if (Version::getId() >= 2000140) {
if (!$view instanceof \Phalcon\Mvc\ViewInterface && $view instanceof \Phalcon\Mvc\ViewBaseInterface) {
$viewFilePath = realpath($view->getViewsDir()) . DIRECTORY_SEPARATOR . $viewFilePath;
}
} elseif ($view instanceof Simple) {
$viewFilePath = realpath($view->getViewsDir()) . DIRECTORY_SEPARATOR . $viewFilePath;
}
$templates = $viewProfiler->templates;
$templates[$viewFilePath]['stopTime'] = microtime(true);
$viewProfiler->templates = $templates;
});
$view->setEventsManager($eventsManager);
$collector = new ViewCollector($viewProfiler, $view);
$this->addCollector($collector);
}