From 41e900fc7a5cf53f340abdaf1e687adea7e0d0d3 Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Sat, 16 Jan 2016 21:35:50 -0500 Subject: Show template rendering time in debug log --- app/Core/Template.php | 53 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 2 deletions(-) (limited to 'app') diff --git a/app/Core/Template.php b/app/Core/Template.php index ce2884a7..8ded6f7c 100644 --- a/app/Core/Template.php +++ b/app/Core/Template.php @@ -18,6 +18,50 @@ class Template extends Helper */ private $overrides = array(); + /** + * Rendering start time + * + * @access private + * @var float + */ + private $startTime = 0; + + /** + * Total rendering time + * + * @access private + * @var float + */ + private $renderingTime = 0; + + /** + * Method executed before the rendering + * + * @access protected + * @param string $template + */ + protected function beforeRender($template) + { + if (DEBUG) { + $this->startTime = microtime(true); + } + } + + /** + * Method executed after the rendering + * + * @access protected + * @param string $template + */ + protected function afterRender($template) + { + if (DEBUG) { + $duration = microtime(true) - $this->startTime; + $this->renderingTime += $duration; + $this->container['logger']->debug('Rendering '.$template.' in '.$duration.'s, total='.$this->renderingTime); + } + } + /** * Render a template * @@ -32,11 +76,16 @@ class Template extends Helper */ public function render($__template_name, array $__template_args = array()) { - extract($__template_args); + $this->beforeRender($__template_name); + extract($__template_args); ob_start(); include $this->getTemplateFile($__template_name); - return ob_get_clean(); + $html = ob_get_clean(); + + $this->afterRender($__template_name); + + return $html; } /** -- cgit v1.2.3