-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathPatternLoader.php
116 lines (95 loc) · 3.82 KB
/
PatternLoader.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
/*!
* Twig Pattern Engine Loader Class - Patterns
*
* Copyright (c) 2014 Dave Olsen, http://dmolsen.com
* Licensed under the MIT license
*
* Sets an instance of Twig to deal with patterns. Tries to find
* files on system first. If not tries to load them as strings.
*
*/
namespace PatternLab\PatternEngine\Twig\Loaders;
use \PatternLab\Config;
use \PatternLab\Dispatcher;
use \PatternLab\PatternEngine\Twig\Loaders\Twig\PatternPartialLoader as Twig_Loader_PatternPartialLoader;
use \PatternLab\PatternEngine\Twig\Loaders\Twig\PatternStringLoader as Twig_Loader_PatternStringLoader;
use \PatternLab\PatternEngine\Loader;
use \PatternLab\PatternEngine\Twig\TwigUtil;
class PatternLoader extends Loader {
/**
* Load a new Twig instance that uses the Pattern Loader
*/
public function __construct($options = array()) {
// set-up default vars
$twigDebug = Config::getOption("twigDebug");
$twigAutoescape = Config::getOption("twigAutoescape");
// go through various places where things can exist
$filesystemLoaderPaths = array();
// see if source/_macros exists
$macrosPath = Config::getOption("sourceDir").DIRECTORY_SEPARATOR."_macros";
if (is_dir($macrosPath)) {
$filesystemLoaderPaths[] = $macrosPath;
}
// see if source/_layouts exists. if so add it to be searchable
$layoutsPath = Config::getOption("sourceDir").DIRECTORY_SEPARATOR."_layouts";
if (is_dir($layoutsPath)) {
$filesystemLoaderPaths[] = $layoutsPath;
}
// add source/_patterns subdirectories for Drupal theme template compatibility
$patternSourceDir = Config::getOption("sourceDir").DIRECTORY_SEPARATOR."_patterns";
$patternObjects = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($patternSourceDir), \RecursiveIteratorIterator::SELF_FIRST);
$patternObjects->setFlags(\FilesystemIterator::SKIP_DOTS);
// sort the returned objects
$patternObjects = iterator_to_array($patternObjects);
ksort($patternObjects);
foreach ($patternObjects as $name => $object) {
if ($object->isDir()) {
$filesystemLoaderPaths[] = $object->getPathname();
}
}
// 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"]));
// 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();
// set-up Twig
$twigLoader = new \Twig_Loader_Chain($loaders);
$instance = new \Twig_Environment($twigLoader, array("debug" => $twigDebug, "autoescape" => $twigAutoescape));
// customize Twig
TwigUtil::setInstance($instance);
TwigUtil::loadFilters();
TwigUtil::loadFunctions();
TwigUtil::loadTags();
TwigUtil::loadTests();
TwigUtil::loadDateFormats();
TwigUtil::loadDebug();
TwigUtil::loadMacros();
$dispatcherInstance->dispatch("twigLoader.customize");
$dispatcherInstance->dispatch("twigPatternLoader.customize");
// get the instance
$this->instance = TwigUtil::getInstance();
}
/**
* Render a pattern
* @param {Array} the options to be rendered by Twig
*
* @return {String} the rendered result
*/
public function render($options = array()) {
$template = $this->instance->createTemplate($options["pattern"]);
return $template->render($options["data"]);
}
}