Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit04522ca

Browse files
committed
[OptionsParser] Added method replaceDefaults()
1 parentb9d053e commit04522ca

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+
publicfunctionreplaceDefaults(array$defaultValues)
90+
{
91+
foreach ($defaultValuesas$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+
publicfunctiontestParseLazyReplaceDefaults()
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-
publicfunctiontestLazyOptionWithEagerCurrentValue()
83+
publicfunctiontestLazyOptionWithEagerPreviousValue()
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-
publicfunctiontestLazyOptionWithLazyCurrentValue()
100+
publicfunctiontestLazyOptionWithLazyPreviousValue()
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)

[8]ページ先頭

©2009-2025 Movatter.jp