Skip to content

Commit

Permalink
Selamat ulang tahun IBU
Browse files Browse the repository at this point in the history
  • Loading branch information
zafex committed Jan 14, 2019
1 parent 57ec28e commit 6476b84
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 21 deletions.
57 changes: 36 additions & 21 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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.");
}

/**
Expand All @@ -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');
}

/**
Expand Down Expand Up @@ -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' => [],
];
Expand Down
2 changes: 2 additions & 0 deletions src/Contracts/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public function invoke(callable $callback);
*/
public function map($id, $target, array $params = []);

public function raw($id);

/**
* @param $id
* @param $target
Expand Down

0 comments on commit 6476b84

Please sign in to comment.