Skip to content

Commit 095db68

Browse files
committed
Merge branch 'dev'
2 parents e9ed396 + 5a38cfb commit 095db68

File tree

3 files changed

+28
-17
lines changed

3 files changed

+28
-17
lines changed

Diff for: .editorconfig

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ root = true
22

33
[*]
44
charset = utf-8
5-
indent_style = tab
5+
indent_style = space
66
trim_trailing_whitespace = false
77
end_of_line = lf
88
insert_final_newline = true

Diff for: README.md

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
![license](https://img.shields.io/github/license/pattern-lab/plugin-php-data-inheritance.svg?maxAge=2592000)
2-
[![Packagist](https://img.shields.io/packagist/v/pattern-lab/plugin-data-inheritance.svg?maxAge=2592000)](https://packagist.org/packages/pattern-lab/plugin-data-inheritance) [![Gitter](https://img.shields.io/gitter/room/pattern-lab/php.svg?maxAge=2592000)](https://gitter.im/pattern-lab/php)
1+
![license](https://img.shields.io/github/license/pattern-lab/plugin-php-data-inheritance.svg)
2+
[![Packagist](https://img.shields.io/packagist/v/pattern-lab/plugin-data-inheritance.svg)](https://packagist.org/packages/pattern-lab/plugin-data-inheritance) [![Gitter](https://img.shields.io/gitter/room/pattern-lab/php.svg)](https://gitter.im/pattern-lab/php)
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

Diff for: src/PatternLab/DataInheritance/PatternLabListener.php

+24-10
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
<?php
22

33
/*!
4-
* Faker Listener Class
4+
* Data Inheritance Listener Class
55
*
66
* Copyright (c) 2016 Dave Olsen, http://dmolsen.com
77
* Licensed under the MIT license
88
*
9-
* Adds Faker support to Pattern Lab
9+
* Allows patterns to inherit data from patterns in their lineage
1010
*
1111
*/
1212

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)