15
15
use \PatternLab \Timer ;
16
16
17
17
class Util {
18
-
18
+
19
19
protected $ patternPaths = array ();
20
-
20
+
21
21
/**
22
22
* Set-up the pattern paths var
23
23
*/
24
24
public function __construct ($ options ) {
25
-
25
+
26
26
$ this ->patternPaths = $ options ["patternPaths " ];
27
-
27
+
28
28
}
29
-
29
+
30
30
/**
31
31
* Helper function to find and replace the given parameters in a particular partial before handing it back to Mustache
32
32
* @param {String} the file contents
@@ -66,7 +66,7 @@ public function findReplaceParameters($fileData, $parameters) {
66
66
}
67
67
return $ fileData ;
68
68
}
69
-
69
+
70
70
/**
71
71
* Helper function for getting a Mustache template file name.
72
72
* @param {String} the pattern type for the pattern
@@ -75,40 +75,40 @@ public function findReplaceParameters($fileData, $parameters) {
75
75
* @return {Array} an array of rendered partials that match the given path
76
76
*/
77
77
public function getFileName ($ name ,$ ext ) {
78
-
78
+
79
79
$ fileName = "" ;
80
80
$ dirSep = DIRECTORY_SEPARATOR ;
81
-
81
+
82
82
// test to see what kind of path was supplied
83
83
$ posDash = strpos ($ name ,"- " );
84
84
$ posSlash = strpos ($ name ,$ dirSep );
85
-
85
+
86
86
if (($ posSlash === false ) && ($ posDash !== false )) {
87
87
$ fileName = $ this ->getPatternFileName ($ name );
88
88
} else {
89
89
$ fileName = $ name ;
90
90
}
91
-
91
+
92
92
if (substr ($ fileName , 0 - strlen ($ ext )) !== $ ext ) {
93
93
$ fileName .= $ ext ;
94
94
}
95
-
95
+
96
96
return $ fileName ;
97
-
97
+
98
98
}
99
-
99
+
100
100
/**
101
101
* Helper function to return the pattern file name
102
102
* @param {String} the name of the pattern
103
103
*
104
104
* @return {String} the file path to the pattern
105
105
*/
106
106
public function getPatternFileName ($ name ) {
107
-
107
+
108
108
$ patternFileName = "" ;
109
-
109
+
110
110
list ($ patternType ,$ pattern ) = $ this ->getPatternInfo ($ name );
111
-
111
+
112
112
// see if the pattern is an exact match for patternPaths. if not iterate over patternPaths to find a likely match
113
113
if (isset ($ this ->patternPaths [$ patternType ][$ pattern ])) {
114
114
$ patternFileName = $ this ->patternPaths [$ patternType ][$ pattern ];
@@ -121,21 +121,21 @@ public function getPatternFileName($name) {
121
121
}
122
122
}
123
123
}
124
-
124
+
125
125
return $ patternFileName ;
126
-
126
+
127
127
}
128
-
128
+
129
129
/**
130
130
* Helper function to return the parts of a partial name
131
131
* @param {String} the name of the partial
132
132
*
133
133
* @return {Array} the pattern type and the name of the pattern
134
134
*/
135
135
public function getPatternInfo ($ name ) {
136
-
136
+
137
137
$ patternBits = explode ("- " ,$ name );
138
-
138
+
139
139
$ i = 1 ;
140
140
$ k = 2 ;
141
141
$ c = count ($ patternBits );
@@ -145,32 +145,32 @@ public function getPatternInfo($name) {
145
145
$ i ++;
146
146
$ k ++;
147
147
}
148
-
148
+
149
149
$ patternBits = explode ("- " ,$ name ,$ k );
150
150
$ pattern = $ patternBits [count ($ patternBits )-1 ];
151
-
151
+
152
152
return array ($ patternType , $ pattern );
153
-
153
+
154
154
}
155
-
155
+
156
156
/**
157
157
* Helper function for finding if a partial name has style modifier or parameters
158
158
* @param {String} the pattern name
159
159
*
160
160
* @return {Array} an array containing just the partial name, a style modifier, and any parameters
161
161
*/
162
162
public function getPartialInfo ($ partial ) {
163
-
163
+
164
164
$ styleModifier = array ();
165
165
$ parameters = array ();
166
-
166
+
167
167
if (strpos ($ partial , "( " ) !== false ) {
168
168
$ partialBits = explode ("( " ,$ partial ,2 );
169
169
$ partial = trim ($ partialBits [0 ]);
170
170
$ parametersString = substr ($ partialBits [1 ],0 ,(strlen ($ partialBits [1 ]) - strlen (strrchr ($ partialBits [1 ],") " ))));
171
171
$ parameters = $ this ->parseParameters ($ parametersString );
172
172
}
173
-
173
+
174
174
if (strpos ($ partial , ": " ) !== false ) {
175
175
$ partialBits = explode (": " ,$ partial ,2 );
176
176
$ partial = $ partialBits [0 ];
@@ -181,19 +181,19 @@ public function getPartialInfo($partial) {
181
181
}
182
182
$ styleModifier = array ("styleModifier " => $ styleModifier );
183
183
}
184
-
184
+
185
185
return array ($ partial ,$ styleModifier ,$ parameters );
186
-
186
+
187
187
}
188
-
188
+
189
189
/**
190
190
* Helper function to parse the parameters and return them as an array
191
191
* @param {String} the parameter string
192
192
*
193
193
* @return {Array} the keys and values for the parameters
194
194
*/
195
195
private function parseParameters ($ string ) {
196
-
196
+
197
197
$ parameters = array ();
198
198
$ arrayParameters = array ();
199
199
$ arrayOptions = array ();
@@ -208,12 +208,12 @@ private function parseParameters($string) {
208
208
$ keyBuffer = "" ;
209
209
$ arrayKeyBuffer = "" ;
210
210
$ strLength = strlen ($ string );
211
-
211
+
212
212
for ($ i = 0 ; $ i < $ strLength ; $ i ++) {
213
-
213
+
214
214
$ previousChar = $ char ;
215
215
$ char = $ string [$ i ];
216
-
216
+
217
217
if ($ inKey && !$ betweenDQuotes && !$ betweenSQuotes && (($ char == "\"" ) || ($ char == "' " ))) {
218
218
// if inKey, a quote, and betweenQuotes is false ignore quote, set betweenQuotes to true and empty buffer to kill spaces
219
219
($ char == "\"" ) ? ($ betweenDQuotes = true ) : ($ betweenSQuotes = true );
@@ -300,18 +300,18 @@ private function parseParameters($string) {
300
300
$ buffer .= $ char ;
301
301
} else if (!$ inValue && !$ inKey && ($ char == ", " )) {
302
302
// if inValue is false, inKey false, and a comma set inKey true
303
- if ($ inArray && !$ inOption ) {
303
+ if ($ inArray && !$ inOption ) {
304
304
// don't do anything
305
305
} else {
306
306
$ inKey = true ;
307
307
}
308
-
308
+
309
309
}
310
310
}
311
-
311
+
312
312
return $ parameters ;
313
-
313
+
314
314
}
315
-
315
+
316
316
317
317
}
0 commit comments