From fd6d8e2fd2a98efdc0c070788a8ad9aceb5e724b Mon Sep 17 00:00:00 2001 From: Dave Olsen Date: Wed, 18 May 2016 23:02:14 -0400 Subject: [PATCH 1/5] updating dependencies --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 83e723e..2108fdc 100644 --- a/composer.json +++ b/composer.json @@ -25,7 +25,7 @@ }, "require": { "pattern-lab/core": "^2.0.0", - "twig/twig": "~1.*" + "twig/twig": "~1.0" }, "extra": { "patternlab": { From 7f5647b8c5843b60174b4e39996b0485c475e091 Mon Sep 17 00:00:00 2001 From: bradwade Date: Fri, 23 Sep 2016 14:27:09 -0400 Subject: [PATCH 2/5] Fixing patternTypePath when not prefixing folders with numbers and dash --- src/PatternLab/PatternEngine/Twig/TwigUtil.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/PatternLab/PatternEngine/Twig/TwigUtil.php b/src/PatternLab/PatternEngine/Twig/TwigUtil.php index eaedd53..b483cd9 100644 --- a/src/PatternLab/PatternEngine/Twig/TwigUtil.php +++ b/src/PatternLab/PatternEngine/Twig/TwigUtil.php @@ -61,7 +61,8 @@ public static function addPaths($filesystemLoader, $patternSourceDir) { $finder = new Finder(); $finder->directories()->depth(0)->in($patternSourceDir); foreach ($finder as $file) { - $patternBits = explode("-",$file->getRelativePathName(),2); + $pattern = $file->getRelativePathName(); + $patternBits = explode("-",$pattern,2); $patternTypePath = (((int)$patternBits[0] != 0) || ($patternBits[0] == '00')) ? $patternBits[1] : $pattern; $filesystemLoader->addPath($file->getPathName(), $patternTypePath); } From 6e3c57f82a0b62588867c7f4c91905dc8977d221 Mon Sep 17 00:00:00 2001 From: Evan Lovely Date: Tue, 11 Oct 2016 12:45:41 -0700 Subject: [PATCH 3/5] Adds event "twigLoaderPreInit.customize" that allows manipulation of Twig Loaders before Init of Twig Environment. --- .../Twig/Loaders/PatternLoader.php | 17 +++++-- .../PatternEngine/Twig/TwigUtil.php | 44 ++++++++++++++++++- 2 files changed, 56 insertions(+), 5 deletions(-) diff --git a/src/PatternLab/PatternEngine/Twig/Loaders/PatternLoader.php b/src/PatternLab/PatternEngine/Twig/Loaders/PatternLoader.php index 3d01683..92371b1 100644 --- a/src/PatternLab/PatternEngine/Twig/Loaders/PatternLoader.php +++ b/src/PatternLab/PatternEngine/Twig/Loaders/PatternLoader.php @@ -64,13 +64,26 @@ public function __construct($options = array()) { // set-up the loader list in order that they should be checked // 1. Patterns 2. Filesystem 3. String $loaders = array(); + // 1. add Patterns $loaders[] = new Twig_Loader_PatternPartialLoader(Config::getOption("patternSourceDir"),array("patternPaths" => $options["patternPaths"])); - // add the paths to the filesystem loader if the paths existed + // 2. add the paths to the filesystem loader if the paths existed if (count($filesystemLoaderPaths) > 0) { $filesystemLoader = new \Twig_Loader_Filesystem($filesystemLoaderPaths); $loaders[] = TwigUtil::addPaths($filesystemLoader, $patternSourceDir); } + + // Setting loaders and giving plugins a chance to manipulate them + TwigUtil::setLoaders($loaders); + // set-up the dispatcher + $dispatcherInstance = Dispatcher::getInstance(); + $dispatcherInstance->dispatch("twigLoaderPreInit.customize"); + // getting the loaders back + $loaders = TwigUtil::getLoaders(); + + // 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(); // set-up Twig @@ -87,8 +100,6 @@ public function __construct($options = array()) { TwigUtil::loadDebug(); TwigUtil::loadMacros(); - // set-up the dispatcher - $dispatcherInstance = Dispatcher::getInstance(); $dispatcherInstance->dispatch("twigLoader.customize"); $dispatcherInstance->dispatch("twigPatternLoader.customize"); diff --git a/src/PatternLab/PatternEngine/Twig/TwigUtil.php b/src/PatternLab/PatternEngine/Twig/TwigUtil.php index b483cd9..64f97da 100644 --- a/src/PatternLab/PatternEngine/Twig/TwigUtil.php +++ b/src/PatternLab/PatternEngine/Twig/TwigUtil.php @@ -19,7 +19,8 @@ class TwigUtil { protected static $instance = ''; - + protected static $loaders = array(); + /** * Get an instance of the Twig environment * @@ -46,7 +47,46 @@ public static function setInstance($instance = "") { } self::$instance = $instance; - + + } + + /** + * Get an instance of the Twig loaders + * + * @return {Array} List of Twig Loaders + */ + public static function getLoaders() { + + if (empty(self::$loaders)) { + return false; + } + + return self::$loaders; + + } + + /** + * Set an instance of the Twig loaders + * @param {Array} List of Twig Loaders + */ + public static function setLoaders($loaders = array()) { + + if (empty($loaders)) { + Console::writeError("please set the loaders"); + } + + self::$loaders = $loaders; + + } + + /** + * Add a loader to the Twig Loaders array + * @param {Loader} A Twig Loader + */ + public static function addLoader($loader) { + + self::$loaders[] = $loader; + } /** From 6e16b3deca4e94016bfe6209504fb3e5db7b586e Mon Sep 17 00:00:00 2001 From: Evan Lovely Date: Wed, 12 Oct 2016 14:26:47 -0700 Subject: [PATCH 4/5] Throwing error on uncompiled templates --- .../PatternEngine/Twig/Loaders/PatternLoader.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/PatternLab/PatternEngine/Twig/Loaders/PatternLoader.php b/src/PatternLab/PatternEngine/Twig/Loaders/PatternLoader.php index 92371b1..8d0ae3a 100644 --- a/src/PatternLab/PatternEngine/Twig/Loaders/PatternLoader.php +++ b/src/PatternLab/PatternEngine/Twig/Loaders/PatternLoader.php @@ -116,7 +116,16 @@ public function __construct($options = array()) { */ public function render($options = array()) { - return $this->instance->render($options["pattern"], $options["data"]); + $result = $this->instance->render($options["pattern"], $options["data"]); + // This error handler catches files that didn't render using any of the loaders. + // The most common scenario is when a file's contents get passed to and through `Twig_Loader_String` and + // outputs the raw Twig file contents like `@atoms/buttons/button.twig`. + // @todo Remove this once `Twig_Loader_String` is removed. + if (strpos($result, "@") === 0) { + throw new \Twig_Error_Loader("Twig file not found: " . $result . "\n"); + } else { + return $result; + } } From 65324c72d2fb3a10cb25bbd65ce08a89fefc5896 Mon Sep 17 00:00:00 2001 From: Evan Lovely Date: Wed, 12 Oct 2016 14:51:54 -0700 Subject: [PATCH 5/5] Improving signal to noise ratio on error --- src/PatternLab/PatternEngine/Twig/Loaders/PatternLoader.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/PatternLab/PatternEngine/Twig/Loaders/PatternLoader.php b/src/PatternLab/PatternEngine/Twig/Loaders/PatternLoader.php index 8d0ae3a..fa2ce2f 100644 --- a/src/PatternLab/PatternEngine/Twig/Loaders/PatternLoader.php +++ b/src/PatternLab/PatternEngine/Twig/Loaders/PatternLoader.php @@ -122,7 +122,8 @@ public function render($options = array()) { // outputs the raw Twig file contents like `@atoms/buttons/button.twig`. // @todo Remove this once `Twig_Loader_String` is removed. if (strpos($result, "@") === 0) { - throw new \Twig_Error_Loader("Twig file not found: " . $result . "\n"); + echo "Twig file not found: " . $result . "\n"; + exit(1); } else { return $result; }