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 ccd5ce1

Browse filesBrowse files
committed
harden dependency with NestedOptions class
1 parent 9e4e02a commit ccd5ce1
Copy full SHA for ccd5ce1

File tree

2 files changed

+118
-7
lines changed
Filter options

2 files changed

+118
-7
lines changed
+98Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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;
13+
14+
use Symfony\Component\OptionsResolver\Exception\AccessException;
15+
16+
/**
17+
* Validates nested options and merges them with default values.
18+
*
19+
* This class is used internally by the OptionsResolver.
20+
* See {@link OptionsResolver::setNested()}.
21+
*
22+
* @author Jules Pietri <jules@heahprod.com>
23+
*
24+
* @internal
25+
*/
26+
class NestedOptions extends OptionsResolver
27+
{
28+
/**
29+
* The root options instance.
30+
*
31+
* @var OptionsResolver
32+
*/
33+
private $root;
34+
35+
/**
36+
* The root option name.
37+
*
38+
* @var string
39+
*/
40+
private $rootName;
41+
42+
/**
43+
* This class should only be instantiated from an OptionsResolver.
44+
*
45+
* See {@link OptionsResolver::setNested()}.
46+
*
47+
* @param string $rootName The root option name
48+
*/
49+
public function __construct($rootName)
50+
{
51+
$this->rootName = $rootName;
52+
}
53+
54+
/**
55+
* Binds the root options.
56+
*
57+
* This method should only be called from root OptionsResolver instance.
58+
* See {@link OptionsResolver::__clone()}.
59+
*
60+
* @param OptionsResolver $root The root options
61+
*
62+
* @return NestedOptions This instance
63+
*/
64+
public function setRoot(OptionsResolver $root)
65+
{
66+
$this->root = $root;
67+
68+
return $this;
69+
}
70+
71+
/**
72+
* {@inheritdoc}
73+
*
74+
* This method should only be called while the root option is resolved
75+
* {@link OptionsResolver::OffsetGet()}
76+
*
77+
* @throws AccessException If the root option is not locked
78+
*/
79+
public function resolve(array $options = array())
80+
{
81+
if (!$this->root->isLocked()) {
82+
throw new AccessException(sprintf('The Nested options of "%s" can only be resolved internally while their root is resolved.', $this->rootName));
83+
}
84+
85+
return parent::resolve($options);
86+
}
87+
88+
/**
89+
* @param string $option
90+
* @param \Closure $normalizer
91+
*
92+
* @return NestedOptions
93+
*/
94+
public function setNormalizer($option, \Closure $normalizer)
95+
{
96+
return parent::setNormalizer($option, $normalizer); // TODO: Change the autogenerated stub
97+
}
98+
}

‎src/Symfony/Component/OptionsResolver/OptionsResolver.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/OptionsResolver/OptionsResolver.php
+20-7Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class OptionsResolver implements Options
4444
/**
4545
* The nested options.
4646
*
47-
* @var OptionsResolver[]
47+
* @var NestedOptions[]
4848
*/
4949
private $nested = array();
5050

@@ -187,7 +187,7 @@ class OptionsResolver implements Options
187187
* @param string $option The name of the option
188188
* @param mixed $value The default value of the option
189189
*
190-
* @return OptionsResolver This instance or the nested instance
190+
* @return OptionsResolver|NestedOptions This instance or the nested instance
191191
*
192192
* @throws AccessException If called from a lazy option or normalizer
193193
*/
@@ -291,9 +291,9 @@ public function hasDefault($option)
291291
}
292292

293293
/**
294-
* Defines an option as a new self.
294+
* Defines an option as a new NestedOptions instance.
295295
*
296-
* Returns a new OptionsResolver instance to configure nested options.
296+
* Returns a new NestedOptions instance to configure nested options.
297297
*
298298
* $nestedOptions = $options->setNested('connexion', array(
299299
* 'host' => 'localhost',
@@ -313,7 +313,7 @@ public function hasDefault($option)
313313
* @param string $option The option name
314314
* @param array $defaults The default nested options
315315
*
316-
* @return OptionsResolver The nested options resolver
316+
* @return NestedOptions The nested options resolver
317317
*
318318
* @throws AccessException If called from a lazy option or normalizer
319319
*/
@@ -323,7 +323,7 @@ public function setNested($option, array $defaults = array())
323323
throw new AccessException('Options cannot be made nested from a lazy option or normalizer.');
324324
}
325325

326-
$nestedOptions = new self();
326+
$nestedOptions = new NestedOptions($option);
327327

328328
foreach ($defaults as $name => $default) {
329329
$nestedOptions->setDefault($name, $default);
@@ -979,6 +979,18 @@ public function allowExtraOptions($allow = true)
979979
return $this;
980980
}
981981

982+
/**
983+
* Returns whether this instance is locked.
984+
*
985+
* @return bool Whether this instance is locked
986+
*
987+
* @internal Used by {@see NestedOptions} to prevent external resolving
988+
*/
989+
public function isLocked()
990+
{
991+
return $this->locked;
992+
}
993+
982994
/**
983995
* Removes the option with the given name.
984996
*
@@ -1383,7 +1395,8 @@ public function count()
13831395
public function __clone()
13841396
{
13851397
foreach ($this->nested as $name => $options) {
1386-
$this->nested[$name] = clone $options;
1398+
$nestedOptions = clone $options;
1399+
$this->nested[$name] = $nestedOptions->setRoot($this);
13871400
}
13881401
}
13891402

0 commit comments

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