Skip to content

Commit b8f0c98

Browse files
authored
Merge pull request #32 from pattern-lab/feature/add-twig-loader-dispatcher-event
Adds event "twigLoaderPreInit.customize"
2 parents a74edca + 6e3c57f commit b8f0c98

File tree

2 files changed

+56
-5
lines changed

2 files changed

+56
-5
lines changed

src/PatternLab/PatternEngine/Twig/Loaders/PatternLoader.php

+14-3
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,26 @@ public function __construct($options = array()) {
6464
// set-up the loader list in order that they should be checked
6565
// 1. Patterns 2. Filesystem 3. String
6666
$loaders = array();
67+
// 1. add Patterns
6768
$loaders[] = new Twig_Loader_PatternPartialLoader(Config::getOption("patternSourceDir"),array("patternPaths" => $options["patternPaths"]));
6869

69-
// add the paths to the filesystem loader if the paths existed
70+
// 2. add the paths to the filesystem loader if the paths existed
7071
if (count($filesystemLoaderPaths) > 0) {
7172
$filesystemLoader = new \Twig_Loader_Filesystem($filesystemLoaderPaths);
7273
$loaders[] = TwigUtil::addPaths($filesystemLoader, $patternSourceDir);
7374
}
75+
76+
// Setting loaders and giving plugins a chance to manipulate them
77+
TwigUtil::setLoaders($loaders);
78+
// set-up the dispatcher
79+
$dispatcherInstance = Dispatcher::getInstance();
80+
$dispatcherInstance->dispatch("twigLoaderPreInit.customize");
81+
// getting the loaders back
82+
$loaders = TwigUtil::getLoaders();
83+
84+
// 3. add String loader
85+
// This *must* go last or no loaders after will work ~ https://github.com/symfony/symfony/issues/10865
86+
// @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
7487
$loaders[] = new \Twig_Loader_String();
7588

7689
// set-up Twig
@@ -87,8 +100,6 @@ public function __construct($options = array()) {
87100
TwigUtil::loadDebug();
88101
TwigUtil::loadMacros();
89102

90-
// set-up the dispatcher
91-
$dispatcherInstance = Dispatcher::getInstance();
92103
$dispatcherInstance->dispatch("twigLoader.customize");
93104
$dispatcherInstance->dispatch("twigPatternLoader.customize");
94105

src/PatternLab/PatternEngine/Twig/TwigUtil.php

+42-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
class TwigUtil {
2020

2121
protected static $instance = '';
22-
22+
protected static $loaders = array();
23+
2324
/**
2425
* Get an instance of the Twig environment
2526
*
@@ -46,7 +47,46 @@ public static function setInstance($instance = "") {
4647
}
4748

4849
self::$instance = $instance;
49-
50+
51+
}
52+
53+
/**
54+
* Get an instance of the Twig loaders
55+
*
56+
* @return {Array} List of Twig Loaders
57+
*/
58+
public static function getLoaders() {
59+
60+
if (empty(self::$loaders)) {
61+
return false;
62+
}
63+
64+
return self::$loaders;
65+
66+
}
67+
68+
/**
69+
* Set an instance of the Twig loaders
70+
* @param {Array} List of Twig Loaders
71+
*/
72+
public static function setLoaders($loaders = array()) {
73+
74+
if (empty($loaders)) {
75+
Console::writeError("please set the loaders");
76+
}
77+
78+
self::$loaders = $loaders;
79+
80+
}
81+
82+
/**
83+
* Add a loader to the Twig Loaders array
84+
* @param {Loader} A Twig Loader
85+
*/
86+
public static function addLoader($loader) {
87+
88+
self::$loaders[] = $loader;
89+
5090
}
5191

5292
/**

0 commit comments

Comments
 (0)