-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathConfig.php
More file actions
527 lines (418 loc) · 18.9 KB
/
Config.php
File metadata and controls
527 lines (418 loc) · 18.9 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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
<?php
/*!
* Config Class
*
* Copyright (c) 2014 Dave Olsen, http://dmolsen.com
* Licensed under the MIT license
*
* Configures Pattern Lab by checking config files and required files
*
*/
namespace PatternLab;
use \PatternLab\Console;
use \PatternLab\FileUtil;
use \PatternLab\Timer;
use \Shudrum\Component\ArrayFinder\ArrayFinder;
use \Symfony\Component\Yaml\Yaml;
use \Symfony\Component\Yaml\Exception\ParseException;
class Config {
protected static $options = array();
protected static $userConfig = "config.yml";
protected static $userConfigDir = "";
protected static $userConfigDirClean = "config";
protected static $userConfigDirDash = "_config";
protected static $userConfigPath = "";
protected static $plConfigPath = "config/config.yml.default";
protected static $dirAdded = false;
/**
* Clean a given dir from the config file
* @param {String} directory to be cleaned
*
* @return {String} cleaned directory
*/
protected static function cleanDir($dir) {
if (isset($dir[0])) {
$dir = trim($dir);
$dir = ($dir[0] == DIRECTORY_SEPARATOR) ? ltrim($dir, DIRECTORY_SEPARATOR) : $dir;
$dir = ($dir[strlen($dir)-1] == DIRECTORY_SEPARATOR) ? rtrim($dir, DIRECTORY_SEPARATOR) : $dir;
}
return $dir;
}
/**
* Get the value associated with an option from the Config
* @param {String} the name of the option to be checked
*
* @return {String/Boolean} the value of the get or false if it wasn't found
*/
public static function getOption($key = "") {
if (!empty($key)) {
$arrayFinder = new ArrayFinder(self::$options);
return $arrayFinder->get($key);
}
return false;
}
/**
* Get the options set in the config
*
* @return {Array} the options from the config
*/
public static function getOptions() {
return self::$options;
}
/**
* Review the given styleguideKitPath to handle pre-2.1.0 backwards compatibility
* @param {String} styleguideKitPath from config.yml
*
* @return {String} the final, post-2.1.0-style styleguideKitPath
*/
protected static function getStyleguideKitPath($styleguideKitPath = "") {
$styleguideKitPathFinal = "";
if (isset($styleguideKitPath[0]) && ($styleguideKitPath[0] == DIRECTORY_SEPARATOR)) {
if (strpos($styleguideKitPath, DIRECTORY_SEPARATOR."vendor".DIRECTORY_SEPARATOR === 0)) {
$styleguideKitPathFinal = $styleguideKitPath; // mistaken set-up, pass to final for clean-up
} else if (strpos($styleguideKitPath, self::$options["baseDir"]) === 0) {
$styleguideKitPathFinal = str_replace(self::$options["baseDir"], "", $styleguideKitPath); // just need to peel off the base
} else if (strpos($styleguideKitPath, DIRECTORY_SEPARATOR."vendor") !== false) {
$parts = explode(DIRECTORY_SEPARATOR."vendor".DIRECTORY_SEPARATOR, $styleguideKitPath); // set on another machine's config.yml? try to be smart about it
$styleguideKitPathFinal = "vendor".DIRECTORY_SEPARATOR.$parts[1];
Console::writeInfo("Please double-check the styleguideKitPath option in <path>./config/config.yml</path>. It should be a path relative to the root of your Pattern Lab project...");
}
} else {
$styleguideKitPathFinal = $styleguideKitPath; // fingers crossed everything is fine
}
return $styleguideKitPathFinal;
}
/**
* Adds the config options to a var to be accessed from the rest of the system
* If it's an old config or no config exists this will update and generate it.
* @param {Boolean} whether we should print out the status of the config being loaded
*/
public static function init($baseDir = "", $verbose = true) {
// make sure a base dir was supplied
if (empty($baseDir)) {
Console::writeError("need a base directory to initialize the config class...");
}
// normalize the baseDir
$baseDir = FileUtil::normalizePath($baseDir);
// double-check the default config file exists
if (!is_dir($baseDir)) {
Console::writeError("make sure ".$baseDir." exists...");
}
// set the baseDir option
self::$options["baseDir"] = ($baseDir[strlen($baseDir)-1] == DIRECTORY_SEPARATOR) ? $baseDir : $baseDir.DIRECTORY_SEPARATOR;
// set-up the paths
self::$userConfigDirClean = self::$options["baseDir"].self::$userConfigDirClean;
self::$userConfigDirDash = self::$options["baseDir"].self::$userConfigDirDash;
self::$userConfigDir = (is_dir(self::$userConfigDirDash)) ? self::$userConfigDirDash : self::$userConfigDirClean;
self::$userConfigPath = self::$userConfigDir.DIRECTORY_SEPARATOR.self::$userConfig;
self::$plConfigPath = self::$options["baseDir"]."vendor/pattern-lab/core/".self::$plConfigPath;
// can't add __DIR__ above so adding here
if (!is_dir(self::$userConfigDir)) {
mkdir(self::$userConfigDir);
}
// check to see if the user config exists, if not create it
if ($verbose) {
Console::writeLine("configuring pattern lab...");
}
// make sure migrate doesn't happen by default
$migrate = false;
$diffVersion = false;
$defaultOptions = array();
$userOptions = array();
// double-check the default config file exists
if (!file_exists(self::$plConfigPath)) {
Console::writeError("the default options for Pattern Lab don't seem to exist at <path>".Console::getHumanReadablePath(self::$plConfigPath)."</path>. please check on the install location of pattern lab...");
}
// set the default config using the pattern lab config
try {
$defaultOptions = Yaml::parse(file_get_contents(self::$plConfigPath));
self::$options = array_merge(self::$options, $defaultOptions);
} catch (ParseException $e) {
Console::writeError("Config parse error in <path>".Console::getHumanReadablePath(self::$plConfigPath)."</path>: ".$e->getMessage());
}
// double-check the user's config exists. if not mark that we should migrate the default one
if (file_exists(self::$userConfigPath)) {
try {
$userOptions = Yaml::parse(file_get_contents(self::$userConfigPath));
self::$options = array_merge(self::$options, $userOptions);
} catch (ParseException $e) {
Console::writeError("Config parse error in <path>".Console::getHumanReadablePath(self::$userConfigPath)."</path>: ".$e->getMessage());
}
} else {
$migrate = true;
}
// compare version numbers
$diffVersion = (isset($userOptions["v"]) && ($userOptions["v"] == $defaultOptions["v"])) ? false : true;
// run an upgrade and migrations if necessary
if ($migrate || $diffVersion) {
if ($verbose) {
Console::writeInfo("upgrading your version of pattern lab...");
}
if ($migrate) {
if (!@copy(self::$plConfigPath, self::$userConfigPath)) {
Console::writeError("make sure that Pattern Lab can write a new config to ".self::$userConfigPath."...");
exit;
}
} else {
self::$options = self::writeNewConfigFile(self::$options, $defaultOptions);
}
}
// making sure the config isn't empty
if (empty(self::$options) && $verbose) {
Console::writeError("a set of configuration options is required to use Pattern Lab...");
exit;
}
// set-up the various dirs
self::$options["configDir"] = self::$userConfigDir;
self::$options["configPath"] = self::$userConfigPath;
self::$options["coreDir"] =
is_dir(self::$options["baseDir"] . "_core")
? self::$options["baseDir"] . "_core"
: self::$options["baseDir"] . "core";
self::$options["exportDir"] =
isset(self::$options["exportDir"])
? self::$options["baseDir"] . self::cleanDir(self::$options["exportDir"])
: self::$options["baseDir"] . "exports";
self::$options["publicDir"] =
isset(self::$options["publicDir"])
? self::$options["baseDir"] . self::cleanDir(self::$options["publicDir"])
: self::$options["baseDir"] . "public";
self::$options["scriptsDir"] =
isset(self::$options["scriptsDir"])
? self::$options["baseDir"] . self::cleanDir(self::$options["scriptsDir"])
: self::$options["baseDir"] . "scripts";
self::$options["sourceDir"] =
isset(self::$options["sourceDir"])
? self::$options["baseDir"] . self::cleanDir(self::$options["sourceDir"])
: self::$options["baseDir"] . "source";
self::$options["componentDir"] =
isset(self::$options["componentDir"])
? self::$options["publicDir"] . DIRECTORY_SEPARATOR . self::cleanDir(self::$options["componentDir"])
: self::$options["publicDir"] . DIRECTORY_SEPARATOR . "patternlab-components";
self::$options["dataDir"] =
isset(self::$options["dataDir"])
? self::$options["sourceDir"] . DIRECTORY_SEPARATOR . self::cleanDir(self::$options["dataDir"])
: self::$options["sourceDir"] . DIRECTORY_SEPARATOR . "_data";
self::$options["patternExportDir"] =
isset(self::$options["patternExportDir"])
? self::$options["exportDir"] . DIRECTORY_SEPARATOR . self::cleanDir(self::$options["patternExportDir"])
: self::$options["exportDir"] . DIRECTORY_SEPARATOR . "patterns";
self::$options["patternPublicDir"] =
isset(self::$options["patternPublicDir"])
? self::$options["publicDir"] . DIRECTORY_SEPARATOR . self::cleanDir(self::$options["patternPublicDir"])
: self::$options["publicDir"] . DIRECTORY_SEPARATOR . "patterns";
self::$options["patternSourceDir"] =
isset(self::$options["patternSourceDir"])
? self::$options["sourceDir"] . DIRECTORY_SEPARATOR . self::cleanDir(self::$options["patternSourceDir"])
: self::$options["sourceDir"] . DIRECTORY_SEPARATOR . "_patterns";
self::$options["metaDir"] =
isset(self::$options["metaDir"])
? self::$options["sourceDir"] . DIRECTORY_SEPARATOR . self::cleanDir(self::$options["metaDir"])
: self::$options["sourceDir"] . DIRECTORY_SEPARATOR . "_meta/";
self::$options["annotationsDir"] =
isset(self::$options["annotationsDir"])
? self::$options["sourceDir"] . DIRECTORY_SEPARATOR . self::cleanDir(self::$options["annotationsDir"])
: self::$options["sourceDir"] . DIRECTORY_SEPARATOR . "_annotations/";
// set-up outputFileSuffixes
self::$options["outputFileSuffixes"]["rendered"] =
isset(self::$options["outputFileSuffixes"]["rendered"])
? self::$options["outputFileSuffixes"]["rendered"]
: '';
self::$options["outputFileSuffixes"]["rawTemplate"] =
isset(self::$options["outputFileSuffixes"]["rawTemplate"])
? self::$options["outputFileSuffixes"]["rawTemplate"]
: '';
self::$options["outputFileSuffixes"]["markupOnly"] =
isset(self::$options["outputFileSuffixes"]["markupOnly"])
? self::$options["outputFileSuffixes"]["markupOnly"]
: '.markup-only';
// handle a pre-2.1.0 styleguideKitPath before saving it
if (isset(self::$options["styleguideKitPath"])) {
self::$options["styleguideKitPath"] = self::$options["baseDir"].self::cleanDir(self::getStyleguideKitPath(self::$options["styleguideKitPath"]));
}
// double-check a few directories are real and set-up
FileUtil::checkPathFromConfig(self::$options["sourceDir"], self::$userConfigPath, "sourceDir");
FileUtil::checkPathFromConfig(self::$options["publicDir"], self::$userConfigPath, "publicDir");
// make sure styleguideExcludes is set to an array even if it's empty
if (is_string(self::$options["styleGuideExcludes"])) {
self::$options["styleGuideExcludes"] = array();
}
// set the cacheBuster
self::$options["cacheBuster"] = (self::$options["cacheBusterOn"] == "false") ? 0 : time();
// provide the default for enable CSS. performance hog so it should be run infrequently
self::$options["enableCSS"] = false;
// which of these should be exposed in the front-end?
self::$options["exposedOptions"] = array();
self::setExposedOption("cacheBuster");
self::setExposedOption("defaultPattern");
self::setExposedOption("defaultShowPatternInfo");
self::setExposedOption("patternExtension");
self::setExposedOption("ishFontSize");
self::setExposedOption("ishMaximum");
self::setExposedOption("ishMinimum");
self::setExposedOption("ishViewportRange");
self::setExposedOption("outputFileSuffixes");
self::setExposedOption("patternStates");
self::setExposedOption("theme");
self::setExposedOption("plugins");
}
/**
* Check to see if the given array is an associative array
* @param {Array} the array to be checked
*
* @return {Boolean} whether it's an associative array
*/
protected static function isAssoc($array) {
return (bool) count(array_filter(array_keys($array), 'is_string'));
}
/**
* Add an option and associated value to the base Config
* @param {String} the name of the option to be added
* @param {String} the value of the option to be added
*
* @return {Boolean} whether the set was successful
*/
public static function setOption($optionName = "", $optionValue = "") {
if (empty($optionName) || empty($optionValue)) {
return false;
}
$arrayFinder = new ArrayFinder(self::$options);
$arrayFinder->set($optionName, $optionValue);
self::$options = $arrayFinder->get();
}
/**
* Add an option to the exposedOptions array so it can be exposed on the front-end
* @param {String} the name of the option to be added to the exposedOption arrays
*
* @return {Boolean} whether the set was successful
*/
public static function setExposedOption($optionName = "") {
if (!empty($optionName) && isset(self::$options[$optionName])) {
if (!in_array($optionName,self::$options["exposedOptions"])) {
self::$options["exposedOptions"][] = $optionName;
}
return true;
}
return false;
}
/**
* Update a single config option based on a change in composer.json
* @param {String} the name of the option to be changed
* @param {String} the new value of the option to be changed
* @param {Boolean} whether to force the update of the option
*/
public static function updateConfigOption($optionName,$optionValue, $force = false) {
if (is_string($optionValue) && strpos($optionValue,"<prompt>") !== false) {
// prompt for input using the supplied query
$options = "";
$default = "";
$prompt = str_replace("</prompt>","",str_replace("<prompt>","",$optionValue));
if (strpos($prompt, "<default>") !== false) {
$default = explode("<default>",$prompt);
$default = explode("</default>",$default[1]);
$default = $default[0];
}
$input = Console::promptInput($prompt,$options,$default,false);
self::writeUpdateConfigOption($optionName,$input);
Console::writeTag("ok","config option ".$optionName." updated...", false, true);
} else if (!isset(self::$options[$optionName]) || (self::$options["overrideConfig"] == "a") || $force) {
// if the option isn't set or the config is always to override update the config
self::writeUpdateConfigOption($optionName,$optionValue);
} else if (self::$options["overrideConfig"] == "q") {
// standardize the values for comparison
$currentOption = self::getOption($optionName);
$currentOptionValue = $currentOption;
$newOptionValue = $optionValue;
$optionNameOutput = $optionName;
// dive into plugins to do a proper comparison to
if ($optionName == "plugins") {
// replace the data in anticipation of it being used
$optionValue = array_replace_recursive($currentOption, $newOptionValue);
// get the key of the plugin that is being added/updated
reset($newOptionValue);
$newOptionKey = key($newOptionValue);
if (!array_key_exists($newOptionKey, $currentOptionValue)) {
// if the key doesn't exist just write out the new config and move on
self::writeUpdateConfigOption($optionName,$optionValue);
return;
} else {
// see if the existing configs for the plugin exists. if so just return with no changes
if ($newOptionValue[$newOptionKey] == $currentOptionValue[$newOptionKey]) {
return;
} else {
$optionNameOutput = $optionName.".".$newOptionKey;
}
}
}
if ($currentOptionValue != $newOptionValue) {
// prompt for input
if (is_array($currentOptionValue)) {
$prompt = "update the config option <desc>".$optionNameOutput."</desc> with the value from the package install?";
} else {
$prompt = "update the config option <desc>".$optionNameOutput." (".$currentOptionValue.")</desc> with the value <desc>".$newOptionValue."</desc>?";
}$options = "Y/n";
$input = Console::promptInput($prompt,$options,"Y");
if ($input == "y") {
// update the config option
self::writeUpdateConfigOption($optionName,$optionValue);
Console::writeInfo("config option ".$optionNameOutput." updated...", false, true);
} else {
Console::writeWarning("config option <desc>".$optionNameOutput."</desc> not updated...", false, true);
}
}
}
}
/**
* Add an option and associated value to the base Config. BC wrap for setOption
* @param {String} the name of the option to be added
* @param {String} the value of the option to be added
*
* @return {Boolean} whether the set was successful
*/
public static function updateOption($optionName = "", $optionValue = "") {
return self::setOption($optionName, $optionValue);
}
/**
* Write out the new config option value
* @param {String} the name of the option to be changed
* @param {String} the new value of the option to be changed
*/
protected static function writeUpdateConfigOption($optionName,$optionValue) {
// parse the YAML options
try {
$options = Yaml::parse(file_get_contents(self::$userConfigPath));
} catch (ParseException $e) {
Console::writeError("Config parse error in <path>".self::$userConfigPath."</path>: ".$e->getMessage());
}
// set this option for the current running of the app
self::setOption($optionName, $optionValue);
// modify the yaml file results
$arrayFinder = new ArrayFinder($options);
$arrayFinder->set($optionName, $optionValue);
$options = $arrayFinder->get();
// dump the YAML
$configOutput = Yaml::dump($options, 3);
// write out the new config file
file_put_contents(self::$userConfigPath,$configOutput);
}
/**
* Use the default config as a base and update it with old config options. Write out a new user config.
* @param {Array} the old configuration file options
* @param {Array} the default configuration file options
*
* @return {Array} the new configuration
*/
protected static function writeNewConfigFile($oldOptions,$defaultOptions) {
// iterate over the old config and replace values in the new config
foreach ($oldOptions as $key => $value) {
if ($key != "v") {
$defaultOptions[$key] = $value;
}
}
// dump the YAML
$configOutput = Yaml::dump($defaultOptions, 3);
// write out the new config file
file_put_contents(self::$userConfigPath,$configOutput);
return $defaultOptions;
}
}