diff options
author | Simon Rettberg | 2023-10-02 18:01:05 +0200 |
---|---|---|
committer | Simon Rettberg | 2023-10-02 18:01:05 +0200 |
commit | c8e975a0151c74ea731dc709640310222d884297 (patch) | |
tree | 62688c460ac71b65a205344e7a6d78a6ab5a96ee /Mustache/Loader | |
parent | Disable deprecated warnings when not in debug mode (diff) | |
download | slx-admin-c8e975a0151c74ea731dc709640310222d884297.tar.gz slx-admin-c8e975a0151c74ea731dc709640310222d884297.tar.xz slx-admin-c8e975a0151c74ea731dc709640310222d884297.zip |
Update Mustache to 46286a1
Diffstat (limited to 'Mustache/Loader')
-rw-r--r-- | Mustache/Loader/ArrayLoader.php | 158 | ||||
-rw-r--r-- | Mustache/Loader/CascadingLoader.php | 69 | ||||
-rw-r--r-- | Mustache/Loader/FilesystemLoader.php | 253 | ||||
-rw-r--r-- | Mustache/Loader/InlineLoader.php | 123 | ||||
-rw-r--r-- | Mustache/Loader/MutableLoader.php | 63 | ||||
-rw-r--r-- | Mustache/Loader/ProductionFilesystemLoader.php | 86 | ||||
-rw-r--r-- | Mustache/Loader/StringLoader.php | 81 |
7 files changed, 562 insertions, 271 deletions
diff --git a/Mustache/Loader/ArrayLoader.php b/Mustache/Loader/ArrayLoader.php index 0a9ceefb..4276493a 100644 --- a/Mustache/Loader/ArrayLoader.php +++ b/Mustache/Loader/ArrayLoader.php @@ -1,79 +1,79 @@ -<?php
-
-/*
- * This file is part of Mustache.php.
- *
- * (c) 2012 Justin Hileman
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
-
-/**
- * Mustache Template array Loader implementation.
- *
- * An ArrayLoader instance loads Mustache Template source by name from an initial array:
- *
- * $loader = new ArrayLoader(
- * 'foo' => '{{ bar }}',
- * 'baz' => 'Hey {{ qux }}!'
- * );
- *
- * $tpl = $loader->load('foo'); // '{{ bar }}'
- *
- * The ArrayLoader is used internally as a partials loader by Mustache_Engine instance when an array of partials
- * is set. It can also be used as a quick-and-dirty Template loader.
- *
- * @implements Loader
- * @implements MutableLoader
- */
-class Mustache_Loader_ArrayLoader implements Mustache_Loader, Mustache_Loader_MutableLoader
-{
-
- /**
- * ArrayLoader constructor.
- *
- * @param array $templates Associative array of Template source (default: array())
- */
- public function __construct(array $templates = array())
- {
- $this->templates = $templates;
- }
-
- /**
- * Load a Template.
- *
- * @param string $name
- *
- * @return string Mustache Template source
- */
- public function load($name)
- {
- if (!isset($this->templates[$name])) {
- throw new InvalidArgumentException('Template '.$name.' not found.');
- }
-
- return $this->templates[$name];
- }
-
- /**
- * Set an associative array of Template sources for this loader.
- *
- * @param array $templates
- */
- public function setTemplates(array $templates)
- {
- $this->templates = $templates;
- }
-
- /**
- * Set a Template source by name.
- *
- * @param string $name
- * @param string $template Mustache Template source
- */
- public function setTemplate($name, $template)
- {
- $this->templates[$name] = $template;
- }
-}
+<?php + +/* + * This file is part of Mustache.php. + * + * (c) 2010-2017 Justin Hileman + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +/** + * Mustache Template array Loader implementation. + * + * An ArrayLoader instance loads Mustache Template source by name from an initial array: + * + * $loader = new ArrayLoader( + * 'foo' => '{{ bar }}', + * 'baz' => 'Hey {{ qux }}!' + * ); + * + * $tpl = $loader->load('foo'); // '{{ bar }}' + * + * The ArrayLoader is used internally as a partials loader by Mustache_Engine instance when an array of partials + * is set. It can also be used as a quick-and-dirty Template loader. + */ +class Mustache_Loader_ArrayLoader implements Mustache_Loader, Mustache_Loader_MutableLoader +{ + private $templates; + + /** + * ArrayLoader constructor. + * + * @param array $templates Associative array of Template source (default: array()) + */ + public function __construct(array $templates = array()) + { + $this->templates = $templates; + } + + /** + * Load a Template. + * + * @throws Mustache_Exception_UnknownTemplateException If a template file is not found + * + * @param string $name + * + * @return string Mustache Template source + */ + public function load($name) + { + if (!isset($this->templates[$name])) { + throw new Mustache_Exception_UnknownTemplateException($name); + } + + return $this->templates[$name]; + } + + /** + * Set an associative array of Template sources for this loader. + * + * @param array $templates + */ + public function setTemplates(array $templates) + { + $this->templates = $templates; + } + + /** + * Set a Template source by name. + * + * @param string $name + * @param string $template Mustache Template source + */ + public function setTemplate($name, $template) + { + $this->templates[$name] = $template; + } +} diff --git a/Mustache/Loader/CascadingLoader.php b/Mustache/Loader/CascadingLoader.php new file mode 100644 index 00000000..3fb6353c --- /dev/null +++ b/Mustache/Loader/CascadingLoader.php @@ -0,0 +1,69 @@ +<?php + +/* + * This file is part of Mustache.php. + * + * (c) 2010-2017 Justin Hileman + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +/** + * A Mustache Template cascading loader implementation, which delegates to other + * Loader instances. + */ +class Mustache_Loader_CascadingLoader implements Mustache_Loader +{ + private $loaders; + + /** + * Construct a CascadingLoader with an array of loaders. + * + * $loader = new Mustache_Loader_CascadingLoader(array( + * new Mustache_Loader_InlineLoader(__FILE__, __COMPILER_HALT_OFFSET__), + * new Mustache_Loader_FilesystemLoader(__DIR__.'/templates') + * )); + * + * @param Mustache_Loader[] $loaders + */ + public function __construct(array $loaders = array()) + { + $this->loaders = array(); + foreach ($loaders as $loader) { + $this->addLoader($loader); + } + } + + /** + * Add a Loader instance. + * + * @param Mustache_Loader $loader + */ + public function addLoader(Mustache_Loader $loader) + { + $this->loaders[] = $loader; + } + + /** + * Load a Template by name. + * + * @throws Mustache_Exception_UnknownTemplateException If a template file is not found + * + * @param string $name + * + * @return string Mustache Template source + */ + public function load($name) + { + foreach ($this->loaders as $loader) { + try { + return $loader->load($name); + } catch (Mustache_Exception_UnknownTemplateException $e) { + // do nothing, check the next loader. + } + } + + throw new Mustache_Exception_UnknownTemplateException($name); + } +} diff --git a/Mustache/Loader/FilesystemLoader.php b/Mustache/Loader/FilesystemLoader.php index 34a9ecfd..e366df70 100644 --- a/Mustache/Loader/FilesystemLoader.php +++ b/Mustache/Loader/FilesystemLoader.php @@ -1,118 +1,135 @@ -<?php
-
-/*
- * This file is part of Mustache.php.
- *
- * (c) 2012 Justin Hileman
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
-
-/**
- * Mustache Template filesystem Loader implementation.
- *
- * An ArrayLoader instance loads Mustache Template source from the filesystem by name:
- *
- * $loader = new FilesystemLoader(dirname(__FILE__).'/views');
- * $tpl = $loader->load('foo'); // equivalent to `file_get_contents(dirname(__FILE__).'/views/foo.mustache');
- *
- * This is probably the most useful Mustache Loader implementation. It can be used for partials and normal Templates:
- *
- * $m = new Mustache(array(
- * 'loader' => new FilesystemLoader(dirname(__FILE__).'/views'),
- * 'partials_loader' => new FilesystemLoader(dirname(__FILE__).'/views/partials'),
- * ));
- *
- * @implements Loader
- */
-class Mustache_Loader_FilesystemLoader implements Mustache_Loader
-{
- private $baseDir;
- private $extension = '.mustache';
- private $templates = array();
-
- /**
- * Mustache filesystem Loader constructor.
- *
- * Passing an $options array allows overriding certain Loader options during instantiation:
- *
- * $options = array(
- * // The filename extension used for Mustache templates. Defaults to '.mustache'
- * 'extension' => '.ms',
- * );
- *
- * @throws RuntimeException if $baseDir does not exist.
- *
- * @param string $baseDir Base directory containing Mustache template files.
- * @param array $options Array of Loader options (default: array())
- */
- public function __construct($baseDir, array $options = array())
- {
- $this->baseDir = rtrim(realpath($baseDir), '/');
-
- if (!is_dir($this->baseDir)) {
- throw new RuntimeException('FilesystemLoader baseDir must be a directory: '.$baseDir);
- }
-
- if (isset($options['extension'])) {
- $this->extension = '.' . ltrim($options['extension'], '.');
- }
- }
-
- /**
- * Load a Template by name.
- *
- * $loader = new FilesystemLoader(dirname(__FILE__).'/views');
- * $loader->load('admin/dashboard'); // loads "./views/admin/dashboard.mustache";
- *
- * @param string $name
- *
- * @return string Mustache Template source
- */
- public function load($name)
- {
- if (!isset($this->templates[$name])) {
- $this->templates[$name] = $this->loadFile($name);
- }
-
- return $this->templates[$name];
- }
-
- /**
- * Helper function for loading a Mustache file by name.
- *
- * @throws InvalidArgumentException if a template file is not found.
- *
- * @param string $name
- *
- * @return string Mustache Template source
- */
- protected function loadFile($name)
- {
- $fileName = $this->getFileName($name);
-
- if (!file_exists($fileName)) {
- throw new InvalidArgumentException('Template '.$name.' not found.');
- }
-
- return file_get_contents($fileName);
- }
-
- /**
- * Helper function for getting a Mustache template file name.
- *
- * @param string $name
- *
- * @return string Template file name
- */
- protected function getFileName($name)
- {
- $fileName = $this->baseDir . '/' . $name;
- if (substr($fileName, 0 - strlen($this->extension)) !== $this->extension) {
- $fileName .= $this->extension;
- }
-
- return $fileName;
- }
-}
+<?php + +/* + * This file is part of Mustache.php. + * + * (c) 2010-2017 Justin Hileman + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +/** + * Mustache Template filesystem Loader implementation. + * + * A FilesystemLoader instance loads Mustache Template source from the filesystem by name: + * + * $loader = new Mustache_Loader_FilesystemLoader(dirname(__FILE__).'/views'); + * $tpl = $loader->load('foo'); // equivalent to `file_get_contents(dirname(__FILE__).'/views/foo.mustache'); + * + * This is probably the most useful Mustache Loader implementation. It can be used for partials and normal Templates: + * + * $m = new Mustache(array( + * 'loader' => new Mustache_Loader_FilesystemLoader(dirname(__FILE__).'/views'), + * 'partials_loader' => new Mustache_Loader_FilesystemLoader(dirname(__FILE__).'/views/partials'), + * )); + */ +class Mustache_Loader_FilesystemLoader implements Mustache_Loader +{ + private $baseDir; + private $extension = '.mustache'; + private $templates = array(); + + /** + * Mustache filesystem Loader constructor. + * + * Passing an $options array allows overriding certain Loader options during instantiation: + * + * $options = array( + * // The filename extension used for Mustache templates. Defaults to '.mustache' + * 'extension' => '.ms', + * ); + * + * @throws Mustache_Exception_RuntimeException if $baseDir does not exist + * + * @param string $baseDir Base directory containing Mustache template files + * @param array $options Array of Loader options (default: array()) + */ + public function __construct($baseDir, array $options = array()) + { + $this->baseDir = $baseDir; + + if (strpos($this->baseDir, '://') === false) { + $this->baseDir = realpath($this->baseDir); + } + + if ($this->shouldCheckPath() && !is_dir($this->baseDir)) { + throw new Mustache_Exception_RuntimeException(sprintf('FilesystemLoader baseDir must be a directory: %s', $baseDir)); + } + + if (array_key_exists('extension', $options)) { + if (empty($options['extension'])) { + $this->extension = ''; + } else { + $this->extension = '.' . ltrim($options['extension'], '.'); + } + } + } + + /** + * Load a Template by name. + * + * $loader = new Mustache_Loader_FilesystemLoader(dirname(__FILE__).'/views'); + * $loader->load('admin/dashboard'); // loads "./views/admin/dashboard.mustache"; + * + * @param string $name + * + * @return string Mustache Template source + */ + public function load($name) + { + if (!isset($this->templates[$name])) { + $this->templates[$name] = $this->loadFile($name); + } + + return $this->templates[$name]; + } + + /** + * Helper function for loading a Mustache file by name. + * + * @throws Mustache_Exception_UnknownTemplateException If a template file is not found + * + * @param string $name + * + * @return string Mustache Template source + */ + protected function loadFile($name) + { + $fileName = $this->getFileName($name); + + if ($this->shouldCheckPath() && !file_exists($fileName)) { + throw new Mustache_Exception_UnknownTemplateException($name); + } + + return file_get_contents($fileName); + } + + /** + * Helper function for getting a Mustache template file name. + * + * @param string $name + * + * @return string Template file name + */ + protected function getFileName($name) + { + $fileName = $this->baseDir . '/' . $name; + if (substr($fileName, 0 - strlen($this->extension)) !== $this->extension) { + $fileName .= $this->extension; + } + + return $fileName; + } + + /** + * Only check if baseDir is a directory and requested templates are files if + * baseDir is using the filesystem stream wrapper. + * + * @return bool Whether to check `is_dir` and `file_exists` + */ + protected function shouldCheckPath() + { + return strpos($this->baseDir, '://') === false || strpos($this->baseDir, 'file://') === 0; + } +} diff --git a/Mustache/Loader/InlineLoader.php b/Mustache/Loader/InlineLoader.php new file mode 100644 index 00000000..ae297fec --- /dev/null +++ b/Mustache/Loader/InlineLoader.php @@ -0,0 +1,123 @@ +<?php + +/* + * This file is part of Mustache.php. + * + * (c) 2010-2017 Justin Hileman + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +/** + * A Mustache Template loader for inline templates. + * + * With the InlineLoader, templates can be defined at the end of any PHP source + * file: + * + * $loader = new Mustache_Loader_InlineLoader(__FILE__, __COMPILER_HALT_OFFSET__); + * $hello = $loader->load('hello'); + * $goodbye = $loader->load('goodbye'); + * + * __halt_compiler(); + * + * @@ hello + * Hello, {{ planet }}! + * + * @@ goodbye + * Goodbye, cruel {{ planet }} + * + * Templates are deliniated by lines containing only `@@ name`. + * + * The InlineLoader is well-suited to micro-frameworks such as Silex: + * + * $app->register(new MustacheServiceProvider, array( + * 'mustache.loader' => new Mustache_Loader_InlineLoader(__FILE__, __COMPILER_HALT_OFFSET__) + * )); + * + * $app->get('/{name}', function ($name) use ($app) { + * return $app['mustache']->render('hello', compact('name')); + * }) + * ->value('name', 'world'); + * + * // ... + * + * __halt_compiler(); + * + * @@ hello + * Hello, {{ name }}! + */ +class Mustache_Loader_InlineLoader implements Mustache_Loader +{ + protected $fileName; + protected $offset; + protected $templates; + + /** + * The InlineLoader requires a filename and offset to process templates. + * + * The magic constants `__FILE__` and `__COMPILER_HALT_OFFSET__` are usually + * perfectly suited to the job: + * + * $loader = new Mustache_Loader_InlineLoader(__FILE__, __COMPILER_HALT_OFFSET__); + * + * Note that this only works if the loader is instantiated inside the same + * file as the inline templates. If the templates are located in another + * file, it would be necessary to manually specify the filename and offset. + * + * @param string $fileName The file to parse for inline templates + * @param int $offset A string offset for the start of the templates. + * This usually coincides with the `__halt_compiler` + * call, and the `__COMPILER_HALT_OFFSET__` + */ + public function __construct($fileName, $offset) + { + if (!is_file($fileName)) { + throw new Mustache_Exception_InvalidArgumentException('InlineLoader expects a valid filename.'); + } + + if (!is_int($offset) || $offset < 0) { + throw new Mustache_Exception_InvalidArgumentException('InlineLoader expects a valid file offset.'); + } + + $this->fileName = $fileName; + $this->offset = $offset; + } + + /** + * Load a Template by name. + * + * @throws Mustache_Exception_UnknownTemplateException If a template file is not found + * + * @param string $name + * + * @return string Mustache Template source + */ + public function load($name) + { + $this->loadTemplates(); + + if (!array_key_exists($name, $this->templates)) { + throw new Mustache_Exception_UnknownTemplateException($name); + } + + return $this->templates[$name]; + } + + /** + * Parse and load templates from the end of a source file. + */ + protected function loadTemplates() + { + if ($this->templates === null) { + $this->templates = array(); + $data = file_get_contents($this->fileName, false, null, $this->offset); + foreach (preg_split("/^@@(?= [\w\d\.]+$)/m", $data, -1) as $chunk) { + if (trim($chunk)) { + list($name, $content) = explode("\n", $chunk, 2); + $this->templates[trim($name)] = trim($content); + } + } + } + } +} diff --git a/Mustache/Loader/MutableLoader.php b/Mustache/Loader/MutableLoader.php index 952db2f0..57fe5be3 100644 --- a/Mustache/Loader/MutableLoader.php +++ b/Mustache/Loader/MutableLoader.php @@ -1,32 +1,31 @@ -<?php
-
-/*
- * This file is part of Mustache.php.
- *
- * (c) 2012 Justin Hileman
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
-
-/**
- * Mustache Template mutable Loader interface.
- */
-interface Mustache_Loader_MutableLoader
-{
-
- /**
- * Set an associative array of Template sources for this loader.
- *
- * @param array $templates
- */
- public function setTemplates(array $templates);
-
- /**
- * Set a Template source by name.
- *
- * @param string $name
- * @param string $template Mustache Template source
- */
- public function setTemplate($name, $template);
-}
+<?php + +/* + * This file is part of Mustache.php. + * + * (c) 2010-2017 Justin Hileman + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +/** + * Mustache Template mutable Loader interface. + */ +interface Mustache_Loader_MutableLoader +{ + /** + * Set an associative array of Template sources for this loader. + * + * @param array $templates + */ + public function setTemplates(array $templates); + + /** + * Set a Template source by name. + * + * @param string $name + * @param string $template Mustache Template source + */ + public function setTemplate($name, $template); +} diff --git a/Mustache/Loader/ProductionFilesystemLoader.php b/Mustache/Loader/ProductionFilesystemLoader.php new file mode 100644 index 00000000..e7353327 --- /dev/null +++ b/Mustache/Loader/ProductionFilesystemLoader.php @@ -0,0 +1,86 @@ +<?php + +/* + * This file is part of Mustache.php. + * + * (c) 2010-2017 Justin Hileman + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +/** + * Mustache Template production filesystem Loader implementation. + * + * A production-ready FilesystemLoader, which doesn't require reading a file if it already exists in the template cache. + * + * {@inheritdoc} + */ +class Mustache_Loader_ProductionFilesystemLoader extends Mustache_Loader_FilesystemLoader +{ + private $statProps; + + /** + * Mustache production filesystem Loader constructor. + * + * Passing an $options array allows overriding certain Loader options during instantiation: + * + * $options = array( + * // The filename extension used for Mustache templates. Defaults to '.mustache' + * 'extension' => '.ms', + * 'stat_props' => array('size', 'mtime'), + * ); + * + * Specifying 'stat_props' overrides the stat properties used to invalidate the template cache. By default, this + * uses 'mtime' and 'size', but this can be set to any of the properties supported by stat(): + * + * http://php.net/manual/en/function.stat.php + * + * You can also disable filesystem stat entirely: + * + * $options = array('stat_props' => null); + * + * But with great power comes great responsibility. Namely, if you disable stat-based cache invalidation, + * YOU MUST CLEAR THE TEMPLATE CACHE YOURSELF when your templates change. Make it part of your build or deploy + * process so you don't forget! + * + * @throws Mustache_Exception_RuntimeException if $baseDir does not exist. + * + * @param string $baseDir Base directory containing Mustache template files. + * @param array $options Array of Loader options (default: array()) + */ + public function __construct($baseDir, array $options = array()) + { + parent::__construct($baseDir, $options); + + if (array_key_exists('stat_props', $options)) { + if (empty($options['stat_props'])) { + $this->statProps = array(); + } else { + $this->statProps = $options['stat_props']; + } + } else { + $this->statProps = array('size', 'mtime'); + } + } + + /** + * Helper function for loading a Mustache file by name. + * + * @throws Mustache_Exception_UnknownTemplateException If a template file is not found. + * + * @param string $name + * + * @return Mustache_Source Mustache Template source + */ + protected function loadFile($name) + { + $fileName = $this->getFileName($name); + + if (!file_exists($fileName)) { + throw new Mustache_Exception_UnknownTemplateException($name); + } + + return new Mustache_Source_FilesystemSource($fileName, $this->statProps); + } +} diff --git a/Mustache/Loader/StringLoader.php b/Mustache/Loader/StringLoader.php index 8f18062f..7012c03b 100644 --- a/Mustache/Loader/StringLoader.php +++ b/Mustache/Loader/StringLoader.php @@ -1,42 +1,39 @@ -<?php
-
-/*
- * This file is part of Mustache.php.
- *
- * (c) 2012 Justin Hileman
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
-
-/**
- * Mustache Template string Loader implementation.
- *
- * A StringLoader instance is essentially a noop. It simply passes the 'name' argument straight through:
- *
- * $loader = new StringLoader;
- * $tpl = $loader->load('{{ foo }}'); // '{{ foo }}'
- *
- * This is the default Template Loader instance used by Mustache:
- *
- * $m = new Mustache;
- * $tpl = $m->loadTemplate('{{ foo }}');
- * echo $tpl->render(array('foo' => 'bar')); // "bar"
- *
- * @implements Loader
- */
-class Mustache_Loader_StringLoader implements Mustache_Loader
-{
-
- /**
- * Load a Template by source.
- *
- * @param string $name Mustache Template source
- *
- * @return string Mustache Template source
- */
- public function load($name)
- {
- return $name;
- }
-}
+<?php + +/* + * This file is part of Mustache.php. + * + * (c) 2010-2017 Justin Hileman + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +/** + * Mustache Template string Loader implementation. + * + * A StringLoader instance is essentially a noop. It simply passes the 'name' argument straight through: + * + * $loader = new StringLoader; + * $tpl = $loader->load('{{ foo }}'); // '{{ foo }}' + * + * This is the default Template Loader instance used by Mustache: + * + * $m = new Mustache; + * $tpl = $m->loadTemplate('{{ foo }}'); + * echo $tpl->render(array('foo' => 'bar')); // "bar" + */ +class Mustache_Loader_StringLoader implements Mustache_Loader +{ + /** + * Load a Template by source. + * + * @param string $name Mustache Template source + * + * @return string Mustache Template source + */ + public function load($name) + { + return $name; + } +} |