Skip to content

Commit 3c50e98

Browse files
authored
Merge pull request #3 from drupal-pattern-lab/master
Sync Master with Dev Branch
2 parents a2657fa + f9406e4 commit 3c50e98

File tree

3 files changed

+70
-8
lines changed

3 files changed

+70
-8
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
}
2525
},
2626
"require": {
27-
"pattern-lab/core": "dev-dev",
27+
"pattern-lab/core": "^2.0.0",
2828
"twig/twig": "~1.0"
2929
},
3030
"extra": {

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

Lines changed: 25 additions & 4 deletions
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

@@ -105,7 +116,17 @@ public function __construct($options = array()) {
105116
*/
106117
public function render($options = array()) {
107118

108-
return $this->instance->render($options["pattern"], $options["data"]);
119+
$result = $this->instance->render($options["pattern"], $options["data"]);
120+
// This error handler catches files that didn't render using any of the loaders.
121+
// The most common scenario is when a file's contents get passed to and through `Twig_Loader_String` and
122+
// outputs the raw Twig file contents like `@atoms/buttons/button.twig`.
123+
// @todo Remove this once `Twig_Loader_String` is removed.
124+
if (strpos($result, "@") === 0) {
125+
echo "Twig file not found: " . $result . "\n";
126+
exit(1);
127+
} else {
128+
return $result;
129+
}
109130

110131
}
111132

src/PatternLab/PatternEngine/Twig/TwigUtil.php

Lines changed: 44 additions & 3 deletions
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
/**
@@ -61,7 +101,8 @@ public static function addPaths($filesystemLoader, $patternSourceDir) {
61101
$finder = new Finder();
62102
$finder->directories()->depth(0)->in($patternSourceDir);
63103
foreach ($finder as $file) {
64-
$patternBits = explode("-",$file->getRelativePathName(),2);
104+
$pattern = $file->getRelativePathName();
105+
$patternBits = explode("-",$pattern,2);
65106
$patternTypePath = (((int)$patternBits[0] != 0) || ($patternBits[0] == '00')) ? $patternBits[1] : $pattern;
66107
$filesystemLoader->addPath($file->getPathName(), $patternTypePath);
67108
}

0 commit comments

Comments
 (0)