8000 Extracted OptionsResolver component out of Form by webmozart · Pull Request #3968 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Extracted OptionsResolver component out of Form #3968

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
May 15, 2012
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[OptionsParser] Added method replaceDefaults()
  • Loading branch information
webmozart committed May 14, 2012
commit 04522ca4ed80b27a864fee29bb4d84c74a853e79
23 changes: 23 additions & 0 deletions src/Symfony/Component/OptionsParser/OptionsParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,29 @@ public function setDefaults(array $defaultValues)
}
}

/**
* 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.
*
Expand Down
21 changes: 21 additions & 0 deletions src/Symfony/Component/OptionsParser/Tests/OptionParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,27 @@ public function testParseLazyDependencyOnRequired()
), $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
*/
Expand Down
12 changes: 6 additions & 6 deletions src/Symfony/Component/OptionsParser/Tests/OptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,24 +80,24 @@ public function testLazyOption()
$this->assertEquals('dynamic', $this->options['foo']);
}

public function testLazyOptionWithEagerCurrentValue()
public function testLazyOptionWithEagerPreviousValue()
{
$test = $this;

// defined by superclass
$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';
};

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

public function testLazyOptionWithLazyCurrentValue()
public function testLazyOptionWithLazyPreviousValue()
{
$test = $this;

Expand All @@ -107,8 +107,8 @@ public function testLazyOptionWithLazyCurrentValue()
};

// 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';
};
Expand Down
0