Skip to content

Commit a1b53dc

Browse files
committed
reworking the config
1 parent 94f4ac6 commit a1b53dc

File tree

2 files changed

+107
-53
lines changed

2 files changed

+107
-53
lines changed

src/PatternLab/Config.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,11 +374,16 @@ protected static function writeUpdateConfigOption($optionName,$optionValue) {
374374
Console::writeError("Config parse error in <path>".self::$userConfigPath."</path>: ".$e->getMessage());
375375
}
376376

377-
print "foo ".$optionName." ".$optionValue;
377+
// set this option for the current running of the app
378378
self::setOption($optionName, $optionValue);
379379

380+
// modify the yaml file results
381+
$arrayFinder = new ArrayFinder($options);
382+
$arrayFinder->set($optionName, $optionValue);
383+
$options = $arrayFinder->get();
384+
380385
// dump the YAML
381-
$configOutput = Yaml::dump(self::$options, 3);
386+
$configOutput = Yaml::dump($options, 3);
382387

383388
// write out the new config file
384389
file_put_contents(self::$userConfigPath,$configOutput);

src/PatternLab/Console/Commands/ConfigCommand.php

Lines changed: 100 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@
1515
use \PatternLab\Console\Command;
1616
use \PatternLab\Generator;
1717
use \PatternLab\Timer;
18+
use \Shudrum\Component\ArrayFinder\ArrayFinder;
1819

1920
class ConfigCommand extends Command {
2021

22+
private $lengthLong;
23+
2124
public function __construct() {
2225

2326
parent::__construct();
@@ -35,68 +38,114 @@ public function run() {
3538

3639
if (Console::findCommandOption("list")) {
3740

38-
// get all of the options
39-
$options = Config::getOptions();
40-
41-
// sort 'em alphabetically
42-
ksort($options);
43-
44-
// find length of longest option
45-
$lengthLong = 0;
46-
foreach ($options as $optionName => $optionValue) {
47-
$lengthLong = (strlen($optionName) > $lengthLong) ? strlen($optionName) : $lengthLong;
48-
}
49-
50-
// iterate over each option and spit it out
51-
foreach ($options as $optionName => $optionValue) {
52-
$optionValue = (is_array($optionValue)) ? implode(", ",$optionValue) : $optionValue;
53-
$optionValue = (!$optionValue) ? "false" : $optionValue;
54-
$spacer = Console::getSpacer($lengthLong,strlen($optionName));
55-
Console::writeLine("<info>".$optionName.":</info>".$spacer.$optionValue);
56-
}
41+
$this->listOptions();
5742

5843
} else if (Console::findCommandOption("get")) {
5944

60-
// figure out which optino was passed
61-
$searchOption = Console::findCommandOptionValue("get");
62-
$optionValue = Config::getOption($searchOption);
63-
64-
// write it out
65-
if (!$optionValue) {
66-
Console::writeError("the --get value you provided, <info>".$searchOption."</info>, does not exists in the config...");
67-
} else {
68-
$optionValue = (is_array($optionValue)) ? implode(", ",$optionValue) : $optionValue;
69-
$optionValue = (!$optionValue) ? "false" : $optionValue;
70-
Console::writeInfo($searchOption.": <ok>".$optionValue."</ok>");
71-
}
45+
$this->getOption();
7246

7347
} else if (Console::findCommandOption("set")) {
7448

75-
// find the value that was passed
76-
$updateOption = Console::findCommandOptionValue("set");
77-
$updateOptionBits = explode("=",$updateOption);
78-
if (count($updateOptionBits) == 1) {
79-
Console::writeError("the --set value should look like <info>optionName=\"optionValue\"</info>. nothing was updated...");
80-
}
81-
82-
// set the name and value that were passed
83-
$updateName = $updateOptionBits[0];
84-
$updateValue = (($updateOptionBits[1][0] == "\"") || ($updateOptionBits[1][0] == "'")) ? substr($updateOptionBits[1],1,strlen($updateOptionBits[1])-1) : $updateOptionBits[1];
49+
$this->setOption();
8550

86-
// make sure the option being updated already exists
87-
$currentValue = Config::getOption($updateName);
51+
} else {
8852

89-
if (!$currentValue) {
90-
Console::writeError("the --set option you provided, <info>".$updateName."</info>, does not exists in the config. nothing will be updated...");
91-
} else {
92-
Config::updateConfigOption($updateName,$updateValue);
93-
Console::writeInfo("config option updated...");
94-
}
53+
Console::writeHelpCommand($this->command);
9554

55+
}
56+
57+
}
58+
59+
/**
60+
* Get the given option and return its value
61+
*/
62+
private function getOption() {
63+
64+
// figure out which option was passed
65+
$searchOption = Console::findCommandOptionValue("get");
66+
$optionValue = Config::getOption($searchOption);
67+
68+
// write it out
69+
if (!$optionValue) {
70+
Console::writeError("the --get value you provided, <info>".$searchOption."</info>, does not exists in the config...");
71+
} else {
72+
$optionValue = (is_array($optionValue)) ? implode(", ",$optionValue) : $optionValue;
73+
$optionValue = (!$optionValue) ? "false" : $optionValue;
74+
Console::writeInfo($searchOption.": <ok>".$optionValue."</ok>");
75+
}
76+
77+
}
78+
79+
/**
80+
* List out of the options available in the config
81+
*/
82+
private function listOptions() {
83+
84+
// get all of the options
85+
$options = Config::getOptions();
86+
87+
// sort 'em alphabetically
88+
ksort($options);
89+
90+
// find length of longest option
91+
$this->lengthLong = 0;
92+
foreach ($options as $optionName => $optionValue) {
93+
$this->lengthLong = (strlen($optionName) > $this->lengthLong) ? strlen($optionName) : $this->lengthLong;
94+
}
95+
96+
$this->writeOutOptions($options);
97+
98+
}
99+
100+
/**
101+
* Set the given option to the given value
102+
*/
103+
protected function setOption() {
104+
105+
// find the value that was passed
106+
$updateOption = Console::findCommandOptionValue("set");
107+
$updateOptionBits = explode("=",$updateOption);
108+
if (count($updateOptionBits) == 1) {
109+
Console::writeError("the --set value should look like <info>optionName=\"optionValue\"</info>. nothing was updated...");
110+
}
111+
112+
// set the name and value that were passed
113+
$updateName = $updateOptionBits[0];
114+
$updateValue = (($updateOptionBits[1][0] == "\"") || ($updateOptionBits[1][0] == "'")) ? substr($updateOptionBits[1],1,strlen($updateOptionBits[1])-1) : $updateOptionBits[1];
115+
116+
// make sure the option being updated already exists
117+
$currentValue = Config::getOption($updateName);
118+
119+
if (!$currentValue) {
120+
Console::writeError("the --set option you provided, <info>".$updateName."</info>, does not exists in the config. nothing will be updated...");
96121
} else {
122+
Config::updateConfigOption($updateName,$updateValue);
123+
Console::writeInfo("config option updated...");
124+
}
125+
126+
}
127+
128+
/**
129+
* Write out the given options. Check to see if it's a nested sequential or associative array
130+
* @param {Mixed} the options to check and write out
131+
* @param {String} copy to be added to the beginning of the option if nested
132+
*/
133+
private function writeOutOptions($options, $pre = "") {
134+
135+
foreach ($options as $optionName => $optionValue) {
97136

98-
// no acceptable options were passed so write out the help
99-
Console::writeHelpCommand($this->command);
137+
if (is_array($optionValue) && (count($optionValue) > 0) && !isset($optionValue[0])) {
138+
139+
$this->writeOutOptions($optionValue, $optionName.".");
140+
141+
} else {
142+
143+
$optionValue = (is_array($optionValue) && isset($optionValue[0])) ? implode(", ",$optionValue) : $optionValue;
144+
$optionValue = (!$optionValue) ? "false" : $optionValue;
145+
$spacer = Console::getSpacer($this->lengthLong,strlen($pre.$optionName));
146+
Console::writeLine("<info>".$pre.$optionName.":</info>".$spacer.$optionValue);
147+
148+
}
100149

101150
}
102151

0 commit comments

Comments
 (0)