From 04522ca4ed80b27a864fee29bb4d84c74a853e79 Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Mon, 23 Apr 2012 12:10:43 +0200 Subject: [PATCH] [OptionsParser] Added method replaceDefaults() --- .../Component/OptionsParser/OptionsParser.php | 23 +++++++++++++++++++ .../OptionsParser/Tests/OptionParserTest.php | 21 +++++++++++++++++ .../OptionsParser/Tests/OptionsTest.php | 12 +++++----- 3 files changed, 50 insertions(+), 6 deletions(-) diff --git a/src/Symfony/Component/OptionsParser/OptionsParser.php b/src/Symfony/Component/OptionsParser/OptionsParser.php index 3a5d6cc85a..444994ee1b 100644 --- a/src/Symfony/Component/OptionsParser/OptionsParser.php +++ b/src/Symfony/Component/OptionsParser/OptionsParser.php @@ -72,6 +72,29 @@ class OptionsParser } } + /** + * Replaces default option values. + * + * Old defaults are erased, which means that closures passed here can't + * access the previous default value. This may be useful to improve + * performance if the previous default value is calculated by an expensive + * closure. + * + * @param array $options A list of option names as keys and default values + * as values. The option values may be closures + * of the following signature: + * + * - function (Options $options) + */ + public function replaceDefaults(array $defaultValues) + { + foreach ($defaultValues as $option => $value) { + unset($this->defaultOptions[$option]); + $this->defaultOptions[$option] = $value; + $this->knownOptions[$option] = true; + } + } + /** * Sets optional options. * diff --git a/src/Symfony/Component/OptionsParser/Tests/OptionParserTest.php b/src/Symfony/Component/OptionsParser/Tests/OptionParserTest.php index 6f6e416b40..70740e629f 100644 --- a/src/Symfony/Component/OptionsParser/Tests/OptionParserTest.php +++ b/src/Symfony/Component/OptionsParser/Tests/OptionParserTest.php @@ -145,6 +145,27 @@ class OptionsParserTest extends \PHPUnit_Framework_TestCase ), $this->parser->parse($options)); } + public function testParseLazyReplaceDefaults() + { + $test = $this; + + $this->parser->setDefaults(array( + 'one' => function (Options $options) use ($test) { + $test->fail('Previous closure should not be executed'); + }, + )); + + $this->parser->replaceDefaults(array( + 'one' => function (Options $options, $previousValue) { + return '1'; + }, + )); + + $this->assertEquals(array( + 'one' => '1', + ), $this->parser->parse(array())); + } + /** * @expectedException Symfony\Component\OptionsParser\Exception\InvalidOptionsException */ diff --git a/src/Symfony/Component/OptionsParser/Tests/OptionsTest.php b/src/Symfony/Component/OptionsParser/Tests/OptionsTest.php index 0d4ab9209b..058a738369 100644 --- a/src/Symfony/Component/OptionsParser/Tests/OptionsTest.php +++ b/src/Symfony/Component/OptionsParser/Tests/OptionsTest.php @@ -80,7 +80,7 @@ class OptionsTest extends \PHPUnit_Framework_TestCase $this->assertEquals('dynamic', $this->options['foo']); } - public function testLazyOptionWithEagerCurrentValue() + public function testLazyOptionWithEagerPreviousValue() { $test = $this; @@ -88,8 +88,8 @@ class OptionsTest extends \PHPUnit_Framework_TestCase $this->options['foo'] = 'bar'; // defined by subclass - $this->options['foo'] = function (Options $options, $currentValue) use ($test) { - $test->assertEquals('bar', $currentValue); + $this->options['foo'] = function (Options $options, $previousValue) use ($test) { + $test->assertEquals('bar', $previousValue); return 'dynamic'; }; @@ -97,7 +97,7 @@ class OptionsTest extends \PHPUnit_Framework_TestCase $this->assertEquals('dynamic', $this->options['foo']); } - public function testLazyOptionWithLazyCurrentValue() + public function testLazyOptionWithLazyPreviousValue() { $test = $this; @@ -107,8 +107,8 @@ class OptionsTest extends \PHPUnit_Framework_TestCase }; // defined by subclass - $this->options['foo'] = function (Options $options, $currentValue) use ($test) { - $test->assertEquals('bar', $currentValue); + $this->options['foo'] = function (Options $options, $previousValue) use ($test) { + $test->assertEquals('bar', $previousValue); return 'dynamic'; };