forked from pattern-lab/patternlab-php-core
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUtil.php
More file actions
317 lines (273 loc) · 11.1 KB
/
Util.php
File metadata and controls
317 lines (273 loc) · 11.1 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
<?php
/*!
* Pattern Engine Util Class
*
* Copyright (c) 2014 Dave Olsen, http://dmolsen.com
* Licensed under the MIT license
*
* Shared functions that are meant to be used across all pattern engines
*
*/
namespace PatternLab\PatternEngine;
use \PatternLab\Timer;
class Util {
protected $patternPaths = array();
/**
* Set-up the pattern paths var
*/
public function __construct($options) {
$this->patternPaths = $options["patternPaths"];
}
/**
* Helper function to find and replace the given parameters in a particular partial before handing it back to Mustache
* @param {String} the file contents
* @param {Array} an array of paramters to match
*
* @return {String} the modified file contents
*/
public function findReplaceParameters($fileData, $parameters) {
$numbers = array("zero","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve");
foreach ($parameters as $k => $v) {
if (is_array($v)) {
if (preg_match('/{{\#([\s]*'.$k.'[\s]*)}}(.*?){{\/([\s]*'.$k.'[\s]*)}}/s',$fileData,$matches)) {
if (isset($matches[2])) {
$partialData = "";
foreach ($v as $v2) {
$partialData .= $this->findReplaceParameters($matches[2], $v2);
}
$fileData = preg_replace('/{{\#([\s]*'.$k.'[\s]*)}}(.*?){{\/([\s]*'.$k .'[\s]*)}}/s',$partialData,$fileData);
}
}
} else if ($v == "true") {
$fileData = preg_replace('/{{\#([\s]*'.$k.'[\s]*)}}(.*?){{\/([\s]*'.$k .'[\s]*)}}/s','$2',$fileData); // {{# asdf }}STUFF{{/ asdf}}
$fileData = preg_replace('/{{\^([\s]*'.$k.'[\s]*)}}(.*?){{\/([\s]*'.$k .'[\s]*)}}/s','',$fileData); // {{^ asdf }}STUFF{{/ asdf}}
} else if ($v == "false") {
$fileData = preg_replace('/{{\^([\s]*'.$k.'[\s]*)}}(.*?){{\/([\s]*'.$k .'[\s]*)}}/s','$2',$fileData); // {{# asdf }}STUFF{{/ asdf}}
$fileData = preg_replace('/{{\#([\s]*'.$k.'[\s]*)}}(.*?){{\/([\s]*'.$k .'[\s]*)}}/s','',$fileData); // {{^ asdf }}STUFF{{/ asdf}}
} else if ($k == "listItems") {
$v = ((int)$v != 0) && ((int)$v < 13) ? $numbers[$v] : $v;
if (($v != "zero") && in_array($v,$numbers)) {
$fileData = preg_replace('/{{\#([\s]*listItems\.[A-z]{3,10}[\s]*)}}/s','{{# listItems.'.$v.' }}',$fileData);
$fileData = preg_replace('/{{\/([\s]*listItems\.[A-z]{3,10}[\s]*)}}/s','{{/ listItems.'.$v.' }}',$fileData);
}
} else {
$fileData = preg_replace('/{{{([\s]*'.$k.'[\s]*)}}}/', $v, $fileData); // {{{ asdf }}}
$fileData = preg_replace('/{{([\s]*'.$k.'[\s]*)}}/', htmlspecialchars($v), $fileData); // escaped {{ asdf }}
}
}
return $fileData;
}
/**
* Helper function for getting a Mustache template file name.
* @param {String} the pattern type for the pattern
* @param {String} the pattern sub-type
*
* @return {Array} an array of rendered partials that match the given path
*/
public function getFileName($name,$ext) {
$fileName = "";
$dirSep = DIRECTORY_SEPARATOR;
// test to see what kind of path was supplied
$posDash = strpos($name,"-");
$posSlash = strpos($name,$dirSep);
if (($posSlash === false) && ($posDash !== false)) {
$fileName = $this->getPatternFileName($name);
} else {
$fileName = $name;
}
if (substr($fileName, 0 - strlen($ext)) !== $ext) {
$fileName .= $ext;
}
return $fileName;
}
/**
* Helper function to return the pattern file name
* @param {String} the name of the pattern
*
* @return {String} the file path to the pattern
*/
public function getPatternFileName($name) {
$patternFileName = "";
list($patternType,$pattern) = $this->getPatternInfo($name);
// see if the pattern is an exact match for patternPaths. if not iterate over patternPaths to find a likely match
if (isset($this->patternPaths[$patternType][$pattern])) {
$patternFileName = $this->patternPaths[$patternType][$pattern];
} else if (isset($this->patternPaths[$patternType])) {
foreach($this->patternPaths[$patternType] as $patternMatchKey=>$patternMatchValue) {
$pos = strpos($patternMatchKey,$pattern);
if ($pos !== false) {
$patternFileName = $patternMatchValue;
break;
}
}
}
return $patternFileName;
}
/**
* Helper function to return the parts of a partial name
* @param {String} the name of the partial
*
* @return {Array} the pattern type and the name of the pattern
*/
public function getPatternInfo($name) {
$patternBits = explode("-",$name);
$i = 1;
$k = 2;
$c = count($patternBits);
$patternType = $patternBits[0];
while (!isset($this->patternPaths[$patternType]) && ($i < $c)) {
$patternType .= "-".$patternBits[$i];
$i++;
$k++;
}
$patternBits = explode("-",$name,$k);
$pattern = $patternBits[count($patternBits)-1];
return array($patternType, $pattern);
}
/**
* Helper function for finding if a partial name has style modifier or parameters
* @param {String} the pattern name
*
* @return {Array} an array containing just the partial name, a style modifier, and any parameters
*/
public function getPartialInfo($partial) {
$styleModifier = array();
$parameters = array();
if (strpos($partial, "(") !== false) {
$partialBits = explode("(",$partial,2);
$partial = trim($partialBits[0]);
$parametersString = substr($partialBits[1],0,(strlen($partialBits[1]) - strlen(strrchr($partialBits[1],")"))));
$parameters = $this->parseParameters($parametersString);
}
if (strpos($partial, ":") !== false) {
$partialBits = explode(":",$partial,2);
$partial = $partialBits[0];
$styleModifier = $partialBits[1];
if (strpos($styleModifier, "|") !== false) {
$styleModifierBits = explode("|",$styleModifier);
$styleModifier = join(" ",$styleModifierBits);
}
$styleModifier = array("styleModifier" => $styleModifier);
}
return array($partial,$styleModifier,$parameters);
}
/**
* Helper function to parse the parameters and return them as an array
* @param {String} the parameter string
*
* @return {Array} the keys and values for the parameters
*/
private function parseParameters($string) {
$parameters = array();
$arrayParameters = array();
$arrayOptions = array();
$betweenSQuotes = false;
$betweenDQuotes = false;
$inKey = true;
$inValue = false;
$inArray = false;
$inOption = false;
$char = "";
$buffer = "";
$keyBuffer = "";
$arrayKeyBuffer = "";
$strLength = strlen($string);
for ($i = 0; $i < $strLength; $i++) {
$previousChar = $char;
$char = $string[$i];
if ($inKey && !$betweenDQuotes && !$betweenSQuotes && (($char == "\"") || ($char == "'"))) {
// if inKey, a quote, and betweenQuotes is false ignore quote, set betweenQuotes to true and empty buffer to kill spaces
($char == "\"") ? ($betweenDQuotes = true) : ($betweenSQuotes = true);
} else if ($inKey && (($betweenDQuotes && ($char == "\"")) || ($betweenSQuotes && ($char == "'"))) && ($previousChar == "\\")) {
// if inKey, a quote, betweenQuotes is true, and previous character is \ add to buffer
$buffer .= $char;
} else if ($inKey && (($betweenDQuotes && ($char == "\"")) || ($betweenSQuotes && ($char == "'")))) {
// if inKey, a quote, betweenQuotes is true set betweenQuotes to false, save as key buffer, empty buffer set inKey false
$keyBuffer = $buffer;
$buffer = "";
$inKey = false;
$betweenSQuotes = false;
$betweenDQuotes = false;
} else if ($inKey && !$betweenDQuotes && !$betweenSQuotes && ($char == ":")) {
// if inKey, a colon, betweenQuotes is false, save as key buffer, empty buffer, set inKey false set inValue true
$keyBuffer = $buffer;
$buffer = "";
$inKey = false;
$inValue = true;
} else if ($inKey) {
// if inKey add to buffer
$buffer .= $char;
} else if (!$inKey && !$inValue && ($char == ":")) {
// if inKey is false, inValue false, and a colon set inValue true
$inValue = true;
} else if ($inValue && !$inArray && !$betweenDQuotes && !$betweenSQuotes && ($char == "[")) {
// if inValue, outside quotes, and find a bracket set inArray to true and add to array buffer
$inArray = true;
$inValue = false;
$arrayKeyBuffer = trim($keyBuffer);
} else if ($inArray && !$betweenDQuotes && !$betweenSQuotes && ($char == "]")) {
// if inValue, outside quotes, and find a bracket set inArray to true and add to array buffer
$inArray = false;
$parameters[$arrayKeyBuffer] = $arrayParameters;
$arrayParameters = array();
} else if ($inArray && !$inOption && !$betweenDQuotes && !$betweenSQuotes && ($char == "{")) {
$inOption = true;
$inKey = true;
} else if ($inArray && $inOption && !$betweenDQuotes && !$betweenSQuotes && ($char == "}")) {
$inOption = false;
$inValue = false;
$inKey = false;
$arrayParameters[] = $arrayOptions;
$arrayOptions = array();
} else if ($inValue && !$betweenDQuotes && !$betweenSQuotes && (($char == "\"") || ($char == "'"))) {
// if inValue, a quote, and betweenQuote is false set betweenQuotes to true and empty buffer to kill spaces
($char == "\"") ? ($betweenDQuotes = true) : ($betweenSQuotes = true);
} else if ($inValue && (($betweenDQuotes && ($char == "\"")) || ($betweenSQuotes && ($char == "'"))) && ($previousChar == "\\")) {
// if inValue, a quote, betweenQuotes is true, and previous character is \ add to buffer
$buffer .= $char;
} else if ($inValue && (($betweenDQuotes && ($char == "\"")) || ($betweenSQuotes && ($char == "'")))) {
// if inValue, a quote, betweenQuotes is true set betweenQuotes to false, save to parameters array, empty buffer, set inValue false
$buffer = str_replace("\\\"","\"",$buffer);
$buffer = str_replace('\\\'','\'',$buffer);
if ($inArray) {
$arrayOptions[trim($keyBuffer)] = trim($buffer);
} else {
$parameters[trim($keyBuffer)] = trim($buffer);
}
$buffer = "";
$inValue = false;
$betweenSQuotes = false;
$betweenDQuotes = false;
} else if ($inValue && !$betweenDQuotes && !$betweenSQuotes && ($char == ",")) {
// if inValue, a comman, betweenQuotes is false, save to parameters array, empty buffer, set inValue false, set inKey true
if ($inArray) {
$arrayOptions[trim($keyBuffer)] = trim($buffer);
} else {
$parameters[trim($keyBuffer)] = trim($buffer);
}
$buffer = "";
$inValue = false;
$inKey = true;
} else if ($inValue && (($i + 1) == $strLength)) {
// if inValue and end of the string add to buffer, save to parameters array
$buffer .= $char;
if ($inArray) {
$arrayOptions[trim($keyBuffer)] = trim($buffer);
} else {
$parameters[trim($keyBuffer)] = trim($buffer);
}
} else if ($inValue) {
// if inValue add to buffer
$buffer .= $char;
} else if (!$inValue && !$inKey && ($char == ",")) {
// if inValue is false, inKey false, and a comma set inKey true
if ($inArray && !$inOption) {
// don't do anything
} else {
$inKey = true;
}
}
}
return $parameters;
}
}