Skip to content

[RFC][DX][OptionsResolver] Allow setting info message per option #35400

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 1 commit into from
Feb 10, 2020
Merged
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
10 changes: 9 additions & 1 deletion src/Symfony/Component/Form/Console/Descriptor/Descriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,15 @@ protected function collectOptions(ResolvedFormTypeInterface $type)

protected function getOptionDefinition(OptionsResolver $optionsResolver, string $option)
{
$definition = [
$definition = [];

if ($info = $optionsResolver->getInfo($option)) {
$definition = [
'info' => $info,
];
}

$definition += [
'required' => $optionsResolver->isRequired($option),
'deprecated' => $optionsResolver->isDeprecated($option),
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ protected function describeOption(OptionsResolver $optionsResolver, array $optio
}
}
$map += [
'info' => 'info',
'required' => 'required',
'default' => 'default',
'allowed_types' => 'allowedTypes',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ protected function describeOption(OptionsResolver $optionsResolver, array $optio
];
}
$map += [
'Info' => 'info',
'Required' => 'required',
'Default' => 'default',
'Allowed types' => 'allowedTypes',
Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Component/Form/Tests/Command/DebugCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ public function testDebugCustomFormTypeOption()
Symfony\Component\Form\Tests\Command\FooType (foo)
==================================================

---------------- -----------%s
Info "Info" %s
---------------- -----------%s
Required true %s
---------------- -----------%s
Expand Down Expand Up @@ -209,5 +211,6 @@ public function configureOptions(OptionsResolver $resolver)
$resolver->setNormalizer('foo', function (Options $options, $value) {
return (string) $value;
});
$resolver->setInfo('foo', 'Info');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Symfony\Component\Form\Extension\Core\Type\ChoiceType (choice_translation_domain)
=================================================================================

---------------- -----------%s
Info - %s
---------------- -----------%s
Required false %s
---------------- -----------%s
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ Symfony\Component\Form\Tests\Console\Descriptor\FooType (bar)
Deprecated true
--------------------- -----------------------------------
Deprecation message "The option "bar" is deprecated."
--------------------- -----------------------------------
Info -
--------------------- -----------------------------------
Required false
--------------------- -----------------------------------
Expand All @@ -15,4 +17,4 @@ Symfony\Component\Form\Tests\Console\Descriptor\FooType (bar)
Allowed values -
--------------------- -----------------------------------
Normalizers -
--------------------- -----------------------------------
--------------------- -----------------------------------
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ Symfony\Component\Form\Tests\Console\Descriptor\FooType (empty_data)
====================================================================

---------------- ----------------------%s
Info - %s
---------------- ----------------------%s
Required false %s
---------------- ----------------------%s
Default Value: null %s
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Symfony\Component\Form\Tests\Console\Descriptor\FooType (foo)
=============================================================

---------------- -----------%s
Info - %s
---------------- -----------%s
Required true %s
---------------- -----------%s
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Form/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"php": "^7.2.5",
"symfony/event-dispatcher": "^4.4|^5.0",
"symfony/intl": "^4.4|^5.0",
"symfony/options-resolver": "^5.0",
"symfony/options-resolver": "^5.1",
"symfony/polyfill-ctype": "~1.8",
"symfony/polyfill-mbstring": "~1.0",
"symfony/property-access": "^5.0",
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/OptionsResolver/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
-----

* added fluent configuration of options using `OptionResolver::define()`
* added `setInfo()` and `getInfo()` methods

5.0.0
-----
Expand Down
14 changes: 14 additions & 0 deletions src/Symfony/Component/OptionsResolver/OptionConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,18 @@ public function required(): self

return $this;
}

/**
* Sets an info message for an option.
*
* @return $this
*
* @throws AccessException If called from a lazy option or normalizer
*/
public function info(string $info): self
{
$this->resolver->setInfo($this->name, $info);

return $this;
}
}
47 changes: 46 additions & 1 deletion src/Symfony/Component/OptionsResolver/OptionsResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ class OptionsResolver implements Options
*/
private $allowedTypes = [];

/**
* A list of info messages for each option.
*/
private $info = [];

/**
* A list of closures for evaluating lazy options.
*/
Expand Down Expand Up @@ -715,6 +720,41 @@ public function define(string $option): OptionConfigurator
return new OptionConfigurator($option, $this);
}

/**
* Sets an info message for an option.
*
* @return $this
*
* @throws UndefinedOptionsException If the option is undefined
* @throws AccessException If called from a lazy option or normalizer
*/
public function setInfo(string $option, string $info): self
{
if ($this->locked) {
throw new AccessException('The Info message cannot be set from a lazy option or normalizer.');
}

if (!isset($this->defined[$option])) {
throw new UndefinedOptionsException(sprintf('The option "%s" does not exist. Defined options are: "%s".', $this->formatOptions([$option]), implode('", "', array_keys($this->defined))));
}

$this->info[$option] = $info;

return $this;
}

/**
* Gets the info message for an option.
*/
public function getInfo(string $option): ?string
{
if (!isset($this->defined[$option])) {
throw new UndefinedOptionsException(sprintf('The option "%s" does not exist. Defined options are: "%s".', $this->formatOptions([$option]), implode('", "', array_keys($this->defined))));
}

return $this->info[$option] ?? null;
}

/**
* Removes the option with the given name.
*
Expand All @@ -734,7 +774,7 @@ public function remove($optionNames)

foreach ((array) $optionNames as $option) {
unset($this->defined[$option], $this->defaults[$option], $this->required[$option], $this->resolved[$option]);
unset($this->lazy[$option], $this->normalizers[$option], $this->allowedTypes[$option], $this->allowedValues[$option]);
unset($this->lazy[$option], $this->normalizers[$option], $this->allowedTypes[$option], $this->allowedValues[$option], $this->info[$option]);
}

return $this;
Expand Down Expand Up @@ -763,6 +803,7 @@ public function clear()
$this->allowedTypes = [];
$this->allowedValues = [];
$this->deprecated = [];
$this->info = [];

return $this;
}
Expand Down Expand Up @@ -996,6 +1037,10 @@ public function offsetGet($option, bool $triggerDeprecation = true)
);
}

if (isset($this->info[$option])) {
$message .= sprintf(' Info: %s.', $this->info[$option]);
}

throw new InvalidOptionsException($message);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\TestCase;
use Symfony\Component\OptionsResolver\Debug\OptionsResolverIntrospector;
use Symfony\Component\OptionsResolver\Exception\AccessException;
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
use Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;

Expand Down Expand Up @@ -2399,6 +2401,7 @@ public function testResolveOptionsDefinedByOptionConfigurator()
->normalize(static function (Options $options, $value) {
return $value;
})
->info('info message')
;
$introspector = new OptionsResolverIntrospector($this->resolver);

Expand All @@ -2409,5 +2412,63 @@ public function testResolveOptionsDefinedByOptionConfigurator()
$this->assertSame(['string', 'bool'], $introspector->getAllowedTypes('foo'));
$this->assertSame(['bar', 'zab'], $introspector->getAllowedValues('foo'));
$this->assertCount(1, $introspector->getNormalizers('foo'));
$this->assertSame('info message', $this->resolver->getInfo('foo'));
}

public function testGetInfo()
{
$info = 'The option info message';
$this->resolver->setDefined('foo');
$this->resolver->setInfo('foo', $info);

$this->assertSame($info, $this->resolver->getInfo('foo'));
}

public function testSetInfoOnNormalization()
{
$this->expectException(AccessException::class);
$this->expectExceptionMessage('The Info message cannot be set from a lazy option or normalizer.');

$this->resolver->setDefined('foo');
$this->resolver->setNormalizer('foo', static function (Options $options, $value) {
$options->setInfo('foo', 'Info');
});

$this->resolver->resolve(['foo' => 'bar']);
}

public function testSetInfoOnUndefinedOption()
{
$this->expectException(UndefinedOptionsException::class);
$this->expectExceptionMessage('The option "bar" does not exist. Defined options are: "foo".');

$this->resolver->setDefined('foo');
$this->resolver->setInfo('bar', 'The option info message');
}

public function testGetInfoOnUndefinedOption2()
{
$this->expectException(UndefinedOptionsException::class);
$this->expectExceptionMessage('The option "bar" does not exist. Defined options are: "foo".');

$this->resolver->setDefined('foo');
$this->resolver->getInfo('bar');
}

public function testInfoOnInvalidValue()
{
$this->expectException(InvalidOptionsException::class);
$this->expectExceptionMessage('The option "expires" with value DateTime is invalid. Info: A future date time.');

$this->resolver
->setRequired('expires')
->setInfo('expires', 'A future date time')
->setAllowedTypes('expires', \DateTime::class)
->setAllowedValues('expires', static function ($value) {
return $value >= new \DateTime('now');
})
;

$this->resolver->resolve(['expires' => new \DateTime('-1 hour')]);
}
}