8000 [OptionsResolver] Support callable as lazy option default by ogizanagi · Pull Request #31527 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[OptionsResolver] Support callable as lazy option default #31527

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[OptionsResolver] Support callable as lazy option default
  • Loading branch information
ogizanagi committed May 17, 2019
commit 7562d7ab9cd04d1205706897c12ba84c5674243b
5 changes: 5 additions & 0 deletions src/Symfony/Component/OptionsResolver/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

4.4.0
-----

* added support for callables as lazy options

4.3.0
-----

Expand Down
9 changes: 5 additions & 4 deletions src/Symfony/Component/OptionsResolver/OptionsResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,9 @@ public function setDefault($option, $value)

// 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);
if ($value instanceof \Closure || \is_callable($value)) {
$closure = $value instanceof \Closure ? $value : \Closure::fromCallable($value);
$reflClosure = new \ReflectionFunction($closure);
$params = $reflClosure->getParameters();

if (isset($params[0]) && null !== ($class = $params[0]->getClass()) && Options::class === $class->name) {
Expand All @@ -190,7 +191,7 @@ public function setDefault($option, $value)
}

// Store closure for later evaluation
$this->lazy[$option][] = $value;
$this->lazy[$option][] = $closure;
$this->defined[$option] = true;

// Make sure the option is processed and is not nested anymore
Expa 8000 nd All @@ -201,7 +202,7 @@ public function setDefault($option, $value)

if (isset($params[0]) && null !== ($class = $params[0]->getClass()) && self::class === $class->name && (!isset($params[1]) || (null !== ($class = $params[1]->getClass()) && Options::class === $class->name))) {
// Store closure for later evaluation
$this->nested[$option][] = $value;
$this->nested[$option][] = $closure;
$this->defaults[$option] = [];
$this->defined[$option] = true;

Expand Down
$this->assertEquals(['foo' => 'lazy'], $this->resolver->resolve());
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,34 @@ public function testSetLazyClosure()
$this->assertEquals(['foo' => 'lazy'], $this->resolver->resolve());
}

public function testSetLazyFromCallable()
{
$object = new class() {
public function resolve(Options $options): string
{
return 'lazy';
}
};

$this->resolver->setDefault('foo', [$object, 'resolve']);

}

public function testSetLazyFromCallableObject()
{
$object = new class() {
public function __invoke(Options $options): string
{
return 'lazy';
}
};

$this->resolver->setDefault('foo', $object);

$this->assertEquals(['foo' => 'lazy'], $this->resolver->resolve());
}

public function testClosureWithoutTypeHintNotInvoked()
{
$closure = function ($options) {
Expand All @@ -143,6 +171,34 @@ public function testClosureWithoutParametersNotInvoked()
$this->assertSame(['foo' => $closure], $this->resolver->resolve());
}

public function testCallableWithoutTypeHintNotInvoked()
{
$object = new class() {
public function __invoke($options): void
{
Assert::fail('Should not be called');
}
};

$this->resolver->setDefault('foo', $object);

$this->assertSame(['foo' => $object], $this->resolver->resolve());
}

public function testCallableWithoutParametersNotInvoked()
{
$object = new class() {
public function __invoke(): void
{
Assert::fail('Should not be called');
}
};

$this->resolver->setDefault('foo', $object);

$this->assertSame(['foo' => $object], $this->resolver->resolve());
}

public function testAccessPreviousDefaultValue()
{
// defined by superclass
Expand Down
0