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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
<?php /** * Builds and sets up the correct builder for a binding. * * @package lucatume\DI52 */
namespace lucatume\DI52\Builders;
use Closure; use lucatume\DI52\Container; use lucatume\DI52\NotFoundException;
/** * Class Factory * * @package lucatume\DI52\Builders */ class Factory { /** * A reference to the resolver that should be used to resolve the implementations. * * @var Resolver */ protected $resolver; /** * A reference to the DI container builder will be built for. * * @var Container */ protected $container;
/** * BuilderFactory constructor. * @param Container $container A reference to the DI container the builder is working for. * @param Resolver $resolver A reference to the resolver builders will use to resolve to implementations. */ public function __construct(Container $container, Resolver $resolver) { $this->container = $container; $this->resolver = $resolver; }
/** * Returns the correct builder for a value. * * @param string|class-string|mixed $id The string id to provide a builder for, or a value. * @param mixed $implementation The implementation to build the builder for. * @param string[]|null $afterBuildMethods A list of methods that should be called on the built * instance after it's been built. * @param mixed ...$buildArgs A set of arguments to pass that should be used to build * the instance, if any. * * @return BuilderInterface A builder instance. * * @throws NotFoundException If a builder cannot find its implementation target. */ public function getBuilder($id, $implementation = null, array $afterBuildMethods = null, ...$buildArgs) { if ($implementation === null) { $implementation = $id; } if (is_string($implementation) && is_string($id)) { if (class_exists($implementation)) { return new ClassBuilder($id, $this->resolver, $implementation, $afterBuildMethods, ...$buildArgs); } return new ValueBuilder($implementation); }
if ($implementation instanceof BuilderInterface) { return $implementation; }
if ($implementation instanceof Closure) { return new ClosureBuilder($this->container, $implementation); }
if (is_callable($implementation)) { return new CallableBuilder($this->container, $implementation); }
return new ValueBuilder($implementation); }
/** * Sets the container the builder should use. * * @since TBD * * @param Container $container The container to bind. * * @return void */ public function setContainer(Container $container) { $this->container = $container; }
/** * Sets the resolver the container should use. * * @since TBD * * @param Resolver $resolver The resolver the container should use. * * @return void */ public function setResolver(Resolver $resolver) { $this->resolver = $resolver; } }
|