Skip to content

Commit b949887

Browse files
committed
2 parents 6734b88 + 7070063 commit b949887

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

README.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33

44
# Data Inheritance Plugin for Pattern Lab
55

6-
The Data Inheritance Plugin forces patterns to inherit data from their lineage. This allows data in included patterns to bubble
7-
to the top of the pattern stack. With this plugin pseudo-patterns become first-class citizens.
8-
9-
This is a crap intro. Rewrite it.
6+
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.
107

118
## Installation
129

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)