1 | <?php |
||
29 | class Builder |
||
30 | { |
||
31 | /** |
||
32 | * @var Provider[] Associative array of string names to `Provider` instances |
||
33 | */ |
||
34 | protected $providers = []; |
||
35 | /** |
||
36 | * @var string[] Array of eager `Provider` names |
||
37 | */ |
||
38 | protected $eager = []; |
||
39 | |||
40 | /** |
||
41 | * Adds a new Provider. |
||
42 | * |
||
43 | * This method exists only for completeness. You'd probably spend less |
||
44 | * effort if you call `->eager`, `->lazy`, or `->proto`. |
||
45 | * |
||
46 | * @param string $name The component name |
||
47 | * @param \Caridea\Container\Provider $provider The provider |
||
48 | * @return $this provides a fluent interface |
||
49 | */ |
||
50 | 1 | public function addProvider(string $name, Provider $provider): self |
|
55 | |||
56 | /** |
||
57 | * Adds a singleton component to be instantiated after the container is. |
||
58 | * |
||
59 | * ```php |
||
60 | * $builder->eager('foobar', 'Acme\Mail\Service', function($c) { |
||
61 | * return new \Acme\Mail\Service($c['dependency']); |
||
62 | * }); |
||
63 | * ``` |
||
64 | * |
||
65 | * @param string $name The component name |
||
66 | * @param string $type The class name of the component |
||
67 | * @param object $factory A `Closure` or class with an `__invoke` method to return the component |
||
68 | * @return $this provides a fluent interface |
||
69 | */ |
||
70 | 1 | public function eager(string $name, string $type, $factory): self |
|
76 | |||
77 | /** |
||
78 | * Adds a singleton component to be instantiated on demand. |
||
79 | * |
||
80 | * ```php |
||
81 | * $builder->lazy('foobar', 'Acme\Mail\Service', function($c) { |
||
82 | * return new \Acme\Mail\Service($c['dependency']); |
||
83 | * }); |
||
84 | * ``` |
||
85 | * |
||
86 | * @param string $name The component name |
||
87 | * @param string $type The class name of the component |
||
88 | * @param object $factory A `Closure` or class with an `__invoke` method to return the component |
||
89 | * @return $this provides a fluent interface |
||
90 | */ |
||
91 | 1 | public function lazy(string $name, string $type, $factory): self |
|
95 | |||
96 | /** |
||
97 | * Adds a component that provides a new instance each time it's instantiated. |
||
98 | * |
||
99 | * ```php |
||
100 | * $builder->lazy('objectStorage', 'SplObjectStorage', function($c) { |
||
101 | * return new \SplObjectStorage(); |
||
102 | * }); |
||
103 | * ``` |
||
104 | * |
||
105 | * @param string $name The component name |
||
106 | * @param string $type The class name of the component |
||
107 | * @param object $factory A `Closure` or class with an `__invoke` method to return the component |
||
108 | * @return $this provides a fluent interface |
||
109 | */ |
||
110 | 1 | public function proto(string $name, string $type, $factory): self |
|
114 | |||
115 | /** |
||
116 | * Builds a container using the settings called. |
||
117 | * |
||
118 | * Any *eager* components will be instantiated at this time. |
||
119 | * |
||
120 | * When this method is called, this builder is reset to its default state. |
||
121 | * |
||
122 | * @param Container $parent An optional parent container |
||
123 | * @return Objects The constructed `Objects` container |
||
124 | */ |
||
125 | 4 | public function build(Container $parent = null): Objects |
|
137 | } |
||
138 |