Skip to content

Commit 8f22291

Browse files
committedAug 4, 2021
update some logic for obj box
Signed-off-by: inhere <[email protected]>
1 parent ffc9a83 commit 8f22291

File tree

4 files changed

+215
-4
lines changed

4 files changed

+215
-4
lines changed
 

Diff for: ‎src/OS.php

+9
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use function defined;
1313
use function explode;
1414
use function function_exists;
15+
use function getcwd;
1516
use function getenv;
1617
use function getmyuid;
1718
use function in_array;
@@ -126,6 +127,14 @@ public static function getCurrentUser(): array
126127
return posix_getpwuid(posix_getuid());
127128
}
128129

130+
/**
131+
* @return string
132+
*/
133+
public static function getWorkDir(): string
134+
{
135+
return (string)getcwd();
136+
}
137+
129138
/**
130139
* @return string
131140
*/

Diff for: ‎src/Obj/Exception/ContainerException.php

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Toolkit\Stdlib\Obj\Exception;
4+
5+
use Psr\Container\ContainerExceptionInterface;
6+
use RuntimeException;
7+
8+
/**
9+
* Class ContainerException
10+
*
11+
* @package Toolkit\Stdlib\Obj\Exception
12+
*/
13+
class ContainerException extends RuntimeException implements ContainerExceptionInterface
14+
{
15+
16+
}

Diff for: ‎src/Obj/Exception/NotFoundException.php

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Toolkit\Stdlib\Obj\Exception;
4+
5+
use Psr\Container\NotFoundExceptionInterface;
6+
use RuntimeException;
7+
8+
/**
9+
* Class NotFoundException
10+
*
11+
* @package Toolkit\Stdlib\Obj\Exception
12+
*/
13+
class NotFoundException extends RuntimeException implements NotFoundExceptionInterface
14+
{
15+
16+
}

Diff for: ‎src/Obj/ObjectBox.php

+174-4
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,192 @@
22

33
namespace Toolkit\Stdlib\Obj;
44

5+
use Psr\Container\ContainerInterface;
6+
use Toolkit\Stdlib\Obj;
7+
use Toolkit\Stdlib\Obj\Exception\ContainerException;
8+
use Toolkit\Stdlib\Obj\Exception\NotFoundException;
9+
use function count;
10+
use function is_array;
11+
use function is_callable;
12+
use function is_object;
13+
use function is_string;
14+
use function method_exists;
15+
516
/**
617
* Class ObjectBox
718
*
8-
* An simple object containers
19+
* An simple object containers implements
920
*
1021
* @package Toolkit\Stdlib\Obj
1122
*/
12-
class ObjectBox
23+
class ObjectBox implements ContainerInterface
1324
{
1425
/**
1526
* @var array
1627
*/
17-
private $config = [];
28+
private $objects = [];
1829

1930
/**
2031
* @var array
2132
*/
22-
private $objects = [];
33+
private $definitions = [];
34+
35+
/**
36+
* @var self
37+
*/
38+
private static $global;
39+
40+
/**
41+
* @return static
42+
*/
43+
public static function global(): self
44+
{
45+
if (!self::$global) {
46+
self::$global = new self();
47+
}
48+
49+
return self::$global;
50+
}
51+
52+
/**
53+
* @param string $id
54+
*
55+
* @return mixed|object
56+
*/
57+
public function get(string $id)
58+
{
59+
if (isset($this->objects[$id])) {
60+
return $this->objects[$id];
61+
}
62+
63+
if (isset($this->definitions[$id])) {
64+
$obj = $this->createObject($this->definitions[$id]);
65+
66+
// storage
67+
$this->objects[$id] = $obj;
68+
return $obj;
69+
}
70+
71+
throw new NotFoundException('box: get undefined object - ' . $id, 404);
72+
}
73+
74+
/**
75+
* @param mixed $value
76+
*
77+
* @return mixed
78+
*/
79+
protected function createObject($value)
80+
{
81+
// Closure or has __invoke()
82+
if (is_object($value) && is_callable($value)) {
83+
return $value($this);
84+
}
85+
86+
// function
87+
if (is_string($value) && is_callable($value)) {
88+
return $value($this);
89+
}
90+
91+
$obj = null;
92+
if (is_array($value)) {
93+
$count = count($value);
94+
95+
if ($count === 2 && isset($value[0], $value[1]) && is_callable($value)) {
96+
$obj = $value($this);
97+
} elseif (isset($value['class'])) {
98+
$cls = $value['class'];
99+
$opt = $value['__opts'] ?? [];
100+
unset($value['class'], $value['__opts']);
101+
102+
// set construct args, will expand for new object.
103+
if ($argsForNew = $opt['argsForNew'] ?? []) {
104+
$obj = new $cls(...$argsForNew);
105+
} else {
106+
$obj = new $cls();
107+
}
108+
109+
// init props
110+
if ($value) {
111+
Obj::init($obj, $value);
112+
}
113+
114+
if ($opt) {
115+
$init = $opt['init'] ?? true;
116+
if ($init && method_exists($obj, 'init')) {
117+
$obj->init();
118+
}
119+
}
120+
}
121+
}
122+
123+
// as config data.
124+
if ($obj === null) {
125+
$obj = $value;
126+
}
127+
128+
return $obj;
129+
}
130+
131+
/**
132+
* @param string $id
133+
* @param mixed $definition
134+
* @param bool $override
135+
*/
136+
public function set(string $id, $definition, bool $override = false): void
137+
{
138+
if ($override === false && $this->has($id)) {
139+
throw new ContainerException("box: the '$id' has been registered");
140+
}
141+
142+
$this->definitions[$id] = $definition;
143+
}
144+
145+
/**
146+
* Returns true if the container can return an entry for the given identifier.
147+
* Returns false otherwise.
148+
*
149+
* `has($id)` returning true does not mean that `get($id)` will not throw an exception.
150+
* It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface`.
151+
*
152+
* @param string $id Identifier of the entry to look for.
153+
*
154+
* @return bool
155+
*/
156+
public function has(string $id): bool
157+
{
158+
if (isset($this->objects[$id])) {
159+
return true;
160+
}
161+
162+
return isset($this->definitions[$id]);
163+
}
164+
165+
/**
166+
* @param string $id
167+
*
168+
* @return mixed|null
169+
*/
170+
public function getObject(string $id)
171+
{
172+
return $this->objects[$id] ?? null;
173+
}
174+
175+
/**
176+
* @param string $id
177+
* @param object|mixed $obj
178+
*/
179+
public function setObject(string $id, $obj): void
180+
{
181+
$this->objects[$id] = $obj;
182+
}
183+
184+
/**
185+
* @param string $id
186+
*
187+
* @return mixed|null
188+
*/
189+
public function getDefinition(string $id)
190+
{
191+
return $this->definitions[$id] ?? null;
192+
}
23193
}

0 commit comments

Comments
 (0)
Please sign in to comment.