18
18
*
19
19
* An simple object containers implements
20
20
*
21
+ * NOTICE: require the `psr/container` package.
22
+ *
21
23
* @package Toolkit\Stdlib\Obj
22
24
*/
23
25
class ObjectBox implements ContainerInterface
24
26
{
25
27
/**
28
+ * @var self
29
+ */
30
+ private static $ global ;
31
+
32
+ /**
33
+ * @var bool
34
+ */
35
+ private $ callInit = true ;
36
+
37
+ /**
38
+ * @var string
39
+ */
40
+ private $ initMethod = 'init ' ;
41
+
42
+ /**
43
+ * Created objects
44
+ *
26
45
* @var array
27
46
*/
28
47
private $ objects = [];
29
48
30
49
/**
50
+ * Definitions for create objects
51
+ *
31
52
* @var array
32
53
*/
33
54
private $ definitions = [];
34
55
35
56
/**
36
- * @var self
57
+ * Class constructor.
58
+ *
59
+ * @param array $options
37
60
*/
38
- private static $ global ;
61
+ public function __construct (array $ options = [])
62
+ {
63
+ Obj::init ($ this , $ options );
64
+ }
39
65
40
66
/**
41
67
* @return static
@@ -56,12 +82,23 @@ public static function global(): self
56
82
*/
57
83
public function get (string $ id )
58
84
{
85
+ // has created.
59
86
if (isset ($ this ->objects [$ id ])) {
60
87
return $ this ->objects [$ id ];
61
88
}
62
89
90
+ // create from definition
63
91
if (isset ($ this ->definitions [$ id ])) {
64
- $ obj = $ this ->createObject ($ this ->definitions [$ id ]);
92
+ [$ obj , $ opt ] = $ this ->createObject ($ this ->definitions [$ id ]);
93
+
94
+ if (is_object ($ obj )) {
95
+ $ callInit = $ opt ['callInit ' ] ?? $ this ->callInit ;
96
+
97
+ // has init method in the object, call it.
98
+ if ($ callInit && method_exists ($ obj , $ this ->initMethod )) {
99
+ $ obj ->init ();
100
+ }
101
+ }
65
102
66
103
// storage
67
104
$ this ->objects [$ id ] = $ obj ;
@@ -72,32 +109,46 @@ public function get(string $id)
72
109
}
73
110
74
111
/**
75
- * @param mixed $value
112
+ * @param mixed $value The definition value.
76
113
*
77
- * @return mixed
114
+ * @return array
78
115
*/
79
- protected function createObject ($ value )
116
+ protected function createObject ($ value ): array
80
117
{
118
+ $ opt = [];
119
+
81
120
// Closure or has __invoke()
82
121
if (is_object ($ value ) && is_callable ($ value )) {
83
- return $ value ($ this );
122
+ return [ $ value ($ this ), $ opt ] ;
84
123
}
85
124
86
- // function
125
+ // function name
87
126
if (is_string ($ value ) && is_callable ($ value )) {
88
- return $ value ($ this );
127
+ return [ $ value ($ this ), $ opt ] ;
89
128
}
90
129
91
130
$ obj = null ;
131
+ // array config:
132
+ // [
133
+ // 'class' => string,
134
+ // // option for create.
135
+ // '__opt' => [
136
+ // 'callInit' => true,
137
+ // 'argsForNew' => [$arg0, $arg1],
138
+ // ],
139
+ // // props settings ...
140
+ // 'propName' => value,
141
+ // ]
92
142
if (is_array ($ value )) {
93
143
$ count = count ($ value );
94
144
145
+ // like [class, method]
95
146
if ($ count === 2 && isset ($ value [0 ], $ value [1 ]) && is_callable ($ value )) {
96
147
$ obj = $ value ($ this );
97
- } elseif (isset ($ value ['class ' ])) {
148
+ } elseif (isset ($ value ['class ' ])) { // full config
98
149
$ cls = $ value ['class ' ];
99
- $ opt = $ value ['__opts ' ] ?? [];
100
- unset($ value ['class ' ], $ value ['__opts ' ]);
150
+ $ opt = $ value ['__opt ' ] ?? [];
151
+ unset($ value ['class ' ], $ value ['__opt ' ]);
101
152
102
153
// set construct args, will expand for new object.
103
154
if ($ argsForNew = $ opt ['argsForNew ' ] ?? []) {
@@ -110,13 +161,6 @@ protected function createObject($value)
110
161
if ($ value ) {
111
162
Obj::init ($ obj , $ value );
112
163
}
113
-
114
- if ($ opt ) {
115
- $ init = $ opt ['init ' ] ?? true ;
116
- if ($ init && method_exists ($ obj , 'init ' )) {
117
- $ obj ->init ();
118
- }
119
- }
120
164
}
121
165
}
122
166
@@ -125,7 +169,7 @@ protected function createObject($value)
125
169
$ obj = $ value ;
126
170
}
127
171
128
- return $ obj ;
172
+ return [ $ obj, $ opt ] ;
129
173
}
130
174
131
175
/**
@@ -190,4 +234,38 @@ public function getDefinition(string $id)
190
234
{
191
235
return $ this ->definitions [$ id ] ?? null ;
192
236
}
237
+
238
+ /**
239
+ * @return bool
240
+ */
241
+ public function isCallInit (): bool
242
+ {
243
+ return $ this ->callInit ;
244
+ }
245
+
246
+ /**
247
+ * @param bool $callInit
248
+ */
249
+ public function setCallInit (bool $ callInit ): void
250
+ {
251
+ $ this ->callInit = $ callInit ;
252
+ }
253
+
254
+ /**
255
+ * @return string
256
+ */
257
+ public function getInitMethod (): string
258
+ {
259
+ return $ this ->initMethod ;
260
+ }
261
+
262
+ /**
263
+ * @param string $initMethod
264
+ */
265
+ public function setInitMethod (string $ initMethod ): void
266
+ {
267
+ if ($ initMethod ) {
268
+ $ this ->initMethod = $ initMethod ;
269
+ }
270
+ }
193
271
}
0 commit comments