merged branch bschussek/optionsresolver (PR #4945)
Commits
-------
610c602
[OptionsResolver] Slightly tweaked the performance of the Options class
Discussion
----------
[OptionsResolver] Slightly tweaked the performance of the Options class
Bug fix: no
Feature addition: no
Backwards compatibility break: no
Symfony2 tests pass: yes
Fixes the following tickets: -
Todo: -
This commit is contained in:
commit
242a388ea4
@ -33,7 +33,7 @@ class LazyOption
|
||||
/**
|
||||
* Creates a new lazy option.
|
||||
*
|
||||
* @param Closure $closure The closure used for initializing the
|
||||
* @param \Closure $closure The closure used for initializing the
|
||||
* option value.
|
||||
* @param mixed $previousValue The previous value of the option. This
|
||||
* value is passed to the closure when it is
|
||||
@ -41,14 +41,14 @@ class LazyOption
|
||||
*
|
||||
* @see evaluate()
|
||||
*/
|
||||
public function __construct(\Closure $closure, $previousValue)
|
||||
public function __construct(\Closure $closure, $previousValue = null)
|
||||
{
|
||||
$this->closure = $closure;
|
||||
$this->previousValue = $previousValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Evaluates the underyling closure and returns its result.
|
||||
* Evaluates the underlying closure and returns its result.
|
||||
*
|
||||
* The given Options instance is passed to the closure as first argument.
|
||||
* The previous default value set in the constructor is passed as second
|
||||
@ -60,10 +60,14 @@ class LazyOption
|
||||
*/
|
||||
public function evaluate(Options $options)
|
||||
{
|
||||
if ($this->previousValue instanceof self) {
|
||||
$this->previousValue = $this->previousValue->evaluate($options);
|
||||
$previousValue = $this->previousValue;
|
||||
$closure = $this->closure;
|
||||
|
||||
if ($previousValue instanceof self) {
|
||||
$previousValue = $this->previousValue->evaluate($options);
|
||||
}
|
||||
|
||||
return $this->closure->__invoke($options, $this->previousValue);
|
||||
// Performs a bit better than __invoke() and call_user_func()
|
||||
return $closure($options, $previousValue);
|
||||
}
|
||||
}
|
||||
|
@ -139,13 +139,13 @@ class Options implements \ArrayAccess, \Iterator, \Countable
|
||||
throw new OptionDefinitionException('Options cannot be overloaded anymore once options have been read.');
|
||||
}
|
||||
|
||||
// Reset lazy flag and locks by default
|
||||
unset($this->lock[$option]);
|
||||
unset($this->lazy[$option]);
|
||||
|
||||
// If an option is a closure that should be evaluated lazily, store it
|
||||
// inside a LazyOption instance.
|
||||
if (self::isEvaluatedLazily($value)) {
|
||||
if ($value instanceof \Closure) {
|
||||
$reflClosure = new \ReflectionFunction($value);
|
||||
$params = $reflClosure->getParameters();
|
||||
|
||||
if (isset($params[0]) && null !== ($class = $params[0]->getClass()) && __CLASS__ === $class->name) {
|
||||
$currentValue = isset($this->options[$option]) ? $this->options[$option] : null;
|
||||
$value = new LazyOption($value, $currentValue);
|
||||
|
||||
@ -154,7 +154,16 @@ class Options implements \ArrayAccess, \Iterator, \Countable
|
||||
|
||||
// Store which options are lazy for more efficient resolving
|
||||
$this->lazy[$option] = true;
|
||||
|
||||
$this->options[$option] = $value;
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Reset lazy flag and locks by default
|
||||
unset($this->lock[$option]);
|
||||
unset($this->lazy[$option]);
|
||||
|
||||
$this->options[$option] = $value;
|
||||
}
|
||||
@ -251,9 +260,15 @@ class Options implements \ArrayAccess, \Iterator, \Countable
|
||||
// Create a copy because resolve() modifies the array
|
||||
$lazy = $this->lazy;
|
||||
|
||||
// Performance-wise this is slightly better than
|
||||
// while (null !== $option = key($this->lazy))
|
||||
foreach ($lazy as $option => $isLazy) {
|
||||
// When resolve() is called, potentially multiple lazy options
|
||||
// are evaluated, so check again if the option is still lazy.
|
||||
if (isset($this->lazy[$option])) {
|
||||
$this->resolve($option);
|
||||
}
|
||||
}
|
||||
|
||||
return $this->options;
|
||||
}
|
||||
@ -329,7 +344,7 @@ class Options implements \ArrayAccess, \Iterator, \Countable
|
||||
*/
|
||||
public function current()
|
||||
{
|
||||
return $this->offsetGet($this->key());
|
||||
return $this->get($this->key());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -385,14 +400,16 @@ class Options implements \ArrayAccess, \Iterator, \Countable
|
||||
*/
|
||||
private function resolve($option)
|
||||
{
|
||||
if ($this->options[$option] instanceof LazyOption) {
|
||||
if ($this->lock[$option]) {
|
||||
$conflicts = array_keys(array_filter($this->lock, function ($locked) {
|
||||
return $locked;
|
||||
}));
|
||||
$conflicts = array();
|
||||
|
||||
throw new OptionDefinitionException('The options "' . implode('", "',
|
||||
$conflicts) . '" have a cyclic dependency.');
|
||||
foreach ($this->lock as $option => $locked) {
|
||||
if ($locked) {
|
||||
$conflicts[] = $option;
|
||||
}
|
||||
}
|
||||
|
||||
throw new OptionDefinitionException('The options "' . implode('", "', $conflicts) . '" have a cyclic dependency.');
|
||||
}
|
||||
|
||||
$this->lock[$option] = true;
|
||||
@ -402,35 +419,4 @@ class Options implements \ArrayAccess, \Iterator, \Countable
|
||||
// The option now isn't lazy anymore
|
||||
unset($this->lazy[$option]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the option is a lazy option closure.
|
||||
*
|
||||
* Lazy option closure expect an {@link Options} instance
|
||||
* in their first parameter.
|
||||
*
|
||||
* @param mixed $value The option value to test.
|
||||
*
|
||||
* @return Boolean Whether it is a lazy option closure.
|
||||
*/
|
||||
private static function isEvaluatedLazily($value)
|
||||
{
|
||||
if (!$value instanceof \Closure) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$reflClosure = new \ReflectionFunction($value);
|
||||
$params = $reflClosure->getParameters();
|
||||
|
||||
if (count($params) < 1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (null === $params[0]->getClass()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return __CLASS__ === $params[0]->getClass()->name;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,44 @@
|
||||
<?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\Tests;
|
||||
|
||||
use Symfony\Component\OptionsResolver\LazyOption;
|
||||
use Symfony\Component\OptionsResolver\Options;
|
||||
|
||||
/**
|
||||
* @author Bernhard Schussek <bschussek@gmail.com>
|
||||
*/
|
||||
class LazyOptionTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testDontCacheEvaluatedPreviousValue()
|
||||
{
|
||||
$previousValue = new LazyOption(function (Options $options) {
|
||||
return $options['foo'];
|
||||
});
|
||||
|
||||
$lazyOption = new LazyOption(function (Options $options, $previousValue) {
|
||||
return $previousValue;
|
||||
}, $previousValue);
|
||||
|
||||
// If provided with two different option sets, two different results
|
||||
// should be returned
|
||||
$options1 = new Options();
|
||||
$options1['foo'] = 'bar';
|
||||
|
||||
$this->assertSame('bar', $lazyOption->evaluate($options1));
|
||||
|
||||
$options2 = new Options();
|
||||
$options2['foo'] = 'boo';
|
||||
|
||||
$this->assertSame('boo', $lazyOption->evaluate($options2));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user