Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 006105f

Browse filesBrowse files
committed
Add & use OptionsResolverIntrospector
1 parent f06eba2 commit 006105f
Copy full SHA for 006105f

File tree

6 files changed

+152
-18
lines changed
Filter options

6 files changed

+152
-18
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Console/Descriptor/Descriptor.php
+19-12Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Console/Descriptor/JsonDescriptor.php
+3-3Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Console/Descriptor/TextDescriptor.php
+3-3Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/OptionsResolver/CHANGELOG.md
+5Lines 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

+96Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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\OptionsResolver;
16+
17+
/**
18+
* @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
19+
*
20+
* @final
21+
*/
22+
class OptionsResolverIntrospector
23+
{
24+
private $get;
25+
26+
public function __construct(OptionsResolver $optionsResolver)
27+
{
28+
$this->get = \Closure::bind(function ($property, $option, $message) {
29+
if (!array_key_exists($option, $this->{$property})) {
30+
throw new NoConfigurationException($message);
31+
}
32+
33+
return $this->{$property}[$option];
34+
}, $optionsResolver, $optionsResolver);
35+
}
36+
37+
/**
38+
* @param string $option
39+
*
40+
* @return mixed
41+
*
42+
* @throws NoConfigurationException on no configured value
43+
*/
44+
public function getDefault($option)
45+
{
46+
return call_user_func($this->get, 'defaults', $option, sprintf('No default value was set for the "%s" option', $option));
47+
}
48+
49+
/**
50+
* @param string $option
51+
*
52+
* @return \Closure[]
53+
*
54+
* @throws NoConfigurationException on no configured closures
55+
*/
56+
public function getLazyClosures($option)
57+
{
58+
return call_user_func($this->get, 'lazy', $option, sprintf('No lazy closures were set for the "%s" option', $option));
59+
}
60+
61+
/**
62+
* @param string $option
63+
*
64+
* @return string[]
65+
*
66+
* @throws NoConfigurationException on no configured types
67+
*/
68+
public function getAllowedTypes($option)
69+
{
70+
return call_user_func($this->get, 'allowedTypes', $option, sprintf('No allowed types were set for the "%s" option', $option));
71+
}
72+
73+
/**
74+
* @param string $option
75+
*
76+
* @return mixed[]
77+
*
78+
* @throws NoConfigurationException on no configured values
79+
*/
80+
public function getAllowedValues($option)
81+
{
82+
return call_user_func($this->get, 'allowedValues', $option, sprintf('No allowed values were set for the "%s" option', $option));
83+
}
84+
85+
/**
86+
* @param string $option
87+
*
88+
* @return \Closure
89+
*
90+
* @throws NoConfigurationException on no configured normalizer
91+
*/
92+
public function getNormalizer($option)
93+
{
94+
return call_user_func($this->get, 'normalizers', $option, sprintf('No normalizer was set for the "%s" option', $option));
95+
}
96+
}
+26Lines 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 <fabien@symfony.com>
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 <maxime.steinhausser@gmail.com>
23+
*/
24+
class NoConfigurationException extends \RuntimeException implements ExceptionInterface
25+
{
26+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.