From 29be204638c095030d16972173c08e165c03998d Mon Sep 17 00:00:00 2001 From: Salem Ghoweri Date: Sat, 2 Sep 2017 17:46:01 -0400 Subject: [PATCH] Working POC of optionally allowing default Pattern Lab rules to be disabled in addition to providing a way to load additional extra rules as well to allow for deeper customization than previously possible. Solves use cases described in #11 --- src/PatternLab/PatternData.php | 40 +++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/src/PatternLab/PatternData.php b/src/PatternLab/PatternData.php index 7980305c..f3d05a05 100644 --- a/src/PatternLab/PatternData.php +++ b/src/PatternLab/PatternData.php @@ -386,18 +386,52 @@ public static function getRules() { } - /** - * Load all of the rules related to Pattern Data + +/** + * Load all of the rules related to Pattern Data, plus any extra add-ons */ public static function loadRules($options) { + // First handle default PL rules, minus any being disabled foreach (glob(__DIR__."/PatternData/Rules/*.php") as $filename) { $ruleName = str_replace(".php","",str_replace(__DIR__."/PatternData/Rules/","",$filename)); - if ($ruleName[0] != "_") { + + $disabledRules = array(); + if (Config::getOption("disabledPatternRules")) { + $disabledRules = Config::getOption("disabledPatternRules"); + } + + // Load all rules that aren't on the disabledPatternRules list + if (($ruleName[0] != "_") && (!in_array($ruleName, $disabledRules))) { $ruleClass = "\PatternLab\PatternData\Rules\\".$ruleName; $rule = new $ruleClass($options); self::setRule($ruleName, $rule); } } + + // Then handle any extra rules to add on top of the default PL rules + $extraPatternRulesDir = Config::getOption("sourceDir") . '/_extensions/rules'; // Default extra rules location to check + if (Config::getOption("extraPatternRulesDir")) { + $extraPatternRulesDir = Config::getOption("extraPatternRulesDir"); + } + + if (is_dir($extraPatternRulesDir)){ + foreach (glob($extraPatternRulesDir . "/*.php") as $filename) { + $ruleName = str_replace(".php","",str_replace($extraPatternRulesDir . "/","",$filename)); + + $extraRules = array(); + if (Config::getOption("extraPatternRules")) { + $extraRules = Config::getOption("extraPatternRules"); + } + + // Only load extra rules that are on the extraPatternRules list + if (($ruleName[0] != "_") && (in_array($ruleName, $extraRules))) { + require_once($filename); // Pull in extra rule so we can use it + $ruleClass = "\PatternLab\PatternData\Rules\\".$ruleName; + $rule = new $ruleClass($options); + self::setRule($ruleName, $rule); + } + } + } } /**