Skip to content

Commit

Permalink
support for events and what not
Browse files Browse the repository at this point in the history
  • Loading branch information
dmolsen committed May 5, 2016
1 parent 0f711e8 commit 8bb0585
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 67 deletions.
44 changes: 27 additions & 17 deletions src/PatternLab/PatternEngine/Twig/Loaders/FilesystemLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace PatternLab\PatternEngine\Twig\Loaders;

use \PatternLab\Config;
use \PatternLab\Dispatcher;
use \PatternLab\PatternEngine\Loader;
use \PatternLab\PatternEngine\Twig\TwigUtil;

Expand All @@ -24,37 +25,46 @@ class FilesystemLoader extends Loader {
public function __construct($options = array()) {

// set-up default vars
$twigDebug = Config::getOption("twigDebug");
$twigDebug = Config::getOption("twigDebug");

// set-up the paths to be searched for templates
$dirPaths = array();
$dirPaths[] = $options["templatePath"];
$dirPaths[] = $options["partialsPath"];
$filesystemLoaderPaths = array();
$filesystemLoaderPaths[] = $options["templatePath"];
$filesystemLoaderPaths[] = $options["partialsPath"];

// see if source/_macros exists. if so add it to be searchable
$macrosPath = Config::getOption("sourceDir").DIRECTORY_SEPARATOR."_macros";
$macrosPath = Config::getOption("sourceDir").DIRECTORY_SEPARATOR."_macros";
if (is_dir($macrosPath)) {
$dirPaths[] = $macrosPath;
$filesystemLoaderPaths[] = $macrosPath;
}

// see if source/_layouts exists. if so add it to be searchable
$layoutsPath = Config::getOption("sourceDir").DIRECTORY_SEPARATOR."_layouts";
$layoutsPath = Config::getOption("sourceDir").DIRECTORY_SEPARATOR."_layouts";
if (is_dir($layoutsPath)) {
$dirPaths[] = $layoutsPath;
$filesystemLoaderPaths[] = $layoutsPath;
}

// set-up Twig
$twigLoader = new \Twig_Loader_Filesystem($dirPaths);
$this->instance = new \Twig_Environment($twigLoader, array("debug" => $twigDebug));
$twigLoader = new \Twig_Loader_Filesystem($filesystemLoaderPaths);
$instance = new \Twig_Environment($twigLoader, array("debug" => $twigDebug));

// customize Twig
$this->instance = TwigUtil::loadFilters($this->instance);
$this->instance = TwigUtil::loadFunctions($this->instance);
$this->instance = TwigUtil::loadTags($this->instance);
$this->instance = TwigUtil::loadTests($this->instance);
$this->instance = TwigUtil::loadDateFormats($this->instance);
$this->instance = TwigUtil::loadDebug($this->instance);
$this->instance = TwigUtil::loadMacros($this->instance);
TwigUtil::setInstance($instance);
TwigUtil::loadFilters();
TwigUtil::loadFunctions();
TwigUtil::loadTags();
TwigUtil::loadTests();
TwigUtil::loadDateFormats();
TwigUtil::loadDebug();
TwigUtil::loadMacros();

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

// add node visitor
$this->instance = TwigUtil::getInstance();

}

Expand Down
51 changes: 30 additions & 21 deletions src/PatternLab/PatternEngine/Twig/Loaders/PatternLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
namespace PatternLab\PatternEngine\Twig\Loaders;

use \PatternLab\Config;
use \PatternLab\PatternEngine\Twig\PatternDataNodeVisitor;
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;
Expand All @@ -28,23 +28,20 @@ class PatternLoader extends Loader {
public function __construct($options = array()) {

// set-up default vars
$twigDebug = Config::getOption("twigDebug");
$twigAutoescape = Config::getOption("twigAutoescape");
$twigDebug = Config::getOption("twigDebug");
$twigAutoescape = Config::getOption("twigAutoescape");

// set-up the loader list
$loaders = array();
// go through various places where things can exist
$filesystemLoaderPaths = array();
$loaders[] = new Twig_Loader_PatternPartialLoader(Config::getOption("patternSourceDir"),array("patternPaths" => $options["patternPaths"]));


// see if source/_macros exists
$macrosPath = Config::getOption("sourceDir").DIRECTORY_SEPARATOR."_macros";
$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";
$layoutsPath = Config::getOption("sourceDir").DIRECTORY_SEPARATOR."_layouts";
if (is_dir($layoutsPath)) {
$filesystemLoaderPaths[] = $layoutsPath;
}
Expand All @@ -63,30 +60,42 @@ public function __construct($options = array()) {
$filesystemLoaderPaths[] = $object->getPathname();
}
}

// set-up the loader list in order that they should be checked
// 1. Patterns 2. Filesystem 3. String
$loaders = array();
$loaders[] = new Twig_Loader_PatternPartialLoader(Config::getOption("patternSourceDir"),array("patternPaths" => $options["patternPaths"]));

// 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);
}

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

// set-up Twig
$twigLoader = new \Twig_Loader_Chain($loaders);
$this->instance = new \Twig_Environment($twigLoader, array("debug" => $twigDebug, "autoescape" => $twigAutoescape));
$twigLoader = new \Twig_Loader_Chain($loaders);
$instance = new \Twig_Environment($twigLoader, array("debug" => $twigDebug, "autoescape" => $twigAutoescape));

// customize Twig
$this->instance = TwigUtil::loadFilters($this->instance);
$this->instance = TwigUtil::loadFunctions($this->instance);
$this->instance = TwigUtil::loadTags($this->instance);
$this->instance = TwigUtil::loadTests($this->instance);
$this->instance = TwigUtil::loadDateFormats($this->instance);
$this->instance = TwigUtil::loadDebug($this->instance);
$this->instance = TwigUtil::loadMacros($this->instance);
TwigUtil::setInstance($instance);
TwigUtil::loadFilters();
TwigUtil::loadFunctions();
TwigUtil::loadTags();
TwigUtil::loadTests();
TwigUtil::loadDateFormats();
TwigUtil::loadDebug();
TwigUtil::loadMacros();
TwigUtil::addNodeVisitor();

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

// add node visitor
$this->instance->addNodeVisitor(new PatternDataNodeVisitor());
$this->instance = TwigUtil::getInstance();

}

/**
Expand Down
46 changes: 28 additions & 18 deletions src/PatternLab/PatternEngine/Twig/Loaders/StringLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace PatternLab\PatternEngine\Twig\Loaders;

use \PatternLab\Config;
use \PatternLab\Dispatcher;
use \PatternLab\PatternEngine\Loader;
use \PatternLab\PatternEngine\Twig\TwigUtil;

Expand All @@ -24,43 +25,52 @@ class StringLoader extends Loader {
public function __construct($options = array()) {

// set-up the defaults
$twigDebug = Config::getOption("twigDebug");
$twigDebug = Config::getOption("twigDebug");

// set-up the loader list
$loaders = array();
// go through various places where things can exist
$filesystemLoaderPaths = array();

// see if source/_macros exists
$macrosPath = Config::getOption("sourceDir").DIRECTORY_SEPARATOR."_macros";
$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";
$layoutsPath = Config::getOption("sourceDir").DIRECTORY_SEPARATOR."_layouts";
if (is_dir($layoutsPath)) {
$filesystemLoaderPaths[] = $layoutsPath;
}

// set-up the loader list
$loaders = array();
// add the paths to the filesystem loader if the paths existed
if (count($filesystemLoaderPaths) > 0) {
$loaders[] = new \Twig_Loader_Filesystem($filesystemLoaderPaths);
$loaders[] = new \Twig_Loader_Filesystem($filesystemLoaderPaths);
}

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

// set-up Twig
$twigLoader = new \Twig_Loader_Chain($loaders);
$this->instance = new \Twig_Environment($twigLoader, array("debug" => $twigDebug));
$twigLoader = new \Twig_Loader_Chain($loaders);
$instance = new \Twig_Environment($twigLoader, array("debug" => $twigDebug));

// customize the loader
$this->instance = TwigUtil::loadFilters($this->instance);
$this->instance = TwigUtil::loadFunctions($this->instance);
$this->instance = TwigUtil::loadTags($this->instance);
$this->instance = TwigUtil::loadTests($this->instance);
$this->instance = TwigUtil::loadDateFormats($this->instance);
$this->instance = TwigUtil::loadDebug($this->instance);
$this->instance = TwigUtil::loadMacros($this->instance);
// customize Twig
TwigUtil::setInstance($instance);
TwigUtil::loadFilters();
TwigUtil::loadFunctions();
TwigUtil::loadTags();
TwigUtil::loadTests();
TwigUtil::loadDateFormats();
TwigUtil::loadDebug();
TwigUtil::loadMacros();

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

// add node visitor
$this->instance = TwigUtil::getInstance();

}

Expand Down
32 changes: 21 additions & 11 deletions src/PatternLab/PatternEngine/Twig/TwigUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@

use \PatternLab\Config;
use \PatternLab\Console;
use \PatternLab\PatternEngine\Twig\PatternDataNodeVisitor;
use \Symfony\Component\Finder\Finder;

class TwigUtil {

protected $instance = '';
protected static $instance = '';

/**
* Get an instance of the Twig environment
Expand All @@ -27,11 +28,11 @@ class TwigUtil {
*/
public static function getInstance() {

if (empty($this->instance)) {
if (empty(self::$instance)) {
return false;
}

return $this->instance;
return self::$instance;

}

Expand All @@ -45,7 +46,16 @@ public static function setInstance($instance = "") {
Console::writeError("please set the instance");
}

$this->instance = $instance;
self::$instance = $instance;

}

/**
* Adds a node visitor
*/
public static function addNodeVisitor() {

self::$instance->addNodeVisitor(new PatternDataNodeVisitor());

}

Expand Down Expand Up @@ -79,7 +89,7 @@ public static function loadDateFormats() {
$intervalFormat = Config::getOption("twigDefaultIntervalFormat");

if ($dateFormat && $intervalFormat && !empty($dateFormat) && !empty($intervalFormat)) {
$this->instance->getExtension("core")->setDateFormat($dateFormat, $intervalFormat);
self::$instance->getExtension("core")->setDateFormat($dateFormat, $intervalFormat);
}

}
Expand All @@ -90,7 +100,7 @@ public static function loadDateFormats() {
public static function loadDebug() {

if (Config::getOption("twigDebug")) {
$this->instance->addExtension(new \Twig_Extension_Debug());
self::$instance->addExtension(new \Twig_Extension_Debug());
}

}
Expand Down Expand Up @@ -121,7 +131,7 @@ public static function loadFilters() {

// $filter should be defined in the included file
if (isset($filter)) {
$this->instance->addFilter($filter);
self::$instance->addFilter($filter);
unset($filter);
}

Expand Down Expand Up @@ -159,7 +169,7 @@ public static function loadFunctions() {

// $function should be defined in the included file
if (isset($function)) {
$this->instance->addFunction($function);
self::$instance->addFunction($function);
unset($function);
}

Expand Down Expand Up @@ -194,7 +204,7 @@ public static function loadMacros() {
if ($baseName[0] != "_") {

// add the macro to the global context
$this->instance->addGlobal($file->getBasename(".".$macroExt), $this->instance->loadTemplate($baseName));
self::$instance->addGlobal($file->getBasename(".".$macroExt), self::$instance->loadTemplate($baseName));

}

Expand Down Expand Up @@ -230,7 +240,7 @@ public static function loadTags() {

// Project_{filenameBase}_TokenParser should be defined in the include
$className = "Project_".$file->getBasename(".".$tagExt)."_TokenParser";
$this->instance->addTokenParser(new $className());
self::$instance->addTokenParser(new $className());

}

Expand Down Expand Up @@ -266,7 +276,7 @@ public static function loadTests() {

// $test should be defined in the included file
if (isset($test)) {
$this->instance->addTest($test);
self::$instance->addTest($test);
unset($test);
}

Expand Down

0 comments on commit 8bb0585

Please sign in to comment.