2012-04-05 09:16:23 +01:00
|
|
|
<?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.
|
|
|
|
*/
|
|
|
|
|
2012-05-10 15:37:03 +01:00
|
|
|
namespace Symfony\Component\OptionsResolver;
|
2012-04-05 09:16:23 +01:00
|
|
|
|
|
|
|
use ArrayAccess;
|
|
|
|
use Iterator;
|
|
|
|
use OutOfBoundsException;
|
2012-05-10 15:37:03 +01:00
|
|
|
use Symfony\Component\OptionsResolver\Exception\OptionDefinitionException;
|
2012-04-05 09:16:23 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Container for resolving inter-dependent options.
|
|
|
|
*
|
|
|
|
* @author Bernhard Schussek <bschussek@gmail.com>
|
|
|
|
*/
|
|
|
|
class Options implements ArrayAccess, Iterator
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* A list of option values and LazyOption instances.
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $options = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A list of Boolean locks for each LazyOption.
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $lock = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether the options have already been resolved.
|
|
|
|
*
|
|
|
|
* Once resolved, no new options can be added or changed anymore.
|
|
|
|
*
|
|
|
|
* @var Boolean
|
|
|
|
*/
|
|
|
|
private $resolved = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether the given option exists.
|
|
|
|
*
|
|
|
|
* @param string $option The option name.
|
|
|
|
*
|
|
|
|
* @return Boolean Whether the option exists.
|
|
|
|
*
|
|
|
|
* @see ArrayAccess::offsetExists()
|
|
|
|
*/
|
|
|
|
public function offsetExists($option)
|
|
|
|
{
|
2012-05-01 13:46:26 +01:00
|
|
|
return isset($this->options[$option]);
|
|
|
|
}
|
2012-04-05 09:16:23 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the value of the given option.
|
|
|
|
*
|
|
|
|
* After reading an option for the first time, this object becomes
|
|
|
|
*
|
|
|
|
* @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()
|
2012-05-01 13:46:26 +01:00
|
|
|
*/
|
2012-04-05 09:16:23 +01:00
|
|
|
public function offsetGet($option)
|
|
|
|
{
|
|
|
|
if (!array_key_exists($option, $this->options)) {
|
|
|
|
throw new OutOfBoundsException('The option "' . $option . '" does not exist');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->resolved = true;
|
|
|
|
|
|
|
|
if ($this->options[$option] instanceof LazyOption) {
|
|
|
|
if ($this->lock[$option]) {
|
|
|
|
$conflicts = array_keys(array_filter($this->lock, function ($locked) {
|
|
|
|
return $locked;
|
|
|
|
}));
|
|
|
|
|
|
|
|
throw new OptionDefinitionException('The options "' . implode('", "', $conflicts) . '" have a cyclic dependency');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->lock[$option] = true;
|
|
|
|
$this->options[$option] = $this->options[$option]->evaluate($this);
|
|
|
|
$this->lock[$option] = false;
|
|
|
|
}
|
|
|
|
|
2012-05-01 13:46:26 +01:00
|
|
|
return $this->options[$option];
|
|
|
|
}
|
2012-04-05 09:16:23 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the value of a given option.
|
|
|
|
*
|
|
|
|
* @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 DefaultOptions::add()
|
|
|
|
* @see ArrayAccess::offsetSet()
|
2012-05-01 13:46:26 +01:00
|
|
|
*/
|
2012-04-05 09:16:23 +01:00
|
|
|
public function offsetSet($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->resolved) {
|
|
|
|
throw new OptionDefinitionException('Options cannot be set after reading options');
|
|
|
|
}
|
|
|
|
|
|
|
|
$newValue = $value;
|
|
|
|
|
|
|
|
// If an option is a closure that should be evaluated lazily, store it
|
|
|
|
// inside a LazyOption instance.
|
|
|
|
if ($newValue instanceof \Closure) {
|
|
|
|
$reflClosure = new \ReflectionFunction($newValue);
|
|
|
|
$params = $reflClosure->getParameters();
|
|
|
|
$isLazyOption = count($params) >= 1 && null !== $params[0]->getClass() && __CLASS__ === $params[0]->getClass()->getName();
|
|
|
|
|
|
|
|
if ($isLazyOption) {
|
|
|
|
$currentValue = isset($this->options[$option]) ? $this->options[$option] : null;
|
|
|
|
$newValue = new LazyOption($newValue, $currentValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store locks for lazy options to detect cyclic dependencies
|
|
|
|
$this->lock[$option] = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->options[$option] = $newValue;
|
2012-05-01 13:46:26 +01:00
|
|
|
}
|
2012-04-05 09:16:23 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes an 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.
|
|
|
|
*
|
|
|
|
* @see ArrayAccess::offsetUnset()
|
2012-05-01 13:46:26 +01:00
|
|
|
*/
|
2012-04-05 09:16:23 +01:00
|
|
|
public function offsetUnset($option)
|
|
|
|
{
|
|
|
|
if ($this->resolved) {
|
|
|
|
throw new OptionDefinitionException('Options cannot be unset after reading options');
|
|
|
|
}
|
|
|
|
|
|
|
|
unset($this->options[$option]);
|
2012-05-01 13:46:26 +01:00
|
|
|
unset($this->lock[$option]);
|
2012-04-05 09:16:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-05-11 09:31:19 +01:00
|
|
|
* {@inheritdoc}
|
2012-05-01 13:46:26 +01:00
|
|
|
*/
|
2012-04-05 09:16:23 +01:00
|
|
|
public function current()
|
|
|
|
{
|
2012-05-01 13:46:26 +01:00
|
|
|
return $this->offsetGet($this->key());
|
|
|
|
}
|
2012-04-05 09:16:23 +01:00
|
|
|
|
|
|
|
/**
|
2012-05-11 09:31:19 +01:00
|
|
|
* {@inheritdoc}
|
2012-05-01 13:46:26 +01:00
|
|
|
*/
|
2012-04-05 09:16:23 +01:00
|
|
|
public function next()
|
|
|
|
{
|
2012-05-01 13:46:26 +01:00
|
|
|
next($this->options);
|
|
|
|
}
|
2012-04-05 09:16:23 +01:00
|
|
|
|
|
|
|
/**
|
2012-05-11 09:31:19 +01:00
|
|
|
* {@inheritdoc}
|
2012-05-01 13:46:26 +01:00
|
|
|
*/
|
2012-04-05 09:16:23 +01:00
|
|
|
public function key()
|
|
|
|
{
|
2012-05-01 13:46:26 +01:00
|
|
|
return key($this->options);
|
|
|
|
}
|
2012-04-05 09:16:23 +01:00
|
|
|
|
|
|
|
/**
|
2012-05-11 09:31:19 +01:00
|
|
|
* {@inheritdoc}
|
2012-05-01 13:46:26 +01:00
|
|
|
*/
|
2012-04-05 09:16:23 +01:00
|
|
|
public function valid()
|
|
|
|
{
|
2012-05-01 13:46:26 +01:00
|
|
|
return null !== $this->key();
|
|
|
|
}
|
2012-04-05 09:16:23 +01:00
|
|
|
|
|
|
|
/**
|
2012-05-11 09:31:19 +01:00
|
|
|
* {@inheritdoc}
|
2012-05-01 13:46:26 +01:00
|
|
|
*/
|
2012-04-05 09:16:23 +01:00
|
|
|
public function rewind()
|
|
|
|
{
|
2012-05-01 13:46:26 +01:00
|
|
|
reset($this->options);
|
2012-04-05 09:16:23 +01:00
|
|
|
}
|
2012-05-01 13:46:26 +01:00
|
|
|
}
|