/usr/share/php/Symfony/Component/OptionsResolver/Options.php is in php-symfony-options-resolver 2.3.21+dfsg-4+deb8u3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 | <?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;
use Symfony\Component\OptionsResolver\Exception\OptionDefinitionException;
/**
* Container for resolving inter-dependent options.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class Options implements \ArrayAccess, \Iterator, \Countable
{
/**
* A list of option values.
* @var array
*/
private $options = array();
/**
* A list of normalizer closures.
* @var array
*/
private $normalizers = array();
/**
* A list of closures for evaluating lazy options.
* @var array
*/
private $lazy = array();
/**
* A list containing the currently locked options.
* @var array
*/
private $lock = array();
/**
* Whether at least one option has already been read.
*
* Once read, the options cannot be changed anymore. This is
* necessary in order to avoid inconsistencies during the resolving
* process. If any option is changed after being read, all evaluated
* lazy options that depend on this option would become invalid.
*
* @var bool
*/
private $reading = false;
/**
* Sets the value of a given option.
*
* You can set lazy options by passing a closure with the following
* signature:
*
* <code>
* function (Options $options)
* </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 mixed $value The value of the option.
*
* @throws OptionDefinitionException If options have already been read.
* Once options are read, the container
* becomes immutable.
*/
public function set($option, $value)
{
// Setting is not possible once an option is read, because then lazy
// options could manipulate the state of the object, leading to
// inconsistent results.
if ($this->reading) {
throw new OptionDefinitionException('Options cannot be set anymore once options have been read.');
}
// Setting is equivalent to overloading while discarding the previous
// option value
unset($this->options[$option]);
unset($this->lazy[$option]);
$this->overload($option, $value);
}
/**
* Sets the normalizer for a given option.
*
* Normalizers should be closures with the following signature:
*
* <code>
* function (Options $options, $value)
* </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.
*
* @throws OptionDefinitionException If options have already been read.
* Once options are read, the container
* becomes immutable.
*/
public function setNormalizer($option, \Closure $normalizer)
{
if ($this->reading) {
throw new OptionDefinitionException('Normalizers cannot be added anymore once options have been read.');
}
$this->normalizers[$option] = $normalizer;
}
/**
* Replaces the contents of the container with the given options.
*
* This method is a shortcut for {@link clear()} with subsequent
* calls to {@link set()}.
*
* @param array $options The options to set.
*
* @throws OptionDefinitionException If options have already been read.
* Once options are read, the container
* becomes immutable.
*/
public function replace(array $options)
{
if ($this->reading) {
throw new OptionDefinitionException('Options cannot be replaced anymore once options have been read.');
}
$this->options = array();
$this->lazy = array();
$this->normalizers = array();
foreach ($options as $option => $value) {
$this->overload($option, $value);
}
}
/**
* Overloads the value of a given option.
*
* Contrary to {@link set()}, this method keeps the previous default
* value of the option so that you can access it if you pass a closure.
* Passed closures should have the following signature:
*
* <code>
* function (Options $options, $value)
* </code>
*
* The second parameter passed to the closure is the current default
* value of the option.
*
* @param string $option The option name.
* @param mixed $value The option value.
*
* @throws OptionDefinitionException If options have already been read.
* Once options are read, the container
* becomes immutable.
*/
public function overload($option, $value)
{
if ($this->reading) {
throw new OptionDefinitionException('Options cannot be overloaded anymore once options have been read.');
}
// If an option is a closure that should be evaluated lazily, store it
// in the "lazy" property.
if ($value instanceof \Closure) {
$reflClosure = new \ReflectionFunction($value);
$params = $reflClosure->getParameters();
if (isset($params[0]) && null !== ($class = $params[0]->getClass()) && __CLASS__ === $class->name) {
// Initialize the option if no previous value exists
if (!isset($this->options[$option])) {
$this->options[$option] = null;
}
// Ignore previous lazy options if the closure has no second parameter
if (!isset($this->lazy[$option]) || !isset($params[1])) {
$this->lazy[$option] = array();
}
// Store closure for later evaluation
$this->lazy[$option][] = $value;
return;
}
}
// Remove lazy options by default
unset($this->lazy[$option]);
$this->options[$option] = $value;
}
/**
* Returns the value of the given option.
*
* If the option was a lazy option, it is evaluated now.
*
* @param string $option The option name.
*
* @return mixed The option value.
*
* @throws \OutOfBoundsException If the option does not exist.
* @throws OptionDefinitionException If a cyclic dependency is detected
* between two lazy options.
*/
public function get($option)
{
$this->reading = true;
if (!array_key_exists($option, $this->options)) {
throw new \OutOfBoundsException(sprintf('The option "%s" does not exist.', $option));
}
if (isset($this->lazy[$option])) {
$this->resolve($option);
}
if (isset($this->normalizers[$option])) {
$this->normalize($option);
}
return $this->options[$option];
}
/**
* Returns whether the given option exists.
*
* @param string $option The option name.
*
* @return bool Whether the option exists.
*/
public function has($option)
{
return array_key_exists($option, $this->options);
}
/**
* Removes the option with the given name.
*
* @param string $option The option name.
*
* @throws OptionDefinitionException If options have already been read.
* Once options are read, the container
* becomes immutable.
*/
public function remove($option)
{
if ($this->reading) {
throw new OptionDefinitionException('Options cannot be removed anymore once options have been read.');
}
unset($this->options[$option]);
unset($this->lazy[$option]);
unset($this->normalizers[$option]);
}
/**
* Removes all options.
*
* @throws OptionDefinitionException If options have already been read.
* Once options are read, the container
* becomes immutable.
*/
public function clear()
{
if ($this->reading) {
throw new OptionDefinitionException('Options cannot be cleared anymore once options have been read.');
}
$this->options = array();
$this->lazy = array();
$this->normalizers = array();
}
/**
* Returns the values of all options.
*
* Lazy options are evaluated at this point.
*
* @return array The option values.
*/
public function all()
{
$this->reading = true;
// Performance-wise this is slightly better than
// while (null !== $option = key($this->lazy))
foreach ($this->lazy as $option => $closures) {
// Double check, in case the option has already been resolved
// by cascade in the previous cycles
if (isset($this->lazy[$option])) {
$this->resolve($option);
}
}
foreach ($this->normalizers as $option => $normalizer) {
if (isset($this->normalizers[$option])) {
$this->normalize($option);
}
}
return $this->options;
}
/**
* Equivalent to {@link has()}.
*
* @param string $option The option name.
*
* @return bool Whether the option exists.
*
* @see \ArrayAccess::offsetExists()
*/
public function offsetExists($option)
{
return $this->has($option);
}
/**
* Equivalent to {@link get()}.
*
* @param string $option The option name.
*
* @return mixed The option value.
*
* @throws \OutOfBoundsException If the option does not exist.
* @throws OptionDefinitionException If a cyclic dependency is detected
* between two lazy options.
*
* @see \ArrayAccess::offsetGet()
*/
public function offsetGet($option)
{
return $this->get($option);
}
/**
* Equivalent to {@link set()}.
*
* @param string $option The name of the option.
* @param mixed $value The value of the option. May be a closure with a
* signature as defined in DefaultOptions::add().
*
* @throws OptionDefinitionException If options have already been read.
* Once options are read, the container
* becomes immutable.
*
* @see \ArrayAccess::offsetSet()
*/
public function offsetSet($option, $value)
{
$this->set($option, $value);
}
/**
* Equivalent to {@link remove()}.
*
* @param string $option The option name.
*
* @throws OptionDefinitionException If options have already been read.
* Once options are read, the container
* becomes immutable.
*
* @see \ArrayAccess::offsetUnset()
*/
public function offsetUnset($option)
{
$this->remove($option);
}
/**
* {@inheritdoc}
*/
public function current()
{
return $this->get($this->key());
}
/**
* {@inheritdoc}
*/
public function next()
{
next($this->options);
}
/**
* {@inheritdoc}
*/
public function key()
{
return key($this->options);
}
/**
* {@inheritdoc}
*/
public function valid()
{
return null !== $this->key();
}
/**
* {@inheritdoc}
*/
public function rewind()
{
reset($this->options);
}
/**
* {@inheritdoc}
*/
public function count()
{
return count($this->options);
}
/**
* Evaluates the given lazy option.
*
* The evaluated value is written into the options array. The closure for
* evaluating the option is discarded afterwards.
*
* @param string $option The option to evaluate.
*
* @throws OptionDefinitionException If the option has a cyclic dependency
* on another option.
*/
private function resolve($option)
{
// The code duplication with normalize() exists for performance
// reasons, in order to save a method call.
// Remember that this method is potentially called a couple of thousand
// times and needs to be as efficient as possible.
if (isset($this->lock[$option])) {
$conflicts = array();
foreach ($this->lock as $option => $locked) {
if ($locked) {
$conflicts[] = $option;
}
}
throw new OptionDefinitionException(sprintf('The options "%s" have a cyclic dependency.', implode('", "', $conflicts)));
}
$this->lock[$option] = true;
foreach ($this->lazy[$option] as $closure) {
$this->options[$option] = $closure($this, $this->options[$option]);
}
unset($this->lock[$option]);
// The option now isn't lazy anymore
unset($this->lazy[$option]);
}
/**
* Normalizes the given option.
*
* The evaluated value is written into the options array.
*
* @param string $option The option to normalizer.
*
* @throws OptionDefinitionException If the option has a cyclic dependency
* on another option.
*/
private function normalize($option)
{
// The code duplication with resolve() exists for performance
// reasons, in order to save a method call.
// Remember that this method is potentially called a couple of thousand
// times and needs to be as efficient as possible.
if (isset($this->lock[$option])) {
$conflicts = array();
foreach ($this->lock as $option => $locked) {
if ($locked) {
$conflicts[] = $option;
}
}
throw new OptionDefinitionException(sprintf('The options "%s" have a cyclic dependency.', implode('", "', $conflicts)));
}
/** @var \Closure $normalizer */
$normalizer = $this->normalizers[$option];
$this->lock[$option] = true;
$this->options[$option] = $normalizer($this, array_key_exists($option, $this->options) ? $this->options[$option] : null);
unset($this->lock[$option]);
// The option is now normalized
unset($this->normalizers[$option]);
}
}
|