From 11ee4e9010beeb9e09cbc6d7fb1e44259641d85e Mon Sep 17 00:00:00 2001 From: Erik Mogensen Date: Wed, 21 Dec 2016 12:58:38 +0100 Subject: [PATCH] Token-like patterns no longer treated as templates If you have templates that have typos in their includes, the use of the Twig_Loader_String causes the typo to be echoed out: {% include "foo" %} results in the string "foo" to be printed out unless "foo" is a twig template. This change causes "foo" to no longer be printed out, but template generation to fail, because "foo" is not present. This change also allows more of the twig features to be used, such as conditional includes and dynamic includes: {% set foo = "something" %} {% include [ foo, "fallback" ] %} Before this change, the string "something" would always be printed. After the change, either the template "something" is included, otherwise, the template "fallback" is included, or else an error is raised: Twig_Error_Loader: Unable to find one of the following templates: "something", "fallback" --- .../Twig/Loaders/PatternLoader.php | 2 +- .../Twig/Loaders/StringLoader.php | 2 +- .../Loaders/Twig/ConditionalStringLoader.php | 31 +++++++++++++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 src/PatternLab/PatternEngine/Twig/Loaders/Twig/ConditionalStringLoader.php diff --git a/src/PatternLab/PatternEngine/Twig/Loaders/PatternLoader.php b/src/PatternLab/PatternEngine/Twig/Loaders/PatternLoader.php index fa2ce2f..87a73f8 100644 --- a/src/PatternLab/PatternEngine/Twig/Loaders/PatternLoader.php +++ b/src/PatternLab/PatternEngine/Twig/Loaders/PatternLoader.php @@ -84,7 +84,7 @@ public function __construct($options = array()) { // 3. add String loader // This *must* go last or no loaders after will work ~ https://github.com/symfony/symfony/issues/10865 // @todo Remove `Twig_Loader_String` - if a Twig include path is wrong, this outputs the string anyway with no error ~ https://github.com/symfony/symfony/issues/10865 - $loaders[] = new \Twig_Loader_String(); + $loaders[] = new Twig\ConditionalStringLoader(); // set-up Twig $twigLoader = new \Twig_Loader_Chain($loaders); diff --git a/src/PatternLab/PatternEngine/Twig/Loaders/StringLoader.php b/src/PatternLab/PatternEngine/Twig/Loaders/StringLoader.php index e81c9b7..276ad7d 100644 --- a/src/PatternLab/PatternEngine/Twig/Loaders/StringLoader.php +++ b/src/PatternLab/PatternEngine/Twig/Loaders/StringLoader.php @@ -48,7 +48,7 @@ public function __construct($options = array()) { if (count($filesystemLoaderPaths) > 0) { $loaders[] = new \Twig_Loader_Filesystem($filesystemLoaderPaths); } - $loaders[] = new \Twig_Loader_String(); + $loaders[] = new Twig\ConditionalStringLoader(); // set-up Twig $twigLoader = new \Twig_Loader_Chain($loaders); diff --git a/src/PatternLab/PatternEngine/Twig/Loaders/Twig/ConditionalStringLoader.php b/src/PatternLab/PatternEngine/Twig/Loaders/Twig/ConditionalStringLoader.php new file mode 100644 index 0000000..f8e814b --- /dev/null +++ b/src/PatternLab/PatternEngine/Twig/Loaders/Twig/ConditionalStringLoader.php @@ -0,0 +1,31 @@ +exists($name)) { + return parent::getSourceContext($name); + } + return null; + } + + /** + * Return false if $name looks like a simple file name, true otherwise. + * + * A simple file name is a simple string consisting of alphanumerics, + * periods, hyphens, underbars, and so on, otherwise + */ + public function exists($name) { + return preg_match("/^[-a-zA-Z0-9~\\.\\/_@]+$/", $name) === 0; + } +}