Skip to content
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

Add support for multilevel data inheritance #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
116 changes: 65 additions & 51 deletions src/PatternLab/DataInheritance/PatternLabListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

}

}


}