-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
LazyOption.php
73 lines (65 loc) · 1.94 KB
/
LazyOption.php
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
<?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;
/**
* An option that is evaluated lazily using a closure.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class LazyOption
{
/**
* The underlying closure.
* @var \Closure
*/
private $closure;
/**
* The previous default value of the option.
* @var mixed
*/
private $previousValue;
/**
* Creates a new lazy option.
*
* @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
* evaluated.
*
* @see evaluate()
*/
public function __construct(\Closure $closure, $previousValue = null)
{
$this->closure = $closure;
$this->previousValue = $previousValue;
}
/**
* 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
* argument.
*
* @param Options $options The container with all concrete options.
*
* @return mixed The result of the closure.
*/
public function evaluate(Options $options)
{
$previousValue = $this->previousValue;
$closure = $this->closure;
if ($previousValue instanceof self) {
$previousValue = $this->previousValue->evaluate($options);
}
// Performs a bit better than __invoke() and call_user_func()
return $closure($options, $previousValue);
}
}