Compiler.php 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2013, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace lithium\template\view;
  9. use lithium\core\Libraries;
  10. use lithium\template\TemplateException;
  11. /**
  12. * The template compiler is a simple string replacement engine which allows PHP templates to be
  13. * overridden with custom syntax. The default process rules allow PHP templates using short-echo
  14. * syntax (`<?=`) to be rewritten to full PHP tags which automatically escape their output.
  15. *
  16. * It is possible to create your own template compiler and have the chosen `View` adapter use that
  17. * instead. Please see the documentation on the dynamic dependencies of the adapter in question
  18. * to know more about how this can be achieved.
  19. *
  20. * @see lithium\template\View
  21. * @see lithium\template\view\adapter
  22. */
  23. class Compiler extends \lithium\core\StaticObject {
  24. /**
  25. * The list of syntax replacements to apply to compiled templates.
  26. *
  27. * Key/value pairs of regular expressions. The keys are the regexes, and the values are the
  28. * resulting expressions along with any capture groups that may have been used in the
  29. * corresponding regexes.
  30. *
  31. * @var array
  32. */
  33. protected static $_processors = array(
  34. '/\<\?=\s*\$this->(.+?)\s*;?\s*\?>/msx' => '<?php echo $this->$1; ?>',
  35. '/\<\?=\s*(\$h\(.+?)\s*;?\s*\?>/msx' => '<?php echo $1; ?>',
  36. '/\<\?=\s*(.+?)\s*;?\s*\?>/msx' => '<?php echo $h($1); ?>'
  37. );
  38. /**
  39. * Compiles a template and writes it to a cache file, which is used for inclusion.
  40. *
  41. * @param string $file The full path to the template that will be compiled.
  42. * @param array $options Options for compilation include:
  43. * - `path`: Path where the compiled template should be written.
  44. * - `fallback`: Boolean indicating that if the compilation failed for some
  45. * reason (e.g. `path` is not writable), that the compiled template
  46. * should still be returned and no exception be thrown.
  47. * @return string The compiled template.
  48. */
  49. public static function template($file, array $options = array()) {
  50. $cachePath = Libraries::get(true, 'resources') . '/tmp/cache/templates';
  51. $defaults = array('path' => $cachePath, 'fallback' => false);
  52. $options += $defaults;
  53. $stats = stat($file);
  54. $dir = dirname($file);
  55. $oname = basename(dirname($dir)) . '_' . basename($dir) . '_' . basename($file, '.php');
  56. $template = "template_{$oname}_{$stats['ino']}_{$stats['mtime']}_{$stats['size']}.php";
  57. $template = "{$options['path']}/{$template}";
  58. if (file_exists($template)) {
  59. return $template;
  60. }
  61. $compiled = static::compile(file_get_contents($file));
  62. if (is_writable($cachePath) && file_put_contents($template, $compiled) !== false) {
  63. foreach (glob("{$options['path']}/template_{$oname}_*.php") as $expired) {
  64. if ($expired !== $template) {
  65. unlink($expired);
  66. }
  67. }
  68. return $template;
  69. }
  70. if ($options['fallback']) {
  71. return $file;
  72. }
  73. throw new TemplateException("Could not write compiled template `{$template}` to cache.");
  74. }
  75. /**
  76. * Preprocess the passed `$string` (usually a PHP template) for syntax replacements
  77. * using sets of regular expressions.
  78. *
  79. * @see lithium\template\view\Compiler::$_processors
  80. * @param string $string The string to be preprocessed.
  81. * @return string Processed string.
  82. */
  83. public static function compile($string) {
  84. $patterns = static::$_processors;
  85. return preg_replace(array_keys($patterns), array_values($patterns), $string);
  86. }
  87. }
  88. ?>