8000 [OptionsParser] Added method replaceDefaults() · symfony/symfony@04522ca · GitHub
[go: up one dir, main page]

Skip to content

Commit 04522ca

Browse files
committed
[OptionsParser] Added method replaceDefaults()
1 parent b9d053e commit 04522ca

File tree

3 files changed

+50
-6
lines changed

3 files changed

+50
-6
lines changed

src/Symfony/Component/OptionsParser/OptionsParser.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,29 @@ public function setDefaults(array $defaultValues)
7272
}
7373
}
7474

75+
/**
76+
* Replaces default option values.
77+
*
78+
* Old defaults are erased, which means that closures passed here can't
79+
* access the previous default value. This may be useful to improve
80+
* performance if the previous default value is calculated by an expensive
81+
* closure.
82+
*
83+
* @param array $options A list of option names as keys and default values
84+
* as values. The option values may be closures
85+
* of the following signature:
86+
*
87+
* - function (Options $options)
88+
*/
89+
public function replaceDefaults(array $defaultValues)
90+
{
91+
foreach ($defaultValues as $option => $value) {
92+
unset($this->defaultOptions[$option]);
93+
$this->defaultOptions[$option] = $value;
94+
$this->knownOptions[$option] = true;
95+
}
96+
}
97+
7598
/**
7699
* Sets optional options.
77100
*

src/Symfony/Component/OptionsParser/Tests/OptionParserTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,27 @@ public function testParseLazyDependencyOnRequired()
145145
), $this->parser->parse($options));
146146
}
147147

148+
public function testParseLazyReplaceDefaults()
149+
{
150+
$test = $this;
151+
152+
$this->parser->setDefaults(array(
153+
'one' => function (Options $options) use ($test) {
154+
$test->fail('Previous closure should not be executed');
155+
},
156+
));
157+
158+
$this->parser->replaceDefaults(array(
159+
'one' => function (Options $options, $previousValue) {
160+
return '1';
161+
},
162+
));
163+
164+
$this->assertEquals(array(
165+
'one' => '1',
166+
), $this->parser->parse(array()));
167+
}
168+
148169
/**
149170
* @expectedException Symfony\Component\OptionsParser\Exception\InvalidOptionsException
150171
*/

src/Symfony/Component/OptionsParser/Tests/OptionsTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,24 +80,24 @@ public function testLazyOption()
8080
$this->assertEquals('dynamic', $this->options['foo']);
8181
}
8282

83-
public function testLazyOptionWithEagerCurrentValue()
83+
public function testLazyOptionWithEagerPreviousValue()
8484
{
8585
$test = $this;
8686

8787
// defined by superclass
8888
$this->options['foo'] = 'bar';
8989

9090
// defined by subclass
91-
$this->options['foo'] = function (Options $options, $currentValue) use ($test) {
92-
$test->assertEquals('bar', $currentValue);
91+
$this->options['foo'] = function (Options $options, $previousValue) use ($test) {
92+
$test->assertEquals('bar', $previousValue);
9393

9494
return 'dynamic';
9595
};
9696

9797
$this->assertEquals('dynamic', $this->options['foo']);
9898
}
9999

100-
public function testLazyOptionWithLazyCurrentValue()
100+
public function testLazyOptionWithLazyPreviousValue()
101101
{
102102
$test = $this;
103103

@@ -107,8 +107,8 @@ public function testLazyOptionWithLazyCurrentValue()
107107
};
108108

109109
// defined by subclass
110-
$this->options['foo'] = function (Options $options, $currentValue) use ($test) {
111-
$test->assertEquals('bar', $currentValue);
110+
$this->options['foo'] = function (Options $options, $previousValue) use ($test) {
111+
$test->assertEquals('bar', $previousValue);
112112

113113
return 'dynamic';
114114
};

0 commit comments

Comments
 (0)
0