Skip to content

Commit 111d3b1

Browse files
committed
redoing the order of inheritance. current pattern takes precedence
1 parent 336437b commit 111d3b1

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

src/PatternLab/DataInheritance/PatternLabListener.php

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
namespace PatternLab\DataInheritance;
1414

1515
use \PatternLab\Config;
16+
use \PatternLab\Data;
1617
use \PatternLab\PatternData;
1718

1819
class PatternLabListener extends \PatternLab\Listener {
@@ -28,33 +29,46 @@ public function __construct() {
2829
}
2930

3031
/**
31-
* Fake some content. Replace the entire store.
32+
* Look up data in lineages, update pattern store data, replace store
3233
*/
3334
public function inherit() {
3435

3536
if ((bool)Config::getOption("plugins.dataInheritance.enabled")) {
3637

37-
$store = PatternData::get();
38+
$storeData = Data::get();
39+
$storePatternData = PatternData::get();
3840

39-
foreach ($store as $patternStoreKey => $patternData) {
41+
foreach ($storePatternData as $patternStoreKey => $patternData) {
4042

41-
if (count($patternData["lineages"]) > 0) {
43+
if (isset($patternData["lineages"]) && (count($patternData["lineages"]) > 0)) {
4244

43-
$data = PatternData::getPatternOption($patternStoreKey, "data");
45+
$dataLineage = array();
4446

4547
foreach($patternData["lineages"] as $lineage) {
4648

47-
$lineageData = PatternData::getPatternOption($lineage["lineagePattern"], "data");
48-
$data = array_replace_recursive($data, $lineageData);
49+
// merge the lineage data with the lineage store. newer/higher-level data is more important.
50+
$lineageKey = $lineage["lineagePattern"];
51+
$lineageData = isset($storeData["patternSpecific"][$lineageKey]) && isset($storeData["patternSpecific"][$lineageKey]["data"]) ? $storeData["patternSpecific"][$lineageKey]["data"] : array();
52+
if (!empty($lineageData)) {
53+
$dataLineage = array_replace_recursive($dataLineage, $lineageData);
54+
}
4955

5056
}
5157

52-
PatternData::setPatternOption($patternStoreKey, "data", $data);
58+
// merge the lineage data with the pattern data. pattern data is more important.
59+
$dataPattern = isset($storeData["patternSpecific"][$patternStoreKey]) && isset($storeData["patternSpecific"][$patternStoreKey]["data"]) ? $storeData["patternSpecific"][$patternStoreKey]["data"] : array();
60+
$dataPattern = array_replace_recursive($dataLineage, $dataPattern);
61+
62+
if (!empty($dataPattern)) {
63+
$storeData["patternSpecific"][$patternStoreKey]["data"] = $dataPattern;
64+
}
5365

5466
}
5567

5668
}
5769

70+
Data::replaceStore($storeData);
71+
5872
}
5973

6074
}

0 commit comments

Comments
 (0)