Skip to content

Commit a29ce4a

Browse files
committed
adding support for ArrayFinder, renaming updateOption to setOption, reorganizing initial set-up
1 parent 509baa9 commit a29ce4a

File tree

1 file changed

+36
-80
lines changed

1 file changed

+36
-80
lines changed

src/PatternLab/Config.php

Lines changed: 36 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use \PatternLab\Console;
1616
use \PatternLab\FileUtil;
1717
use \PatternLab\Timer;
18+
use \Shudrum\Component\ArrayFinder\ArrayFinder;
1819
use \Symfony\Component\Yaml\Yaml;
1920
use \Symfony\Component\Yaml\Exception\ParseException;
2021

@@ -53,14 +54,11 @@ protected static function cleanDir($dir) {
5354
*
5455
* @return {String/Boolean} the value of the get or false if it wasn't found
5556
*/
56-
public static function getOption($optionName = "") {
57+
public static function getOption($key = "") {
5758

58-
if (empty($optionName)) {
59-
return false;
60-
}
61-
62-
if (array_key_exists($optionName,self::$options)) {
63-
return self::$options[$optionName];
59+
if (!empty($key)) {
60+
$arrayFinder = new ArrayFinder(self::$options);
61+
return $arrayFinder->get($key);
6462
}
6563

6664
return false;
@@ -138,46 +136,44 @@ public static function init($baseDir = "", $verbose = true) {
138136
mkdir(self::$userConfigDir);
139137
}
140138

139+
// check to see if the user config exists, if not create it
140+
if ($verbose) {
141+
Console::writeLine("configuring pattern lab...");
142+
}
143+
141144
// make sure migrate doesn't happen by default
142-
$migrate = false;
143-
$diffVersion = false;
145+
$migrate = false;
146+
$diffVersion = false;
147+
$defaultOptions = array();
148+
$userOptions = array();
144149

145150
// double-check the default config file exists
146151
if (!file_exists(self::$plConfigPath)) {
147-
Console::writeError("make sure <path>".self::$plConfigPath."</path> exists before trying to have Pattern Lab build the config.yml file automagically...");
152+
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...");
148153
}
149154

150155
// set the default config using the pattern lab config
151156
try {
152-
$data = Yaml::parse(file_get_contents(self::$plConfigPath));
157+
$defaultOptions = Yaml::parse(file_get_contents(self::$plConfigPath));
158+
$defaultOptions = array_merge(self::$options, $defaultOptions);
153159
} catch (ParseException $e) {
154-
Console::writeError("Config parse error in <path>".self::$plConfigPath."</path>: ".$e->getMessage());
155-
}
156-
157-
// load the options from the default file
158-
self::loadOptions($data);
159-
160-
// make sure these are copied
161-
$defaultOptions = self::$options;
162-
163-
// check to see if the user config exists, if not create it
164-
if ($verbose) {
165-
Console::writeLine("configuring pattern lab...");
160+
Console::writeError("Config parse error in <path>".Console::getHumanReadablePath(self::$plConfigPath)."</path>: ".$e->getMessage());
166161
}
167162

168-
if (!file_exists(self::$userConfigPath)) {
169-
$migrate = true;
170-
} else {
163+
// double-check the user's config exists. if not mark that we should migrate the default one
164+
if (file_exists(self::$userConfigPath)) {
171165
try {
172-
$data = Yaml::parse(file_get_contents(self::$userConfigPath));
166+
$userOptions = Yaml::parse(file_get_contents(self::$userConfigPath));
167+
self::$options = array_merge(self::$options, $userOptions);
173168
} catch (ParseException $e) {
174-
Console::writeError("Config parse error in <path>".self::$userConfigPath."</path>: ".$e->getMessage());
169+
Console::writeError("Config parse error in <path>".Console::getHumanReadablePath(self::$userConfigPath)."</path>: ".$e->getMessage());
175170
}
176-
self::loadOptions($data);
171+
} else {
172+
$migrate = true;
177173
}
178174

179175
// compare version numbers
180-
$diffVersion = (self::$options["v"] != $defaultOptions["v"]) ? true : false;
176+
$diffVersion = (isset($userOptions["v"]) && ($userOptions["v"] == $defaultOptions["v"])) ? false : true;
181177

182178
// run an upgrade and migrations if necessary
183179
if ($migrate || $diffVersion) {
@@ -190,7 +186,7 @@ public static function init($baseDir = "", $verbose = true) {
190186
exit;
191187
}
192188
} else {
193-
self::$options = self::writeNewConfigFile(self::$options,$defaultOptions);
189+
self::$options = self::writeNewConfigFile(self::$options, $defaultOptions);
194190
}
195191
}
196192

@@ -256,29 +252,6 @@ protected static function isAssoc($array) {
256252
return (bool) count(array_filter(array_keys($array), 'is_string'));
257253
}
258254

259-
/**
260-
* Load the options into self::$options
261-
* @param {Array} the data to be added
262-
* @param {String} any addition that may need to be added to the option key
263-
*/
264-
public static function loadOptions($data,$parentKey = "") {
265-
266-
foreach ($data as $key => $value) {
267-
268-
$key = $parentKey.trim($key);
269-
270-
if (is_array($value) && self::isAssoc($value)) {
271-
self::loadOptions($value,$key.".");
272-
} else if (is_array($value) && !self::isAssoc($value)) {
273-
self::$options[$key] = $value;
274-
} else {
275-
self::$options[$key] = trim($value);
276-
}
277-
278-
}
279-
280-
}
281-
282255
/**
283256
* Add an option and associated value to the base Config
284257
* @param {String} the name of the option to be added
@@ -292,12 +265,9 @@ public static function setOption($optionName = "", $optionValue = "") {
292265
return false;
293266
}
294267

295-
if (!array_key_exists($optionName,self::$options)) {
296-
self::$options[$optionName] = $optionValue;
297-
return true;
298-
}
299-
300-
return false;
268+
$arrayFinder = new ArrayFinder(self::$options);
269+
$arrayFinder->set($key, $value);
270+
self::$options = $arrayFinder->get();
301271

302272
}
303273

@@ -377,29 +347,15 @@ public static function updateConfigOption($optionName,$optionValue, $force = fal
377347
}
378348

379349
/**
380-
* Update an option and associated value to the base Config
381-
* @param {String} the name of the option to be updated
382-
* @param {String} the value of the option to be updated
350+
* Add an option and associated value to the base Config. BC wrap for setOption
351+
* @param {String} the name of the option to be added
352+
* @param {String} the value of the option to be added
383353
*
384-
* @return {Boolean} whether the update was successful
354+
* @return {Boolean} whether the set was successful
385355
*/
386356
public static function updateOption($optionName = "", $optionValue = "") {
387357

388-
if (empty($optionName) || empty($optionValue)) {
389-
return false;
390-
}
391-
392-
if (array_key_exists($optionName,self::$options)) {
393-
if (is_array(self::$options[$optionName])) {
394-
$optionValue = is_array($optionValue) ? $optionValue : array($optionValue);
395-
self::$options[$optionName] = array_merge(self::$options[$optionName], $optionValue);
396-
} else {
397-
self::$options[$optionName] = $optionValue;
398-
}
399-
return true;
400-
}
401-
402-
return false;
358+
return self::setOption($optionName, $optionValue);
403359

404360
}
405361

@@ -425,7 +381,7 @@ protected static function writeUpdateConfigOption($optionName,$optionValue) {
425381
}
426382

427383
// reload the options
428-
self::loadOptions($options);
384+
self::$options = $options;
429385

430386
// dump the YAML
431387
$configOutput = Yaml::dump($options, 3);

0 commit comments

Comments
 (0)