|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Hackphp\Container; |
| 4 | + |
| 5 | +use Closure; |
| 6 | +use Psr\Container\ContainerInterface; |
| 7 | + |
| 8 | +class Container implements ContainerInterface |
| 9 | +{ |
| 10 | + use Resolver; |
| 11 | + |
| 12 | + /** |
| 13 | + * Container instance. |
| 14 | + * |
| 15 | + * @var static |
| 16 | + */ |
| 17 | + protected static $instance; |
| 18 | + |
| 19 | + /** |
| 20 | + * The binded services. |
| 21 | + * |
| 22 | + * @var array |
| 23 | + */ |
| 24 | + protected array $bindings = []; |
| 25 | + |
| 26 | + /** |
| 27 | + * The shared services. |
| 28 | + * |
| 29 | + * @var array |
| 30 | + */ |
| 31 | + protected array $instances = []; |
| 32 | + |
| 33 | + /** |
| 34 | + * Enable/Disable autowiring. |
| 35 | + * |
| 36 | + * @var bool |
| 37 | + */ |
| 38 | + protected bool $autowiring = true; |
| 39 | + |
| 40 | + /** |
| 41 | + * Prevent create new instance. |
| 42 | + */ |
| 43 | + private function __construct() |
| 44 | + { |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Get the Container instance. |
| 49 | + * |
| 50 | + * @return static |
| 51 | + */ |
| 52 | + public static function getInstance(): self |
| 53 | + { |
| 54 | + if (is_null(static::$instance)) { |
| 55 | + static::$instance = new static(); |
| 56 | + } |
| 57 | + |
| 58 | + return static::$instance; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @inheritDoc |
| 63 | + */ |
| 64 | + public function get(string $id) |
| 65 | + { |
| 66 | + return $this->make($id); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * @inheritDoc |
| 71 | + */ |
| 72 | + public function has(string $id): bool |
| 73 | + { |
| 74 | + return isset($this->bindings[$id]) || isset($this->instances[$id]); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Bind entry to the container. |
| 79 | + * |
| 80 | + * @param string $name |
| 81 | + * @param Closure|string|null $entry |
| 82 | + * @param bool $shared |
| 83 | + * @return void |
| 84 | + */ |
| 85 | + public function bind(string $name, $entry = null, bool $shared = false): void |
| 86 | + { |
| 87 | + $this->bindings[$name] = [ |
| 88 | + "entry" => $entry, |
| 89 | + "shared" => $shared |
| 90 | + ]; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Buind entry to the container as singleton. |
| 95 | + * |
| 96 | + * @param string $name |
| 97 | + * @param Closure|string|null $entry |
| 98 | + * @return void |
| 99 | + */ |
| 100 | + public function singleton(string $name, $entry = null): void |
| 101 | + { |
| 102 | + $this->bind($name, $entry, true); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Bind entry to the container shared instances. |
| 107 | + * |
| 108 | + * @param string $name |
| 109 | + * @param mixed $entry |
| 110 | + * @return void |
| 111 | + */ |
| 112 | + public function instance(string $name, $entry): void |
| 113 | + { |
| 114 | + $this->instances[$name] = $entry; |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Get the entry from the container. |
| 119 | + * |
| 120 | + * @param string $name |
| 121 | + * @return mixed |
| 122 | + */ |
| 123 | + public function make(string $name) |
| 124 | + { |
| 125 | + return $this->resolve($name); |
| 126 | + } |
| 127 | +} |
0 commit comments