|
2 | 2 |
|
3 | 3 | namespace Toolkit\Stdlib\Obj;
|
4 | 4 |
|
| 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 | + |
5 | 16 | /**
|
6 | 17 | * Class ObjectBox
|
7 | 18 | *
|
8 |
| - * An simple object containers |
| 19 | + * An simple object containers implements |
9 | 20 | *
|
10 | 21 | * @package Toolkit\Stdlib\Obj
|
11 | 22 | */
|
12 |
| -class ObjectBox |
| 23 | +class ObjectBox implements ContainerInterface |
13 | 24 | {
|
14 | 25 | /**
|
15 | 26 | * @var array
|
16 | 27 | */
|
17 |
| - private $config = []; |
| 28 | + private $objects = []; |
18 | 29 |
|
19 | 30 | /**
|
20 | 31 | * @var array
|
21 | 32 | */
|
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 | + } |
23 | 193 | }
|
0 commit comments