From ee6eaddb3200636a1fcfe8909874ead6cdafbf2f Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Tue, 28 May 2013 21:14:20 -0400 Subject: [PATCH] Make use of Template names for usage profiling. --- testing/tests/views/template_test.php | 16 ++++++++-------- views/template.php | 14 ++++++++++---- views/template_loader.php | 24 ++++++++++++++++++++++-- 3 files changed, 40 insertions(+), 14 deletions(-) diff --git a/testing/tests/views/template_test.php b/testing/tests/views/template_test.php index 057b215..1c78c85 100644 --- a/testing/tests/views/template_test.php +++ b/testing/tests/views/template_test.php @@ -28,20 +28,20 @@ class TemplateTest extends \PHPUnit_Framework_TestCase public function testRenderSimple() { - $template = Template::NewWithData('Hello World'); + $template = Template::NewWithData('test', 'Hello World'); $this->assertEquals('Hello World', $this->_Render($template)); } public function testRender1Var() { - $template = Template::NewWithData('Hello, {% $name | str %}'); + $template = Template::NewWithData('test', 'Hello, {% $name | str %}'); $template->name = 'Robert'; $this->assertEquals('Hello, Robert', $this->_Render($template)); } public function testRender2Vars() { - $template = Template::NewWithData('Hello, {% $name %}. Today is the {% $date->day | int %} of July.'); + $template = Template::NewWithData('test', 'Hello, {% $name %}. Today is the {% $date->day | int %} of July.'); $date = new \stdClass(); $date->day = 26; $template->name = 'Robert'; @@ -51,7 +51,7 @@ class TemplateTest extends \PHPUnit_Framework_TestCase public function testRenderIf() { - $template = Template::NewWithData( + $template = Template::NewWithData('test', 'You are {!% if (!$user->logged_in): %}not logged in{!% else: %}{% $user->name %}{!% endif %}'); $template->user = new \stdClass(); $template->user->logged_in = TRUE; @@ -66,7 +66,7 @@ class TemplateTest extends \PHPUnit_Framework_TestCase { try { $catch = FALSE; - $template = Template::NewWithData('Hello %}'); + $template = Template::NewWithData('test', 'Hello %}'); } catch (\hoplite\views\TemplateException $e) { $message = $e->GetMessage(); // Check that the column number is correct. @@ -77,7 +77,7 @@ class TemplateTest extends \PHPUnit_Framework_TestCase try { $catch = FALSE; - $template = Template::NewWithData("Salve\n{% {%"); + $template = Template::NewWithData('test', "Salve\n{% {%"); } catch (\hoplite\views\TemplateException $e) { $message = $e->GetMessage(); $this->assertTrue(strpos($message, '2:4') !== FALSE); @@ -87,7 +87,7 @@ class TemplateTest extends \PHPUnit_Framework_TestCase try { $catch = FALSE; - $template = Template::NewWithData("Salve\n\n{% \$name {!%"); + $template = Template::NewWithData('test', "Salve\n\n{% \$name {!%"); } catch (\hoplite\views\TemplateException $e) { $message = $e->GetMessage(); $this->assertTrue(strpos($message, '3:10') !== FALSE); @@ -98,7 +98,7 @@ class TemplateTest extends \PHPUnit_Framework_TestCase public function testRenderVars() { - $template = Template::NewWithData('Some {% $v %}'); + $template = Template::NewWithData('test', 'Some {% $v %}'); $this->assertEquals('Some value', $template->Render(array('v' => 'value'))); $template->v = 'other'; diff --git a/views/template.php b/views/template.php index 18f7796..53598ef 100644 --- a/views/template.php +++ b/views/template.php @@ -16,7 +16,10 @@ namespace hoplite\views; +use \hoplite\base\Profiling; + require_once HOPLITE_ROOT . '/base/filter.php'; +require_once HOPLITE_ROOT . '/base/profiling.php'; /*! Renders a template with additional vars. @@ -64,16 +67,16 @@ class Template $this->template_name = $name; } - static public function NewWithData($data) + static public function NewWithData($name, $data) { - $template = new Template(''); + $template = new Template($name); $template->data = $template->_ProcessTemplate($data); return $template; } - static public function NewWithCompiledData($data) + static public function NewWithCompiledData($name, $data) { - $template = new Template(''); + $template = new Template($name); $template->data = $data; return $template; } @@ -113,6 +116,9 @@ class Template error_reporting($error); + if (Profiling::IsProfilingEnabled()) + TemplateLoader::GetInstance()->MarkTemplateRendered($this->name); + $data = ob_get_contents(); ob_end_clean(); return $data; diff --git a/views/template_loader.php b/views/template_loader.php index daebfaa..f0e624d 100644 --- a/views/template_loader.php +++ b/views/template_loader.php @@ -16,6 +16,9 @@ namespace hoplite\views; +use \hoplite\base\Profiling; + +require_once HOPLITE_ROOT . '/base/profiling.php'; require_once HOPLITE_ROOT . '/views/template.php'; /*! @@ -46,6 +49,11 @@ class TemplateLoader /*! @var array An array of Template objects, keyed by the template name. */ protected $cache = array(); + /*! @var array Array of template usage counts, keyed by name. Only used + when profiling. + */ + protected $usage = array(); + /*! Gets the singleton instance. */ static public function GetInstance() { @@ -75,6 +83,9 @@ class TemplateLoader */ public function Load($name) { + if (Profiling::IsProfilingEnabled() && !isset($this->usage[$name])) + $this->usage[$name] = 0; + // First check the memory cache. if (isset($this->cache[$name])) return clone $this->cache[$name]; @@ -98,6 +109,15 @@ class TemplateLoader return self::GetInstance()->Load($name); } + /*! Marks a template as having been used. */ + public function MarkTemplateRendered($name) + { + if (!isset($this->usage[$name])) + throw new \InvalidArgumentException("Template $name has not been loaded through this instance"); + + $this->usage[$name]++; + } + /*! Loads a cached filesystem template if it is up-to-date. @@ -119,7 +139,7 @@ class TemplateLoader if ($data === FALSE) return NULL; - return Template::NewWithCompiledData($data); + return Template::NewWithCompiledData($name, $data); } /*! @@ -139,7 +159,7 @@ class TemplateLoader if ($data === FALSE) throw new TemplateLoaderException('Could not load template ' . $name); - $template = Template::NewWithData($data); + $template = Template::NewWithData($name, $data); // Cache the file. if (file_put_contents($cache_path, $this->cache_prefix . $template->template()) === FALSE) -- 2.22.5