Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d6d187d

Browse files
committed
Add & use OptionResolverIntrospector
1 parent 8bbb5e7 commit d6d187d

File tree

7 files changed

+361
-18
lines changed

7 files changed

+361
-18
lines changed

src/Symfony/Component/Form/Console/Descriptor/Descriptor.php

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
use Symfony\Component\Console\Style\SymfonyStyle;
1919
use Symfony\Component\Form\ResolvedFormTypeInterface;
2020
use Symfony\Component\Form\Util\OptionsResolverWrapper;
21+
use Symfony\Component\OptionsResolver\Debug\OptionsResolverIntrospector;
22+
use Symfony\Component\OptionsResolver\Exception\NoConfigurationException;
2123
use Symfony\Component\OptionsResolver\OptionsResolver;
2224

2325
/**
@@ -104,20 +106,25 @@ protected function collectOptions(ResolvedFormTypeInterface $type)
104106

105107
protected function getOptionDefinition(OptionsResolver $optionsResolver, $option)
106108
{
107-
$refObject = new \ReflectionObject($optionsResolver);
108-
foreach (array('defaults', 'lazy', 'allowedTypes', 'allowedValues', 'normalizers') as $name) {
109-
$property = $refObject->getProperty($name);
110-
$property->setAccessible(true);
111-
$value = $property->getValue($optionsResolver);
112-
if (array_key_exists($option, $value)) {
113-
$definition[$name] = $value[$option];
109+
$definition = array('required' => $optionsResolver->isRequired($option));
110+
111+
$introspector = new OptionsResolverIntrospector($optionsResolver);
112+
113+
$map = array(
114+
'default' => 'getDefault',
115+
'lazy' => 'getLazyClosures',
116+
'allowedTypes' => 'getAllowedTypes',
117+
'allowedValues' => 'getAllowedValues',
118+
'normalizer' => 'getNormalizer',
119+
);
120+
121+
foreach ($map as $key => $method) {
122+
try {
123+
$definition[$key] = $introspector->{$method}($option);
124+
} catch (NoConfigurationException $e) {
125+
// noop
114126
}
115127
}
116-
$definition['required'] = $optionsResolver->isRequired($option);
117-
118-
if (isset($definition['lazy']) && 1 === count($definition['lazy'])) {
119-
$definition['lazy'] = $definition['lazy'][0];
120-
}
121128

122129
return $definition;
123130
}

src/Symfony/Component/Form/Console/Descriptor/JsonDescriptor.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,20 +61,20 @@ protected function describeOption(OptionsResolver $optionsResolver, array $optio
6161

6262
$map = array(
6363
'required' => 'required',
64-
'default' => 'defaults',
64+
'default' => 'default',
6565
'allowed_types' => 'allowedTypes',
6666
'allowed_values' => 'allowedValues',
6767
);
6868
foreach ($map as $label => $name) {
6969
if (array_key_exists($name, $definition)) {
7070
$data[$label] = $definition[$name];
7171

72-
if ('defaults' === $name) {
72+
if ('default' === $name) {
7373
$data['is_lazy'] = isset($definition['lazy']);
7474
}
7575
}
7676
}
77-
$data['has_normalizer'] = isset($definition['normalizers']);
77+
$data['has_normalizer'] = isset($definition['normalizer']);
7878

7979
$this->writeData($data, $options);
8080
}

src/Symfony/Component/Form/Console/Descriptor/TextDescriptor.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,15 @@ protected function describeOption(OptionsResolver $optionsResolver, array $optio
103103
$dump = $this->getDumpFunction();
104104
$map = array(
105105
'Required' => 'required',
106-
'Default' => 'defaults',
106+
'Default' => 'default',
107107
'Allowed types' => 'allowedTypes',
108108
'Allowed values' => 'allowedValues',
109-
'Normalizer' => 'normalizers',
109+
'Normalizer' => 'normalizer',
110110
);
111111
$rows = array();
112112
foreach ($map as $label => $name) {
113113
$value = array_key_exists($name, $definition) ? $dump($definition[$name]) : '-';
114-
if ('defaults' === $name && isset($definition['lazy'])) {
114+
if ('default' === $name && isset($definition['lazy'])) {
115115
$value = "Value: $value\n\nClosure(s): ".$dump($definition['lazy']);
116116
}
117117

src/Symfony/Component/OptionsResolver/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
3.4.0
5+
-----
6+
7+
* added `OptionsResolverIntrospector` to inspect options definitions inside an `OptionsResolver` instance
8+
49
2.6.0
510
-----
611

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\OptionsResolver\Debug;
13+
14+
use Symfony\Component\OptionsResolver\Exception\NoConfigurationException;
15+
use Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException;
16+
use Symfony\Component\OptionsResolver\OptionsResolver;
17+
18+
/**
19+
* @author Maxime Steinhausser <[email protected]>
20+
*
21+
* @final
22+
*/
23+
class OptionsResolverIntrospector
24+
{
25+
private $get;
26+
27+
public function __construct(OptionsResolver $optionsResolver)
28+
{
29+
$this->get = \Closure::bind(function ($property, $option, $message) {
30+
/** @var OptionsResolver $this */
31+
if (!$this->isDefined($option)) {
32+
throw new UndefinedOptionsException(sprintf('The option "%s" does not exist.', $option));
33+
}
34+
35+
if (!array_key_exists($option, $this->{$property})) {
36+
throw new NoConfigurationException($message);
37+
}
38+
39+
return $this->{$property}[$option];
40+
}, $optionsResolver, $optionsResolver);
41+
}
42+
43+
/**
44+
* @param string $option
45+
*
46+
* @return mixed
47+
*
48+
* @throws NoConfigurationException on no configured value
49+
*/
50+
public function getDefault($option)
51+
{
52+
return call_user_func($this->get, 'defaults', $option, sprintf('No default value was set for the "%s" option.', $option));
53+
}
54+
55+
/**
56+
* @param string $option
57+
*
58+
* @return \Closure[]
59+
*
60+
* @throws NoConfigurationException on no configured closures
61+
*/
62+
public function getLazyClosures($option)
63+
{
64+
return call_user_func($this->get, 'lazy', $option, sprintf('No lazy closures were set for the "%s" option.', $option));
65+
}
66+
67+
/**
68+
* @param string $option
69+
*
70+
* @return string[]
71+
*
72+
* @throws NoConfigurationException on no configured types
73+
*/
74+
public function getAllowedTypes($option)
75+
{
76+
return call_user_func($this->get, 'allowedTypes', $option, sprintf('No allowed types were set for the "%s" option.', $option));
77+
}
78+
79+
/**
80+
* @param string $option
81+
*
82+
* @return mixed[]
83+
*
84+
* @throws NoConfigurationException on no configured values
85+
*/
86+
public function getAllowedValues($option)
87+
{
88+
return call_user_func($this->get, 'allowedValues', $option, sprintf('No allowed values were set for the "%s" option.', $option));
89+
}
90+
91+
/**
92+
* @param string $option
93+
*
94+
* @return \Closure
95+
*
96+
* @throws NoConfigurationException on no configured normalizer
97+
*/
98+
public function getNormalizer($option)
99+
{
100+
return call_user_func($this->get, 'normalizers', $option, sprintf('No normalizer was set for the "%s" option.', $option));
101+
}
102+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\OptionsResolver\Exception;
13+
14+
use Symfony\Component\OptionsResolver\Debug\OptionsResolverIntrospector;
15+
16+
/**
17+
* Thrown when trying to introspect an option definition property
18+
* for which no value was configured inside the OptionsResolver instance.
19+
*
20+
* @see OptionsResolverIntrospector
21+
*
22+
* @author Maxime Steinhausser <[email protected]>
23+
*/
24+
class NoConfigurationException extends \RuntimeException implements ExceptionInterface
25+
{
26+
}

0 commit comments

Comments
 (0)