8000 [POC] [OptionsResolver] Support nested options by wouterj · Pull Request #11509 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[POC] [OptionsResolver] Support nested options #11509

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\OptionsResolver;

/**
* @author Wouter J <wouter@wouterj.nl>
*/
interface NestableOptionsResolverInterface extends OptionsResolverInterface
{
/**
* Sets nested options resolvers.
*
* @param array $options A list of option names as keys and instances of
* OptionsResolverInterface as values.
*/
public function setNestedOptionsResolver(array $options);
}
16 changes: 10 additions & 6 deletions src/Symfony/Component/OptionsResolver/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,25 +97,29 @@ public function set($option, $value)
/**
* Sets the normalizer for a given option.
*
* Normalizers should be closures with the following signature:
* Normalizers should be valid callables with the following signature:
*
* <code>
* function (Options $options, $value)
* function (Options $options, $value, $option)
* </code>
*
* This closure will be evaluated once the option is read using
* {@link get()}. The closure has access to the resolved values of
* other options through the passed {@link Options} instance.
*
* @param string $option The name of the option.
* @param \Closure $normalizer The normalizer.
* @param callable $normalizer The normalizer.
*
* @throws OptionDefinitionException If options have already been read.
* Once options are read, the container
* becomes immutable.
*/
public function setNormalizer($option, \Closure $normalizer)
public function setNormalizer($option, $normalizer)
{
if (!is_callable($normalizer)) {
throw new \InvalidArgumentException('Normalizers should be a valid callable.');
}

if ($this->reading) {
throw new OptionDefinitionException('Normalizers cannot be added anymore once options have been read.');
}
Expand Down Expand Up @@ -500,11 +504,11 @@ private function normalize($option)
throw new OptionDefinitionException(sprintf('The options "%s" have a cyclic dependency.', implode('", "', $conflicts)));
}

/** @var \Closure $normalizer */
/** @var callable $normalizer */
$normalizer = $this->normalizers[$option];

$this->lock[$option] = true;
$this->options[$option] = $normalizer($this, array_key_exists($option, $this->options) ? $this->options[$option] : null);
$this->options[$option] = call_user_func($normalizer, $this, array_key_exists($option, $this->options) ? $this->options[$option] : null, $option);
unset($this->lock[$option]);

// The option is now normalized
Expand Down
39 changes: 38 additions & 1 deletion src/Symfony/Component/OptionsResolver/OptionsResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* @author Bernhard Schussek <bschussek@gmail.com>
* @author Tobias Schultze <http://tobion.de>
*/
class OptionsResolver implements OptionsResolverInterface
class OptionsResolver implements NestableOptionsResolverInterface
{
/**
* The default option values.
Expand Down Expand Up @@ -53,6 +53,12 @@ class OptionsResolver implements OptionsResolverInterface
*/
private $allowedTypes = array();

/**
* A list of the nested option resolvers.
* @var array
*/
private $optionResolvers = array();

/**
* Creates a new instance.
*/
Expand Down Expand Up @@ -181,6 +187,37 @@ public function addAllowedTypes(array $allowedTypes)
return $this;
}

/**
* {@inheritdoc}
*/
public function setNestedOptionsResolver(array $options)
{
$this->validateOptionsExistence($options);

foreach ($options as $option => $resolver) {
if (!$resolver instanceof OptionsResolverInterface) {
throw new \InvalidArgumentException('Nested option resolvers have to implement OptionsResolverInterface.');
}
$this->optionResolvers[$option] = $resolver;
$this->setNormalizers(array($option => array($this, 'nestedNormalizer')));
}

return $this;
}

/**
* This normalizer will be called when an option has a nested options
* resolver.
*
* @param Options $options
* @param null|array $value
* @param string $option
*/
public function nestedNormalizer(Options $options, $value, $option)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing return doc. also this method is only for internal purposes. and manually calling it can result in php error (when option is not set in optionResolvers).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I should tag it with @internal?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, mark it with @internal.

{
return $this->optionResolvers[$option]->resolve($value ?: array());
}

/**
* {@inheritdoc}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -717,4 +717,54 @@ public function testClone()
'three' => '3',
), $clone->resolve());
}

public function testNestedResolversForRequiredOption()
{
$this->resolver->setRequired(array('db'));
$this->resolver->setNestedOptionsResolver(array(
'db' => $this->getNestedResolver(),
));

$this->assertEquals(array(
'db' => array(
'dsn' => 'sqlite:app.sqlite',
'user' => 'root',
'password' => '',
'port' => 3306,
),
), $this->resolver->resolve(array(
'db' => array(
'dsn' => 'sqlite:app.sqlite',
),
)));
}

/**
* @expectedException Symfony\Component\OptionsResolver\Exception\MissingOptionsException
*
* Nested options resolvers will always be executed, eventhough the option
* is missing. See {@link https://github.com/symfony/symfony/issues/9174}
*/
public function testNestedResolversForOptionalOption()
{
$this->resolver->setOptional(array('db'));
$this->resolver->setNestedOptionsResolver(array(
'db' => $this->getNestedResolver(),
));

$this->assertEquals(array(), $this->resolver->resolve(array()));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The assert is actually not needed as your expecting an exception.

}

private function getNestedResolver()
{
$nestedResolver = new OptionsResolver();
$nestedResolver->setDefaults(array(
'user' => 'root',
'password' => '',
'port' => 3306,
));
$nestedResolver->setRequired(array('dsn'));

return $nestedResolver;
}
}
0