Skip to content

Commit 29be204

Browse files
committed
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
1 parent 1fa44ec commit 29be204

File tree

1 file changed

+37
-3
lines changed

1 file changed

+37
-3
lines changed

src/PatternLab/PatternData.php

+37-3
Original file line numberDiff line numberDiff line change
@@ -386,18 +386,52 @@ public static function getRules() {
386386

387387
}
388388

389-
/**
390-
* Load all of the rules related to Pattern Data
389+
390+
/**
391+
* Load all of the rules related to Pattern Data, plus any extra add-ons
391392
*/
392393
public static function loadRules($options) {
394+
// First handle default PL rules, minus any being disabled
393395
foreach (glob(__DIR__."/PatternData/Rules/*.php") as $filename) {
394396
$ruleName = str_replace(".php","",str_replace(__DIR__."/PatternData/Rules/","",$filename));
395-
if ($ruleName[0] != "_") {
397+
398+
$disabledRules = array();
399+
if (Config::getOption("disabledPatternRules")) {
400+
$disabledRules = Config::getOption("disabledPatternRules");
401+
}
402+
403+
// Load all rules that aren't on the disabledPatternRules list
404+
if (($ruleName[0] != "_") && (!in_array($ruleName, $disabledRules))) {
396405
$ruleClass = "\PatternLab\PatternData\Rules\\".$ruleName;
397406
$rule = new $ruleClass($options);
398407
self::setRule($ruleName, $rule);
399408
}
400409
}
410+
411+
// Then handle any extra rules to add on top of the default PL rules
412+
$extraPatternRulesDir = Config::getOption("sourceDir") . '/_extensions/rules'; // Default extra rules location to check
413+
if (Config::getOption("extraPatternRulesDir")) {
414+
$extraPatternRulesDir = Config::getOption("extraPatternRulesDir");
415+
}
416+
417+
if (is_dir($extraPatternRulesDir)){
418+
foreach (glob($extraPatternRulesDir . "/*.php") as $filename) {
419+
$ruleName = str_replace(".php","",str_replace($extraPatternRulesDir . "/","",$filename));
420+
421+
$extraRules = array();
422+
if (Config::getOption("extraPatternRules")) {
423+
$extraRules = Config::getOption("extraPatternRules");
424+
}
425+
426+
// Only load extra rules that are on the extraPatternRules list
427+
if (($ruleName[0] != "_") && (in_array($ruleName, $extraRules))) {
428+
require_once($filename); // Pull in extra rule so we can use it
429+
$ruleClass = "\PatternLab\PatternData\Rules\\".$ruleName;
430+
$rule = new $ruleClass($options);
431+
self::setRule($ruleName, $rule);
432+
}
433+
}
434+
}
401435
}
402436

403437
/**

0 commit comments

Comments
 (0)