Skip to content

Commit f25aa92

Browse files
committed
up: update obj box and some str util
1 parent 99803f7 commit f25aa92

File tree

3 files changed

+70
-9
lines changed

3 files changed

+70
-9
lines changed

Diff for: src/Obj/ObjectBox.php

+57-8
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ class ObjectBox implements ContainerInterface
5858
private bool $callInit = true;
5959

6060
/**
61-
* @var string
61+
* @var string Will call init method on object create. Default is 'initObj()'
6262
*/
63-
private string $initMethod = 'init';
63+
private string $initMethod = 'initObj';
6464

6565
/**
6666
* Created objects
@@ -70,9 +70,57 @@ class ObjectBox implements ContainerInterface
7070
private array $objects = [];
7171

7272
/**
73-
* Definitions for create objects
73+
* Definitions for create objects.
74+
*
75+
* For definition format:
76+
*
77+
* - Closure(ObjectBox): object
78+
* - Object and has __invoke()
79+
* - string: an function name
80+
* - array: callable array [class, method]
81+
* - array: config array for create object
82+
* - more, any type as config data.
83+
*
84+
* ```php
85+
* [
86+
* // array config of definition
87+
* 'myObj' => [
88+
* 'class' => MyObj::class,
89+
* '__opt' => [
90+
* 'callInit' => true, // call init method on object create. Default is true
91+
* 'objType' => ObjectBox::TYPE_SINGLETON, // default is TYPE_SINGLETON
92+
* 'argsForNew' => [
93+
* 'foo' => 'bar',
94+
* ],
95+
* ],
96+
* // props settings ...
97+
* 'propName' => value,
98+
* ],
99+
* // use creator func
100+
* 'myObj2' => [
101+
* '__creator' => function (ObjectBox $box) {
102+
* return new MyObj2();
103+
* },
104+
* '__opt' => [],
105+
* // props settings ...
106+
* 'propName' => value,
107+
* ],
108+
* // use [class, method] of definition
109+
* 'myObj3' => [MyObj3::class, 'createObj3'],
110+
* // function name of definition
111+
* 'myObj4' => 'my_create_func',
112+
* // Closure object
113+
* 'myObj5' => function (ObjectBox $box) {
114+
* return new MyObj5();
115+
* },
116+
* // callable object: has __invoke() method
117+
* 'myObj6' => new Obj6Factory(),
118+
* ]
119+
*
120+
* ```
74121
*
75122
* @var array
123+
* @see createObject()
76124
*/
77125
private array $definitions = [];
78126

@@ -108,10 +156,11 @@ public function get(string $id): mixed
108156

109157
if (is_object($obj)) {
110158
$callInit = $opt['callInit'] ?? $this->callInit;
159+
$initMth = $this->initMethod;
111160

112161
// has init method in the object, call it.
113-
if ($callInit && method_exists($obj, $this->initMethod)) {
114-
$obj->init();
162+
if ($callInit && $initMth && method_exists($obj, $initMth)) {
163+
$obj->$initMth();
115164
}
116165

117166
// storage it on type is TYPE_SINGLETON
@@ -130,7 +179,7 @@ public function get(string $id): mixed
130179
/**
131180
* Create object from definition.
132181
*
133-
* return options:
182+
* return [obj, options], options:
134183
*
135184
* ```php
136185
* [
@@ -188,7 +237,7 @@ protected function createObject(mixed $value): array
188237
}
189238
}
190239

191-
// as config data.
240+
// save as config data.
192241
if ($obj === null) {
193242
$obj = $value;
194243
}
@@ -209,7 +258,7 @@ protected function createObject(mixed $value): array
209258
*
210259
* ```php
211260
* [
212-
* 'class' => class-string,
261+
* 'class' => 'class-string',
213262
* // '__creator' => callable(ObjectBox): object, // can also use creator func.
214263
*
215264
* // option for create object.

Diff for: src/Str/Traits/StringTruncateHelperTrait.php

+12
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,18 @@ trait StringTruncateHelperTrait
4141
/// Truncate
4242
////////////////////////////////////////////////////////////////////////
4343

44+
/**
45+
* @param string $s
46+
* @param string $start
47+
* @param int|null $length
48+
*
49+
* @return string
50+
*/
51+
public static function afterStart(string $s, string $start, ?int $length = null): string
52+
{
53+
return substr($s, strlen($start), $length);
54+
}
55+
4456
/**
4557
* @param string $str
4658
* @param int $start

Diff for: src/Type.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* @package Toolkit\Stdlib
1919
* @see \gettype()
2020
*/
21-
final class Type
21+
class Type
2222
{
2323
// ------ basic types ------
2424

0 commit comments

Comments
 (0)