CallableBuilder.php
2.74 KB
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
<?php
/**
* Callable-based builder.
*
* @package StellarWP\Learndash\lucatume\DI52\Builders
*
* @license GPL-3.0
* Modified by learndash on 20-September-2023 using Strauss.
* @see https://github.com/BrianHenryIE/strauss
*/
namespace StellarWP\Learndash\lucatume\DI52\Builders;
use StellarWP\Learndash\lucatume\DI52\Container;
/**
* Class CallableBuilder
*
* @package StellarWP\Learndash\lucatume\DI52\Builders
*/
class CallableBuilder implements BuilderInterface, ReinitializableBuilderInterface
{
/**
* An instance of the DI Container.
*
* @var Container The
*/
protected $container;
/**
* The callable this builder will use.
*
* @var callable
*/
protected $callable;
/**
* An array of method that will be called on the built object.
*
* @var array<string>|null
*/
protected $afterBuildMethods;
/**
* An array of arguments that will be passed as input to the callable method.
*
* @var array<mixed>
*/
protected $buildArgs;
/**
* CallableBuilder constructor.
*
* @param Container $container An instance of the DI Container.
* @param callable $callable The builder callable.
* @param array<string>|null $afterBuildMethods A set of methods to call on the built instance.
* @param mixed ...$buildArgs A set of optional arguments for the callable method.
*/
public function __construct(
Container $container,
callable $callable,
array $afterBuildMethods = null,
...$buildArgs
) {
$this->container = $container;
$this->callable = $callable;
$this->afterBuildMethods = $afterBuildMethods ?: [];
$this->buildArgs = $buildArgs;
}
/**
* Calls the callable for the builder and returns its value.
*
* @return mixed The built implementation.
*/
public function build()
{
$built = call_user_func($this->callable, ...$this->buildArgs);
foreach ((array)$this->afterBuildMethods as $afterBuildMethod) {
$built->{$afterBuildMethod}();
}
return $built;
}
/**
* Reinitialize the builder setting the after build methods and build args.
*
* @param array<string>|null $afterBuildMethods A set of methods to call on the object after it's built.
* @param mixed ...$buildArgs A set of build arguments that will be passed to the constructor.
*
* @return void This method does not return any value.
*/
public function reinit(array $afterBuildMethods = null, ...$buildArgs)
{
$this->afterBuildMethods = $afterBuildMethods ?: [];
$this->buildArgs = $buildArgs;
}
}