PageRenderTime 39ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/mustache/src/Mustache/Loader/InlineLoader.php

https://bitbucket.org/moodle/moodle
PHP | 123 lines | 39 code | 9 blank | 75 comment | 6 complexity | 65f449d1f43febb087fdc90214c7efb4 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-3.0
  1. <?php
  2. /*
  3. * This file is part of Mustache.php.
  4. *
  5. * (c) 2010-2017 Justin Hileman
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. /**
  11. * A Mustache Template loader for inline templates.
  12. *
  13. * With the InlineLoader, templates can be defined at the end of any PHP source
  14. * file:
  15. *
  16. * $loader = new Mustache_Loader_InlineLoader(__FILE__, __COMPILER_HALT_OFFSET__);
  17. * $hello = $loader->load('hello');
  18. * $goodbye = $loader->load('goodbye');
  19. *
  20. * __halt_compiler();
  21. *
  22. * @@ hello
  23. * Hello, {{ planet }}!
  24. *
  25. * @@ goodbye
  26. * Goodbye, cruel {{ planet }}
  27. *
  28. * Templates are deliniated by lines containing only `@@ name`.
  29. *
  30. * The InlineLoader is well-suited to micro-frameworks such as Silex:
  31. *
  32. * $app->register(new MustacheServiceProvider, array(
  33. * 'mustache.loader' => new Mustache_Loader_InlineLoader(__FILE__, __COMPILER_HALT_OFFSET__)
  34. * ));
  35. *
  36. * $app->get('/{name}', function ($name) use ($app) {
  37. * return $app['mustache']->render('hello', compact('name'));
  38. * })
  39. * ->value('name', 'world');
  40. *
  41. * // ...
  42. *
  43. * __halt_compiler();
  44. *
  45. * @@ hello
  46. * Hello, {{ name }}!
  47. */
  48. class Mustache_Loader_InlineLoader implements Mustache_Loader
  49. {
  50. protected $fileName;
  51. protected $offset;
  52. protected $templates;
  53. /**
  54. * The InlineLoader requires a filename and offset to process templates.
  55. *
  56. * The magic constants `__FILE__` and `__COMPILER_HALT_OFFSET__` are usually
  57. * perfectly suited to the job:
  58. *
  59. * $loader = new Mustache_Loader_InlineLoader(__FILE__, __COMPILER_HALT_OFFSET__);
  60. *
  61. * Note that this only works if the loader is instantiated inside the same
  62. * file as the inline templates. If the templates are located in another
  63. * file, it would be necessary to manually specify the filename and offset.
  64. *
  65. * @param string $fileName The file to parse for inline templates
  66. * @param int $offset A string offset for the start of the templates.
  67. * This usually coincides with the `__halt_compiler`
  68. * call, and the `__COMPILER_HALT_OFFSET__`
  69. */
  70. public function __construct($fileName, $offset)
  71. {
  72. if (!is_file($fileName)) {
  73. throw new Mustache_Exception_InvalidArgumentException('InlineLoader expects a valid filename.');
  74. }
  75. if (!is_int($offset) || $offset < 0) {
  76. throw new Mustache_Exception_InvalidArgumentException('InlineLoader expects a valid file offset.');
  77. }
  78. $this->fileName = $fileName;
  79. $this->offset = $offset;
  80. }
  81. /**
  82. * Load a Template by name.
  83. *
  84. * @throws Mustache_Exception_UnknownTemplateException If a template file is not found
  85. *
  86. * @param string $name
  87. *
  88. * @return string Mustache Template source
  89. */
  90. public function load($name)
  91. {
  92. $this->loadTemplates();
  93. if (!array_key_exists($name, $this->templates)) {
  94. throw new Mustache_Exception_UnknownTemplateException($name);
  95. }
  96. return $this->templates[$name];
  97. }
  98. /**
  99. * Parse and load templates from the end of a source file.
  100. */
  101. protected function loadTemplates()
  102. {
  103. if ($this->templates === null) {
  104. $this->templates = array();
  105. $data = file_get_contents($this->fileName, false, null, $this->offset);
  106. foreach (preg_split("/^@@(?= [\w\d\.]+$)/m", $data, -1) as $chunk) {
  107. if (trim($chunk)) {
  108. list($name, $content) = explode("\n", $chunk, 2);
  109. $this->templates[trim($name)] = trim($content);
  110. }
  111. }
  112. }
  113. }
  114. }