1818 *
1919 * An simple object containers implements
2020 *
21+ * NOTICE: require the `psr/container` package.
22+ *
2123 * @package Toolkit\Stdlib\Obj
2224 */
2325class ObjectBox implements ContainerInterface
2426{
2527 /**
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+ *
2645 * @var array
2746 */
2847 private $ objects = [];
2948
3049 /**
50+ * Definitions for create objects
51+ *
3152 * @var array
3253 */
3354 private $ definitions = [];
3455
3556 /**
36- * @var self
57+ * Class constructor.
58+ *
59+ * @param array $options
3760 */
38- private static $ global ;
61+ public function __construct (array $ options = [])
62+ {
63+ Obj::init ($ this , $ options );
64+ }
3965
4066 /**
4167 * @return static
@@ -56,12 +82,23 @@ public static function global(): self
5682 */
5783 public function get (string $ id )
5884 {
85+ // has created.
5986 if (isset ($ this ->objects [$ id ])) {
6087 return $ this ->objects [$ id ];
6188 }
6289
90+ // create from definition
6391 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+ }
65102
66103 // storage
67104 $ this ->objects [$ id ] = $ obj ;
@@ -72,32 +109,46 @@ public function get(string $id)
72109 }
73110
74111 /**
75- * @param mixed $value
112+ * @param mixed $value The definition value.
76113 *
77- * @return mixed
114+ * @return array
78115 */
79- protected function createObject ($ value )
116+ protected function createObject ($ value ): array
80117 {
118+ $ opt = [];
119+
81120 // Closure or has __invoke()
82121 if (is_object ($ value ) && is_callable ($ value )) {
83- return $ value ($ this );
122+ return [ $ value ($ this ), $ opt ] ;
84123 }
85124
86- // function
125+ // function name
87126 if (is_string ($ value ) && is_callable ($ value )) {
88- return $ value ($ this );
127+ return [ $ value ($ this ), $ opt ] ;
89128 }
90129
91130 $ 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+ // ]
92142 if (is_array ($ value )) {
93143 $ count = count ($ value );
94144
145+ // like [class, method]
95146 if ($ count === 2 && isset ($ value [0 ], $ value [1 ]) && is_callable ($ value )) {
96147 $ obj = $ value ($ this );
97- } elseif (isset ($ value ['class ' ])) {
148+ } elseif (isset ($ value ['class ' ])) { // full config
98149 $ cls = $ value ['class ' ];
99- $ opt = $ value ['__opts ' ] ?? [];
100- unset($ value ['class ' ], $ value ['__opts ' ]);
150+ $ opt = $ value ['__opt ' ] ?? [];
151+ unset($ value ['class ' ], $ value ['__opt ' ]);
101152
102153 // set construct args, will expand for new object.
103154 if ($ argsForNew = $ opt ['argsForNew ' ] ?? []) {
@@ -110,13 +161,6 @@ protected function createObject($value)
110161 if ($ value ) {
111162 Obj::init ($ obj , $ value );
112163 }
113-
114- if ($ opt ) {
115- $ init = $ opt ['init ' ] ?? true ;
116- if ($ init && method_exists ($ obj , 'init ' )) {
117- $ obj ->init ();
118- }
119- }
120164 }
121165 }
122166
@@ -125,7 +169,7 @@ protected function createObject($value)
125169 $ obj = $ value ;
126170 }
127171
128- return $ obj ;
172+ return [ $ obj, $ opt ] ;
129173 }
130174
131175 /**
@@ -190,4 +234,38 @@ public function getDefinition(string $id)
190234 {
191235 return $ this ->definitions [$ id ] ?? null ;
192236 }
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+ }
193271}
0 commit comments