Skip to content

Commit 76d4440

Browse files
committed
adding addPaths support
1 parent 3888ca2 commit 76d4440

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ public function __construct($options = array()) {
6666

6767
// add the paths to the filesystem loader if the paths existed
6868
if (count($filesystemLoaderPaths) > 0) {
69-
$loaders[] = new \Twig_Loader_Filesystem($filesystemLoaderPaths);
69+
$filesystemLoader = new \Twig_Loader_Filesystem($filesystemLoaderPaths);
70+
$loaders[] = TwigUtil::addPaths($filesystemLoader, $patternSourceDir);
7071
}
7172

7273
$loaders[] = new \Twig_Loader_String();

src/PatternLab/PatternEngine/Twig/TwigUtil.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,26 @@
1818

1919
class TwigUtil {
2020

21+
/**
22+
* Registering each directory under `_patterns/` as a namespace. For example, `_patterns/00-atoms/` as `@atoms`
23+
* @param {Instance} an instance of the filesystem Loader
24+
* @param {String} the path to the pattern directory
25+
*
26+
* @return {Instance} an instance of the filesystem Loader
27+
*/
28+
public static function addPaths($filesystemLoader, $patternSourceDir) {
29+
30+
$finder = new Finder();
31+
$finder->directories()->depth(0)->in($patternSourceDir);
32+
foreach ($finder as $file) {
33+
$patternBits = explode("-",$file->getRelativePathName(),2);
34+
$patternTypePath = (((int)$patternBits[0] != 0) || ($patternBits[0] == '00')) ? $patternBits[1] : $pattern;
35+
$filesystemLoader->addPath($file->getPathName(), $patternTypePath);
36+
}
37+
38+
return $filesystemLoader;
39+
40+
}
2141
/**
2242
* Load custom date formats for Twig
2343
* @param {Instance} an instance of the twig engine

0 commit comments

Comments
 (0)