Make use of Template names for usage profiling.
authorRobert Sesek <rsesek@bluestatic.org>
Wed, 29 May 2013 01:14:20 +0000 (21:14 -0400)
committerRobert Sesek <rsesek@bluestatic.org>
Wed, 29 May 2013 01:14:20 +0000 (21:14 -0400)
testing/tests/views/template_test.php
views/template.php
views/template_loader.php

index 057b215734146af6731c25534d5c8300d5d7b5d2..1c78c85a1f3f59c3b8e2637ad0c5dd8cc5180a81 100644 (file)
@@ -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';
index 18f7796c3cdb73ee99e5e0bc835082b076cf9386..53598efe60c1584b150630d4848e0153854337c8 100644 (file)
 
 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;
index daebfaa4bb599b91f36bd8373fcd683775de1cea..f0e624d428ef30dff28d09dca4b43acfb6bf80bb 100644 (file)
@@ -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)