|
| 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 custom date formats for Twig |
| 23 | + * @param {Instance} an instance of the twig engine |
| 24 | + * |
| 25 | + * @return {Instance} an instance of the twig engine |
| 26 | + */ |
| 27 | + public static function loadDateFormats($instance) { |
| 28 | + |
| 29 | + $dateFormat = Config::getOption("twigDefaultDateFormat"); |
| 30 | + $intervalFormat = Config::getOption("twigDefaultIntervalFormat"); |
| 31 | + |
| 32 | + if ($dateFormat && $intervalFormat && !empty($dateFormat) && !empty($intervalFormat)) { |
| 33 | + $instance->getExtension("core")->setDateFormat($dateFormat, $intervalFormat); |
| 34 | + } |
| 35 | + |
| 36 | + return $instance; |
| 37 | + |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Enable the debug options for Twig |
| 42 | + * @param {Instance} an instance of the twig engine |
| 43 | + * |
| 44 | + * @return {Instance} an instance of the twig engine |
| 45 | + */ |
| 46 | + public static function loadDebug($instance) { |
| 47 | + |
| 48 | + if (Config::getOption("twigDebug")) { |
| 49 | + $instance->addExtension(new \Twig_Extension_Debug()); |
| 50 | + } |
| 51 | + |
| 52 | + return $instance; |
| 53 | + |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Load macros for the Twig PatternEngine |
| 58 | + * @param {Instance} an instance of the twig engine |
| 59 | + * @param {String} description of the loader type for the error |
| 60 | + * |
| 61 | + * @return {Instance} an instance of the twig engine |
| 62 | + */ |
| 63 | + public static function loadMacros($instance, $instanceType) { |
| 64 | + |
| 65 | + // load defaults |
| 66 | + $macroDir = Config::getOption("sourceDir").DIRECTORY_SEPARATOR."_macros"; |
| 67 | + $macroExt = Config::getOption("twigMacroExt"); |
| 68 | + $macroExt = $macroExt ? $macroExt : "macro"; |
| 69 | + |
| 70 | + if (is_dir($macroDir)) { |
| 71 | + |
| 72 | + // loop through some macro containing dir and run... |
| 73 | + $finder = new Finder(); |
| 74 | + $finder->files()->name("*.".$macroExt)->in($macroDir); |
| 75 | + $finder->sortByName(); |
| 76 | + foreach ($finder as $file) { |
| 77 | + $instance->addGlobal($file->getBasename(".".$macroExt), $instance->loadTemplate($file->getBasename())); |
| 78 | + } |
| 79 | + |
| 80 | + } else { |
| 81 | + |
| 82 | + // write warning because the macro dir doesn't exist |
| 83 | + $macroDirHR = Console::getHumanReadablePath($macroDir); |
| 84 | + Console::writeWarning("the path <path>".$macroDirHR."</path> doesn't exist so macros won't be loaded for the ".$instanceType." loader..."); |
| 85 | + |
| 86 | + } |
| 87 | + |
| 88 | + return $instance; |
| 89 | + |
| 90 | + } |
| 91 | + |
| 92 | +} |
0 commit comments