From 6476b84631d0abf1642597f7e95f4ae4a78ba528 Mon Sep 17 00:00:00 2001 From: zafex Date: Tue, 15 Jan 2019 05:15:32 +0700 Subject: [PATCH] Selamat ulang tahun IBU --- src/Container.php | 57 +++++++++++++++++++++++-------------- src/Contracts/Container.php | 2 ++ 2 files changed, 38 insertions(+), 21 deletions(-) diff --git a/src/Container.php b/src/Container.php index f655b8d..814d9f7 100644 --- a/src/Container.php +++ b/src/Container.php @@ -20,22 +20,22 @@ class Container implements IContainer protected $components = []; /** - * @var mixed + * @var array */ - protected static $instance = null; + protected $definitions = []; /** - * @var array + * @var mixed */ - protected $instances = []; + protected static $instance = null; /** - * @param array $components + * @param array $definitions */ - public function __construct(array $components = []) + public function __construct(array $definitions = []) { static::setInstance($this); - foreach ($components as $key => $param) { + foreach ($definitions as $key => $param) { $this->set($key, $param); } } @@ -86,24 +86,24 @@ public function factory(string $class, array $params = []) */ public function get($id) { - if (!array_key_exists($id, $this->instances)) { + if (!array_key_exists($id, $this->components)) { if ($this->has($id) === true) { - if (is_callable($this->components[$id]['target'])) { - $this->instances[$id] = $this->invoke( - $this->components[$id]['target'], - $this->components[$id]['params'] + if (is_callable($this->definitions[$id]['target'])) { + $this->components[$id] = $this->invoke( + $this->definitions[$id]['target'], + $this->definitions[$id]['params'] ); } else { - $this->instances[$id] = $this->factory( - $this->components[$id]['target'], - $this->components[$id]['params'] + $this->components[$id] = $this->factory( + $this->definitions[$id]['target'], + $this->definitions[$id]['params'] ); } } else { throw new NotFoundException("{$id} does not found."); } } - return $this->instances[$id]; + return $this->components[$id]; } public static function getInstance(): IContainer @@ -119,7 +119,7 @@ public static function getInstance(): IContainer */ public function has($id) { - return array_key_exists($id, $this->components); + return array_key_exists($id, $this->definitions); } /** @@ -159,7 +159,19 @@ public function map($id, $target, array $params = []) if ($id === Closure::class) { throw new ContainerException("{$id} names cannot be registered."); } - $this->components[$id] = compact('target', 'params'); + $this->definitions[$id] = compact('target', 'params'); + } + + /** + * @param $id + * @return mixed + */ + public function raw($id) + { + if ($this->has($id)) { + return $this->definitions[$id]; + } + throw new NotFoundException("{$id} does not found."); } /** @@ -175,7 +187,10 @@ public function remap($id, $target, array $params = []) if ($id === Closure::class) { throw new ContainerException("{$id} names cannot be registered."); } - $this->components[$id] = compact('target', 'params'); + if (array_key_exists($id, $this->components)) { + throw new ContainerException("{$id} names has been resolved."); + } + $this->definitions[$id] = compact('target', 'params'); } /** @@ -205,8 +220,8 @@ public function set($id, $argument) public static function setInstance(IContainer $container) { static::$instance = $container; - static::$instance->instances[IContainer::class] = $container; - static::$instance->components[IContainer::class] = [ + static::$instance->components[IContainer::class] = $container; + static::$instance->definitions[IContainer::class] = [ 'target' => get_class($container), 'params' => [], ]; diff --git a/src/Contracts/Container.php b/src/Contracts/Container.php index 2346ad2..4a115c4 100644 --- a/src/Contracts/Container.php +++ b/src/Contracts/Container.php @@ -25,6 +25,8 @@ public function invoke(callable $callback); */ public function map($id, $target, array $params = []); + public function raw($id); + /** * @param $id * @param $target