Skip to content

Sync Master with Dev Branch #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
May 18, 2017
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
}
},
"require": {
"pattern-lab/core": "dev-dev",
"pattern-lab/core": "^2.0.0",
"twig/twig": "~1.0"
},
"extra": {
Expand Down
29 changes: 25 additions & 4 deletions src/PatternLab/PatternEngine/Twig/Loaders/PatternLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,26 @@ public function __construct($options = array()) {
// 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"]));

// add the paths to the filesystem loader if the paths existed
// 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();

// 3. add String loader
// This *must* go last or no loaders after will work ~ https://github.com/symfony/symfony/issues/10865
// @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
$loaders[] = new \Twig_Loader_String();

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

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

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

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

}

Expand Down
47 changes: 44 additions & 3 deletions src/PatternLab/PatternEngine/Twig/TwigUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
class TwigUtil {

protected static $instance = '';

protected static $loaders = array();

/**
* Get an instance of the Twig environment
*
Expand All @@ -46,7 +47,46 @@ public static function setInstance($instance = "") {
}

self::$instance = $instance;


}

/**
* Get an instance of the Twig loaders
*
* @return {Array} List of Twig Loaders
*/
public static function getLoaders() {

if (empty(self::$loaders)) {
return false;
}

return self::$loaders;

}

/**
* Set an instance of the Twig loaders
* @param {Array} List of Twig Loaders
*/
public static function setLoaders($loaders = array()) {

if (empty($loaders)) {
Console::writeError("please set the loaders");
}

self::$loaders = $loaders;

}

/**
* Add a loader to the Twig Loaders array
* @param {Loader} A Twig Loader
*/
public static function addLoader($loader) {

self::$loaders[] = $loader;

}

/**
Expand All @@ -61,7 +101,8 @@ public static function addPaths($filesystemLoader, $patternSourceDir) {
$finder = new Finder();
$finder->directories()->depth(0)->in($patternSourceDir);
foreach ($finder as $file) {
$patternBits = explode("-",$file->getRelativePathName(),2);
$pattern = $file->getRelativePathName();
$patternBits = explode("-",$pattern,2);
$patternTypePath = (((int)$patternBits[0] != 0) || ($patternBits[0] == '00')) ? $patternBits[1] : $pattern;
$filesystemLoader->addPath($file->getPathName(), $patternTypePath);
}
Expand Down