diff --git a/README.md b/README.md index 2358685..fe70eb5 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ # Data Inheritance Plugin for Pattern Lab -The Data Inheritance Plugin allows patterns to inherit data from patterns within its lineage. This means that data from included patterns is merged with the current pattern. The current pattern's data takes precedence. +The Data Inheritance Plugin allows patterns to inherit data from patterns within its lineage and sub-lineage recursively. This means that data from included patterns is merged with the current pattern. The current pattern's data takes precedence. ## Installation diff --git a/src/PatternLab/DataInheritance/PatternLabListener.php b/src/PatternLab/DataInheritance/PatternLabListener.php index 366ce29..776c663 100644 --- a/src/PatternLab/DataInheritance/PatternLabListener.php +++ b/src/PatternLab/DataInheritance/PatternLabListener.php @@ -17,60 +17,74 @@ use \PatternLab\PatternData; class PatternLabListener extends \PatternLab\Listener { - - /** - * Add the listeners for this plug-in - */ - public function __construct() { - - // add listener - $this->addListener("patternData.lineageHelperEnd","inherit"); - - } - - /** - * Look up data in lineages, update pattern store data, replace store - */ - public function inherit() { - - if ((bool)Config::getOption("plugins.dataInheritance.enabled")) { - - $storeData = Data::get(); - $storePatternData = PatternData::get(); - - foreach ($storePatternData as $patternStoreKey => $patternData) { - - if (isset($patternData["lineages"]) && (count($patternData["lineages"]) > 0)) { - - $dataLineage = array(); - - foreach($patternData["lineages"] as $lineage) { - - // merge the lineage data with the lineage store. newer/higher-level data is more important. + + protected $storeData; + protected $storePatternData; + // To keep track of the processed patterns + protected $processedPatterns; + + /** + * Add the listeners for this plug-in + */ + public function __construct() { + + // add listener + $this->addListener("patternData.lineageHelperEnd", "inherit"); + + } + + /** + * Look up data in lineages, update pattern store data, replace store + */ + public function inherit() { + + if ((bool)Config::getOption("plugins.dataInheritance.enabled")) { + $this->storeData = Data::get(); + $this->storePatternData = PatternData::get(); + $this->processedPatterns = []; + foreach ($this->storePatternData as $patternKey => $patternData) { + if (isset($patternData["lineages"]) && is_array($patternData["lineages"]) && (count($patternData["lineages"]) > 0)) { + if (in_array($patternKey, $this->processedPatterns)) { + continue; + } + $this->processPatternData($patternKey); + } + } + Data::replaceStore($this->storeData); + } + + } + + private function processPatternData($patternKey) { + + $patternData = $this->storePatternData[$patternKey]; + $dataLineage = array(); + + foreach ($patternData["lineages"] as $lineage) { + $lineageKey = $lineage["lineagePattern"]; - $lineageData = isset($storeData["patternSpecific"][$lineageKey]) && isset($storeData["patternSpecific"][$lineageKey]["data"]) ? $storeData["patternSpecific"][$lineageKey]["data"] : array(); + // process sub-lineages first with recursive calls + if (isset($this->storePatternData[$lineageKey]['lineages']) && count($this->storePatternData[$lineageKey]['lineages']) > 0) { + $this->processPatternData($lineageKey); + } + // merge the lineage data with the lineage store. newer/higher-level data is more important. + $lineageData = isset($this->storeData["patternSpecific"][$lineageKey]) && isset($this->storeData["patternSpecific"][$lineageKey]["data"]) ? $this->storeData["patternSpecific"][$lineageKey]["data"] : array(); + if (!empty($lineageData)) { - $dataLineage = array_replace_recursive($dataLineage, $lineageData); + $dataLineage = array_replace_recursive($dataLineage, $lineageData); } - - } - - // merge the lineage data with the pattern data. pattern data is more important. - $dataPattern = isset($storeData["patternSpecific"][$patternStoreKey]) && isset($storeData["patternSpecific"][$patternStoreKey]["data"]) ? $storeData["patternSpecific"][$patternStoreKey]["data"] : array(); - $dataPattern = array_replace_recursive($dataLineage, $dataPattern); - - if (!empty($dataPattern)) { - $storeData["patternSpecific"][$patternStoreKey]["data"] = $dataPattern; - } - + + } + + // merge the lineage data with the pattern data. pattern data is more important. + $dataPattern = isset($this->storeData["patternSpecific"][$patternKey]) && isset($this->storeData["patternSpecific"][$patternKey]["data"]) ? $this->storeData["patternSpecific"][$patternKey]["data"] : array(); + $dataPattern = array_replace_recursive($dataLineage, $dataPattern); + if (!empty($dataPattern)) { + $this->storeData["patternSpecific"][$patternKey]["data"] = $dataPattern; } - - } - - Data::replaceStore($storeData); - + + $this->processedPatterns[] = $patternKey; + } - - } - + }