1 | <?php |
||
23 | trait ContainerTrait |
||
24 | { |
||
25 | use ResolveTrait; |
||
26 | use FactoryTrait; |
||
27 | use ConfigAwareTrait; |
||
28 | |||
29 | /** |
||
30 | * service objects stored by its service id |
||
31 | * |
||
32 | * @var object[] |
||
33 | */ |
||
34 | protected $objects = []; |
||
35 | |||
36 | /** |
||
37 | * DI definition prefix in $config |
||
38 | * |
||
39 | * @var string |
||
40 | */ |
||
41 | protected $prefix = 'di.'; |
||
42 | |||
43 | /** |
||
44 | * Get the instance by SERVICE id, create it if not yet |
||
45 | * |
||
46 | * @param string $id defined service id |
||
47 | * @return object |
||
48 | * @throws UnresolvedClassException if dependencies unresolved |
||
49 | * @throws LogicException if 'di.before' or 'di.after' definitions go wrong |
||
50 | */ |
||
51 | protected function getInstance(string $id): object |
||
76 | |||
77 | /** |
||
78 | * initiate an object by its definition |
||
79 | * |
||
80 | * @param array $definition |
||
81 | * @return object |
||
82 | * @throws UnresolvedClassException if dependencies unresolved |
||
83 | * @throws LogicException if 'di.before' or 'di.after' definitions go wrong |
||
84 | */ |
||
85 | public function newInstance(array $definition): object |
||
98 | |||
99 | /** |
||
100 | * get the raw service id by adding prefix & stripping the scope '@XXX' off |
||
101 | * |
||
102 | * @param string $id |
||
103 | * @return string |
||
104 | */ |
||
105 | protected function getRawId(string $id): string |
||
113 | |||
114 | /** |
||
115 | * execute 'di.before' or 'di.after' methods for newly created object |
||
116 | * |
||
117 | * @param object|array $object newly created object or object definition |
||
118 | * @param string $stage 'before' | 'after' |
||
119 | * @return void |
||
120 | * @throws LogicException if 'di.before' or 'di.after' definitions go wrong |
||
121 | */ |
||
122 | protected function executeMethods($object, string $stage): void |
||
133 | |||
134 | /** |
||
135 | * A service defined in the definitions ? |
||
136 | * |
||
137 | * @param string $id |
||
138 | * @return bool |
||
139 | */ |
||
140 | protected function hasDefinition(string $id): bool |
||
144 | |||
145 | /** |
||
146 | * for reserving 'container' or 'config' |
||
147 | * |
||
148 | * @param string $id |
||
149 | * @param object $object |
||
150 | * @return $this |
||
151 | */ |
||
152 | protected function reserveObject(string $id, object $object) |
||
158 | |||
159 | /** |
||
160 | * Find the service definition and fix any non-standard stuff |
||
161 | * |
||
162 | * @param string $id |
||
163 | * @return array |
||
164 | */ |
||
165 | protected function getServiceDefinition(string $id): array |
||
170 | } |