Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
dmolsen committed Jun 30, 2016
2 parents 6734b88 + 7070063 commit b949887
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
5 changes: 1 addition & 4 deletions README.md
Expand Up @@ -3,10 +3,7 @@

# Data Inheritance Plugin for Pattern Lab

The Data Inheritance Plugin forces patterns to inherit data from their lineage. This allows data in included patterns to bubble
to the top of the pattern stack. With this plugin pseudo-patterns become first-class citizens.

This is a crap intro. Rewrite it.
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.

## Installation

Expand Down
30 changes: 22 additions & 8 deletions src/PatternLab/DataInheritance/PatternLabListener.php
Expand Up @@ -13,6 +13,7 @@
namespace PatternLab\DataInheritance;

use \PatternLab\Config;
use \PatternLab\Data;
use \PatternLab\PatternData;

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

/**
* Fake some content. Replace the entire store.
* Look up data in lineages, update pattern store data, replace store
*/
public function inherit() {

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

$store = PatternData::get();
$storeData = Data::get();
$storePatternData = PatternData::get();

foreach ($store as $patternStoreKey => $patternData) {
foreach ($storePatternData as $patternStoreKey => $patternData) {

if (count($patternData["lineages"]) > 0) {
if (isset($patternData["lineages"]) && (count($patternData["lineages"]) > 0)) {

$data = PatternData::getPatternOption($patternStoreKey, "data");
$dataLineage = array();

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

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

}

PatternData::setPatternOption($patternStoreKey, "data", $data);
// 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;
}

}

}

Data::replaceStore($storeData);

}

}
Expand Down

0 comments on commit b949887

Please sign in to comment.