File tree Expand file tree Collapse file tree 3 files changed +50
-6
lines changed
src/Symfony/Component/OptionsParser Expand file tree Collapse file tree 3 files changed +50
-6
lines changed Original file line number Diff line number Diff line change @@ -72,6 +72,29 @@ public function setDefaults(array $defaultValues)
72
72
}
73
73
}
74
74
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
+
75
98
/**
76
99
* Sets optional options.
77
100
*
Original file line number Diff line number Diff line change @@ -145,6 +145,27 @@ public function testParseLazyDependencyOnRequired()
145
145
), $ this ->parser ->parse ($ options ));
146
146
}
147
147
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
+
148
169
/**
149
170
* @expectedException Symfony\Component\OptionsParser\Exception\InvalidOptionsException
150
171
*/
Original file line number Diff line number Diff line change @@ -80,24 +80,24 @@ public function testLazyOption()
80
80
$ this ->assertEquals ('dynamic ' , $ this ->options ['foo ' ]);
81
81
}
82
82
83
- public function testLazyOptionWithEagerCurrentValue ()
83
+ public function testLazyOptionWithEagerPreviousValue ()
84
84
{
85
85
$ test = $ this ;
86
86
87
87
// defined by superclass
88
88
$ this ->options ['foo ' ] = 'bar ' ;
89
89
90
90
// 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 );
93
93
94
94
return 'dynamic ' ;
95
95
};
96
96
97
97
$ this ->assertEquals ('dynamic ' , $ this ->options ['foo ' ]);
98
98
}
99
99
100
- public function testLazyOptionWithLazyCurrentValue ()
100
+ public function testLazyOptionWithLazyPreviousValue ()
101
101
{
102
102
$ test = $ this ;
103
103
@@ -107,8 +107,8 @@ public function testLazyOptionWithLazyCurrentValue()
107
107
};
108
108
109
109
// 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 );
112
112
113
113
return 'dynamic ' ;
114
114
};
You can’t perform that action at this time.
0 commit comments