|
| 1 | +<?php |
| 2 | + |
| 3 | +/*! |
| 4 | + * Twig Pattern Engine Loader Class - String |
| 5 | + * |
| 6 | + * Copyright (c) 2014 Dave Olsen, http://dmolsen.com |
| 7 | + * Licensed under the MIT license |
| 8 | + * |
| 9 | + * Sets an instance of Twig to deal with rendering of strings |
| 10 | + * |
| 11 | + */ |
| 12 | + |
| 13 | +namespace PatternLab\PatternEngine\Twig; |
| 14 | + |
| 15 | +use \PatternLab\Config; |
| 16 | +use \PatternLab\Console; |
| 17 | +use \Symfony\Component\Finder\Finder; |
| 18 | + |
| 19 | +class TwigUtil { |
| 20 | + |
| 21 | + /** |
| 22 | + * Load macros for the Twig PatternEngine |
| 23 | + * @param {Instance} an instance of the twig engine |
| 24 | + * @param {String} description of the loader type for the error |
| 25 | + * |
| 26 | + * @return {Instance} an instance of the twig engine |
| 27 | + */ |
| 28 | + public static function loadMacros($instance, $instanceType) { |
| 29 | + |
| 30 | + // load defaults |
| 31 | + $macroDir = Config::getOption("sourceDir").DIRECTORY_SEPARATOR."_macros"; |
| 32 | + $macroExt = Config::getOption("twigMacroExt"); |
| 33 | + $macroExt = $macroExt ? $macroExt : "macro"; |
| 34 | + |
| 35 | + if (is_dir($macroDir)) { |
| 36 | + |
| 37 | + // loop through some macro containing dir and run... |
| 38 | + $finder = new Finder(); |
| 39 | + $finder->files()->name("*.".$macroExt)->in($macroDir); |
| 40 | + $finder->sortByName(); |
| 41 | + foreach ($finder as $file) { |
| 42 | + $instance->addGlobal($file->getBasename($macroExt), $twig->loadTemplate($file->getRealPath())); |
| 43 | + } |
| 44 | + |
| 45 | + } else { |
| 46 | + |
| 47 | + // write warning because the macro dir doesn't exist |
| 48 | + $macroDirHR = Console::getHumanReadablePath($macroDir); |
| 49 | + Console::writeWarning("the path <path>".$macroDirHR."</path> doesn't exist so macros won't be loaded for the ".$instanceType." loader..."); |
| 50 | + |
| 51 | + } |
| 52 | + |
| 53 | + return $instance; |
| 54 | + |
| 55 | + } |
| 56 | + |
| 57 | +} |
0 commit comments