It is possible to create your own template compiler and have the chosen View adapter use that instead. Please see the documentation on the dynamic dependencies of the adapter in question to know more about how this can be achieved.
See also: lithium\template\View
See also: lithium\template\view\adapter
Inheritance: extends lithium\core\StaticObject
 public function testFallbackWithNonWritableDirectory()
 {
     $this->expectException('/failed to open stream/');
     $result = Compiler::template("{$this->_path}/{$this->_file}", array('path' => LITHIUM_APP_PATH . '/foo', 'fallback' => true));
     $this->assertEqual("{$this->_path}/{$this->_file}", $result);
     $this->expectException('/Could not write compiled template to cache/');
     $this->expectException('/failed to open stream/');
     $result = Compiler::template("{$this->_path}/{$this->_file}", array('path' => LITHIUM_APP_PATH . '/foo', 'fallback' => false));
 }
Example #2
0
 public function testTemplateCacheHit()
 {
     $path = Libraries::get(true, 'resources') . '/tmp/cache/templates';
     $original = Compiler::template("{$this->_path}/{$this->_file}", compact('path'));
     $cache = glob("{$path}/*");
     clearstatcache();
     $cached = Compiler::template("{$this->_path}/{$this->_file}", compact('path'));
     $this->assertEqual($original, $cached);
     $this->assertEqual($cache, glob("{$path}/*"));
     file_put_contents("{$this->_path}/{$this->_file}", "Updated");
     clearstatcache();
     $updated = Compiler::template("{$this->_path}/{$this->_file}", compact('path'));
     $newCache = glob("{$path}/*");
     $this->assertNotEqual($cache, $updated);
     $this->assertEqual(count($cache), count($newCache));
     $this->assertNotEqual($cache, $newCache);
 }
Example #3
0
 /**
  * Override compile to have a different cache path by default.
  *
  * @see lithium\template\view\Compiler::template()
  * @param string $file The full path to the template that will be compiled.
  * @param array $options Options, see `Compiler::template()`.
  * @return string The compiled template.
  */
 public static function template($file, array $options = array())
 {
     $path = Libraries::get(true, 'resources') . '/tmp/cache/mails';
     $options += compact('path');
     return parent::template($file, $options);
 }
Example #4
0
 /**
  * Parses a PHP file for messages marked as translatable.  Recognized as message
  * marking are `$t()` and `$tn()` which are implemented in the `View` class. This
  * is a rather simple and stupid parser but also fast and easy to grasp. It doesn't
  * actively attempt to detect and work around syntax errors in marker functions.
  *
  * @see lithium\g11n\Message::aliases()
  * @param string $file Absolute path to a PHP file.
  * @return array
  */
 protected function _parsePhp($file)
 {
     $contents = file_get_contents($file);
     $contents = Compiler::compile($contents);
     $defaults = array('ids' => array(), 'context' => null, 'open' => false, 'position' => 0, 'occurrence' => array('file' => $file, 'line' => null));
     extract($defaults);
     $data = array();
     if (strpos($contents, '$t(') === false && strpos($contents, '$tn(') === false) {
         return $data;
     }
     $tokens = token_get_all($contents);
     unset($contents);
     $findContext = function ($position) use($tokens) {
         $ignore = array(T_WHITESPACE, '(', ')', T_ARRAY, ',');
         $open = 1;
         $options = array();
         $depth = 0;
         while (isset($tokens[$position]) && ($token = $tokens[$position])) {
             if (!is_array($token)) {
                 $token = array(0 => null, 1 => $token, 2 => null);
             }
             if ($token[1] === '(') {
                 $open++;
             } elseif ($token[1] === ')' && --$open === 0) {
                 break;
             }
             if ($token[0] === T_ARRAY || $token[1] === '[') {
                 $depth++;
             } elseif ($depth > 1 && ($token[1] === ')' || $token[1] === ']')) {
                 $depth--;
             }
             if ($depth === 1 && $open === 2) {
                 if (!in_array($token[0] ?: $token[1], $ignore)) {
                     $options[] = $token;
                 }
             }
             $position++;
         }
         foreach ($options as $i => $token) {
             if (!(isset($options[$i + 1]) && isset($options[$i + 2]))) {
                 break;
             }
             $condition1 = substr($token[1], 1, -1) === 'context';
             $condition2 = $options[$i + 1][0] === T_DOUBLE_ARROW;
             $condition3 = $options[$i + 2][0] === T_CONSTANT_ENCAPSED_STRING;
             if ($condition1 && $condition2 && $condition3) {
                 return $options[$i + 2][1];
             }
         }
         return null;
     };
     foreach ($tokens as $key => $token) {
         if (!is_array($token)) {
             $token = array(0 => null, 1 => $token, 2 => null);
         }
         if ($open) {
             if ($position >= ($open === 'singular' ? 1 : 2)) {
                 $data = $this->_merge($data, array('id' => $ids['singular'], 'ids' => $ids, 'occurrences' => array($occurrence), 'context' => $context));
                 extract($defaults, EXTR_OVERWRITE);
             } elseif ($token[0] === T_CONSTANT_ENCAPSED_STRING) {
                 $ids[$ids ? 'plural' : 'singular'] = $token[1];
                 $position++;
             }
         } else {
             if (isset($tokens[$key + 1]) && $tokens[$key + 1] === '(') {
                 if ($token[1] === '$t') {
                     $open = 'singular';
                 } elseif ($token[1] === '$tn') {
                     $open = 'plural';
                 } else {
                     continue;
                 }
                 $occurrence['line'] = $token[2];
                 $context = $findContext($key + 2);
             }
         }
     }
     return $data;
 }
Example #5
0
 /**
  * Parses a PHP file for messages marked as translatable.  Recognized as message
  * marking are `$t()` and `$tn()` which are implemented in the `View` class. This
  * is a rather simple and stupid parser but also fast and easy to grasp. It doesn't
  * actively attempt to detect and work around syntax errors in marker functions.
  *
  * @see lithium\g11n\Message::aliases()
  * @param string $file Absolute path to a PHP file.
  * @return array
  */
 protected function _parsePhp($file)
 {
     $contents = file_get_contents($file);
     $contents = Compiler::compile($contents);
     $defaults = array('ids' => array(), 'open' => false, 'position' => 0, 'occurrence' => array('file' => $file, 'line' => null));
     extract($defaults);
     $data = array();
     if (strpos($contents, '$t(') === false && strpos($contents, '$tn(') == false) {
         return $data;
     }
     $tokens = token_get_all($contents);
     unset($contents);
     foreach ($tokens as $key => $token) {
         if (!is_array($token)) {
             $token = array(0 => null, 1 => $token, 2 => null);
         }
         if ($open) {
             if ($position >= ($open === 'singular' ? 1 : 2)) {
                 $data = $this->_merge($data, array('id' => $ids['singular'], 'ids' => $ids, 'occurrences' => array($occurrence)));
                 extract($defaults, EXTR_OVERWRITE);
             } elseif ($token[0] === T_CONSTANT_ENCAPSED_STRING) {
                 $ids[$ids ? 'plural' : 'singular'] = $token[1];
                 $position++;
             }
         } else {
             if (isset($tokens[$key + 1]) && $tokens[$key + 1] === '(') {
                 if ($token[1] === '$t') {
                     $open = 'singular';
                 } elseif ($token[1] === '$tn') {
                     $open = 'plural';
                 } else {
                     continue;
                 }
                 $occurrence['line'] = $token[2];
             }
         }
     }
     return $data;
 }