Skip to content
This repository was archived by the owner on Feb 7, 2023. It is now read-only.

Commit b4e12dd

Browse files
committed
data json and yaml files can now be "chunked"
1 parent e315ecf commit b4e12dd

File tree

1 file changed

+56
-25
lines changed

1 file changed

+56
-25
lines changed

core/lib/PatternLab/Data.php

Lines changed: 56 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public static function copy() {
3030
}
3131

3232
/**
33-
* Gather data from source/_data/_data.json, source/_data/_listitems.json, and pattern-specific json files
33+
* Gather data from any JSON and YAML files in source/_data
3434
*
3535
* Reserved attributes:
3636
* - Data::$store["listItems"] : listItems from listitems.json, duplicated into separate arrays for Data::$store["listItems"]["one"], Data::$store["listItems"]["two"]... etc.
@@ -49,30 +49,61 @@ public static function gather($options = array()) {
4949
$listItemsJSON = array();
5050
$listItemsYAML = array();
5151

52-
// gather the data from the main source data.json
53-
if (file_exists(Config::$options["sourceDir"]."/_data/_data.json")) {
54-
$file = file_get_contents(Config::$options["sourceDir"]."/_data/_data.json");
55-
$dataJSON = json_decode($file,true);
56-
if ($jsonErrorMessage = JSON::hasError()) {
57-
JSON::lastErrorMsg("_data/_data.json",$jsonErrorMessage,$data);
58-
}
59-
$found = true;
60-
}
52+
// iterate over all of the other files in the source directory
53+
$directoryIterator = new \RecursiveDirectoryIterator(Config::$options["sourceDir"]."/_data/");
54+
$objects = new \RecursiveIteratorIterator($directoryIterator, \RecursiveIteratorIterator::SELF_FIRST);
6155

62-
// gather the data from the main source data.yaml
63-
if (file_exists(Config::$options["sourceDir"]."/_data/_data.yaml")) {
64-
$file = file_get_contents(Config::$options["sourceDir"]."/_data/_data.yaml");
65-
$dataYAML = Yaml::parse($file);
66-
$found = true;
67-
}
56+
// make sure dots are skipped
57+
$objects->setFlags(\FilesystemIterator::SKIP_DOTS);
6858

69-
if (!$found) {
70-
print "Missing a required file, source/_data/_data.json. Aborting.\n";
71-
exit;
59+
foreach ($objects as $name => $object) {
60+
61+
$ext = $object->getExtension();
62+
$data = array();
63+
$fileName = $object->getFilename();
64+
$hidden = ($fileName[0] == "_");
65+
$isFile = $object->isFile();
66+
$isListItems = strpos("listitems",$fileName);
67+
$pathName = $object->getPathname();
68+
$pathNameClean = str_replace(Config::$options["sourceDir"]."/","",$pathName);
69+
70+
if ($isFile && !$hidden && (($ext == "json") || ($ext == "yaml"))) {
71+
72+
if ($isListItems === false) {
73+
74+
if ($ext == "json") {
75+
76+
$file = file_get_contents($pathName);
77+
$data = json_decode($file,true);
78+
if ($jsonErrorMessage = JSON::hasError()) {
79+
JSON::lastErrorMsg($pathNameClean,$jsonErrorMessage,$data);
80+
}
81+
82+
} else if ($ext == "yaml") {
83+
84+
$file = file_get_contents($pathName);
85+
$data = Yaml::parse($file);
86+
87+
}
88+
89+
self::$store = array_replace_recursive(self::$store,$data);
90+
91+
} else if ($isListItems !== false) {
92+
93+
$data = ($ext == "json") ? self::getListItems("data/listitems.json") : self::getListItems("data/listitems.yaml","yaml");
94+
95+
if (!isset(self::$store["listItems"])) {
96+
self::$store["listItems"] = array();
97+
}
98+
99+
self::$store["listItems"] = array_replace_recursive(self::$store["listItems"],$data);
100+
101+
}
102+
103+
}
104+
72105
}
73106

74-
self::$store = array_replace_recursive($dataJSON,$dataYAML);
75-
76107
if (is_array(self::$store)) {
77108
foreach (self::$reservedKeys as $reservedKey) {
78109
if (array_key_exists($reservedKey,self::$store)) {
@@ -81,14 +112,14 @@ public static function gather($options = array()) {
81112
}
82113
}
83114

84-
$listItemsJSON = self::getListItems("data/_listitems.json");
85-
$listItemsYAML = self::getListItems("data/_listitems.yaml","yaml");
86-
87-
self::$store["listItems"] = array_replace_recursive($listItemsJSON,$listItemsYAML);
88115
self::$store["cacheBuster"] = Config::$options["cacheBuster"];
89116
self::$store["link"] = array();
90117
self::$store["patternSpecific"] = array();
91118

119+
unset(self::$store["listItems"]);
120+
print_r(self::$store);
121+
exit;
122+
92123
}
93124

94125
/**

0 commit comments

Comments
 (0)