| 1 | <?php |
||
| 10 | trait Injector |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * @var array<string> プロパティマップ |
||
| 14 | */ |
||
| 15 | private $__propertyMap; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * オブジェクトを注入する |
||
| 19 | * @param string プロパティ名 |
||
| 20 | * @param mixed オブジェクト |
||
| 21 | * @return Injector |
||
| 22 | */ |
||
| 23 | public function inject($name, $object) |
||
| 24 | { |
||
| 25 | $this->{$name} = $object; |
||
| 26 | |||
| 27 | return $this; |
||
| 28 | } |
||
| 29 | |||
| 30 | /** |
||
| 31 | * overload setter |
||
| 32 | */ |
||
| 33 | public function __set($name, $value) |
||
| 34 | { |
||
| 35 | if ($this->__propertyMap === null) { |
||
| 36 | $this->__propertyMap = []; |
||
| 37 | } |
||
| 38 | $this->__propertyMap[$name] = $value; |
||
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * overload setter |
||
| 43 | */ |
||
| 44 | public function __get($name) |
||
| 45 | { |
||
| 46 | return $this->__propertyMap !== null && array_key_exists($name, $this->__propertyMap) ? $this->__propertyMap[$name] : null; |
||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * overload isset |
||
| 51 | */ |
||
| 52 | public function __isset($name) |
||
| 53 | { |
||
| 54 | return $this->__propertyMap === null && array_key_exists($name, $this->__propertyMap) === false; |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * overload unset |
||
| 59 | */ |
||
| 60 | public function __unset($name) |
||
| 61 | { |
||
| 62 | if ($this->__propertyMap !== null && array_key_exists($name, $this->__propertyMap)) { |
||
| 63 | unset($this->__propertyMap[$name]); |
||
| 64 | } |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * コンテナクリア |
||
| 69 | */ |
||
| 70 | public function __clear() |
||
| 71 | { |
||
| 72 | $this->__propertyMap = null; |
||
| 73 | } |
||
| 74 | } |
||
| 75 |