-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathPseudoPatternRule.php
189 lines (156 loc) · 7.75 KB
/
PseudoPatternRule.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
/*!
* Pattern Data Pseudo-Pattern Rule Class
*
* Copyright (c) 2014 Dave Olsen, http://dmolsen.com
* Licensed under the MIT license
*
* If it's a JSON or YAML file with a ~ add the pseudo-pattern info to the PatternData::$store
*
*/
namespace PatternLab\PatternData\Rules;
use \PatternLab\Config;
use \PatternLab\PatternData;
use \PatternLab\JSON;
use \PatternLab\Timer;
use \Symfony\Component\Yaml\Exception\ParseException;
use \Symfony\Component\Yaml\Yaml;
class PseudoPatternRule extends \PatternLab\PatternData\Rule {
public function __construct($options) {
parent::__construct($options);
$this->depthProp = 3; // 3 means that depth won't be checked
$this->extProp = "json||yaml||yml";
$this->isDirProp = false;
$this->isFileProp = true;
$this->searchProp = "~";
$this->ignoreProp = "";
}
public function run($depth, $ext, $path, $pathName, $name) {
// load default vars
$patternSubtype = PatternData::getPatternSubtype();
$patternSubtypeClean = PatternData::getPatternSubtypeClean();
$patternSubtypeDash = PatternData::getPatternSubtypeDash();
$patternType = PatternData::getPatternType();
$patternTypeClean = PatternData::getPatternTypeClean();
$patternTypeDash = PatternData::getPatternTypeDash();
$dirSep = PatternData::getDirSep();
$frontMeta = PatternData::getFrontMeta();
// should this pattern get rendered?
$hidden = ($name[0] == "_");
$noviewall = ($name[0] == "-");
// set-up the names
$patternFull = in_array($name[0],$frontMeta) ? substr($name,1) : $name; // 00-colors~foo.mustache
$patternState = "";
// check for pattern state
if (strpos($patternFull,"@") !== false) {
$patternBits = explode("@",$patternFull,2);
$patternState = str_replace(".".$ext,"",$patternBits[1]);
$patternFull = preg_replace("/@(.*?)\./",".",$patternFull);
}
// finish setting up vars
$patternBits = explode("~",$patternFull);
$patternBase = $patternBits[0].".".Config::getOption("patternExtension"); // 00-homepage.mustache
$patternBaseDash = $this->getPatternName($patternBits[0],false); // homepage
$patternBaseOrig = $patternTypeDash."-".$patternBaseDash; // pages-homepage
$patternBaseData = $patternBits[0].".".$ext; // 00-homepage.json
$stripJSON = str_replace(".".$ext,"",$patternBits[1]);
$patternBitClean = preg_replace("/@(.*?)/","",$patternBits[0]);
$pattern = $patternBitClean."-".$stripJSON; // 00-homepage-00-emergency
$patternInt = $patternBitClean."-".$this->getPatternName($stripJSON, false); // 00-homepage-emergency
$patternDash = $this->getPatternName($patternInt,false); // homepage-emergency
$patternClean = str_replace("-"," ",$patternDash); // homepage emergency
$patternPartial = $patternTypeDash."-".$patternDash; // pages-homepage-emergency
$patternPath = str_replace(".".$ext,"",str_replace("~","-",$pathName)); // 00-atoms/01-global/00-colors
$patternPathDash = str_replace($dirSep,"-",$patternPath); // 00-atoms-01-global-00-colors (file path)
// check the original pattern path. if it doesn't exist make a guess
$patternPathOrig = PatternData::getPatternOption($patternBaseOrig,"pathName"); // 04-pages/00-homepage
$patternPathOrigDash = PatternData::getPatternOption($patternBaseOrig,"pathDash"); // 04-pages-00-homepage
if (!$patternPathOrig) {
if (($patternBase . $ext) != $patternFull) {
$patternPathOrig = $path . DIRECTORY_SEPARATOR . $patternBaseDash; // 04-pages/homepage
$patternPathOrigDash = str_replace($dirSep,"-",$patternPathOrig); // 04-pages-homepage
if ( ! file_exists(Config::getOption("patternSourceDir").DIRECTORY_SEPARATOR.$patternPathOrig.'.'.Config::getOption("patternExtension"))) {
$patternPathOrig = $path . DIRECTORY_SEPARATOR . '_' . $patternBaseDash; // 04-pages/_homepage
}
} else {
$patternPathOrigBits = explode("~",$pathName);
$patternPathOrig = $patternPathOrigBits[0]; // 04-pages/00-homepage
$patternPathOrigDash = str_replace($dirSep,"-",$patternPathOrig); // 04-pages-00-homepage
}
}
// create a key for the data store
$patternStoreKey = $patternPartial;
// collect the data
$patternStoreData = array("category" => "pattern",
"name" => $pattern,
"partial" => $patternPartial,
"nameDash" => $patternDash,
"nameClean" => $patternClean,
"type" => $patternType,
"typeDash" => $patternTypeDash,
"breadcrumb" => array("patternType" => $patternTypeClean),
"state" => $patternState,
"hidden" => $hidden,
"noviewall" => $noviewall,
"depth" => $depth,
"ext" => $ext,
"path" => $path,
"pathName" => $patternPath,
"pathDash" => $patternPathDash,
"isDir" => $this->isDirProp,
"isFile" => $this->isFileProp,
"pseudo" => true,
"original" => $patternBaseOrig,
"pathOrig" => $patternPathOrig,
"pathOrigDash" => $patternPathOrigDash);
// add any subtype info if necessary
if ($depth > 1) {
$patternStoreData["subtype"] = $patternSubtype;
$patternStoreData["subtypeDash"] = $patternSubtypeDash;
$patternStoreData["breadcrumb"] = array("patternType" => $patternTypeClean, "patternSubtype" => $patternSubtypeClean);
}
$patternDataBase = array();
if (file_exists(Config::getOption("patternSourceDir").DIRECTORY_SEPARATOR.$path.DIRECTORY_SEPARATOR.$patternBaseData)) {
$data = file_get_contents(Config::getOption("patternSourceDir").DIRECTORY_SEPARATOR.$path.DIRECTORY_SEPARATOR.$patternBaseData);
if ($ext == "json") {
$patternDataBase = json_decode($data,true);
if ($jsonErrorMessage = JSON::hasError()) {
JSON::lastErrorMsg($patternBaseJSON,$jsonErrorMessage,$data);
}
} else {
try {
$patternDataBase = YAML::parse($data);
} catch (ParseException $e) {
printf("unable to parse ".$pathNameClean.": %s..\n", $e->getMessage());
}
// single line of text won't throw a YAML error. returns as string
if (gettype($patternDataBase) == "string") {
$patternDataBase = array();
}
}
}
// get the data for the pseudo-pattern
$data = file_get_contents(Config::getOption("patternSourceDir").DIRECTORY_SEPARATOR.$pathName);
if ($ext == "json") {
$patternData = json_decode($data,true);
if ($jsonErrorMessage = JSON::hasError()) {
JSON::lastErrorMsg($name,$jsonErrorMessage,$data);
}
} else {
try {
$patternData = YAML::parse($data);
} catch (ParseException $e) {
printf("unable to parse ".$pathNameClean.": %s..\n", $e->getMessage());
}
// single line of text won't throw a YAML error. returns as string
if (gettype($patternData) == "string") {
$patternData = array();
}
}
// make sure the pattern data is an array before merging the data
$patternStoreData["data"] = is_array($patternData) ? array_replace_recursive($patternDataBase, $patternData) : $patternDataBase;
// if the pattern data store already exists make sure it is merged and overwrites this data
$patternStoreData = (PatternData::checkOption($patternStoreKey)) ? array_replace_recursive(PatternData::getOption($patternStoreKey),$patternStoreData) : $patternStoreData;
PatternData::setOption($patternStoreKey, $patternStoreData);
}
}