Skip to content

Commit

Permalink
add method alias for make aliases, and method forget for unset defini…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
zafex committed May 1, 2019
1 parent f3f7702 commit e785599
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 22 deletions.
86 changes: 68 additions & 18 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@

class Container implements IContainer
{
/**
* @var array
*/
protected $aliases = [];

/**
* @var array
*/
Expand All @@ -33,23 +38,57 @@ public function __get($id)
return $this->get($id);
}

/**
* @param string $id
* @param string $target
*/
public function alias(string $id, string $target): void
{
if (array_key_exists($id, $this->definitions)) {
throw new ContainerException("ID {$id} already registered as definitions.");
}
if (array_key_exists($id, $this->aliases)) {
throw new ContainerException("ID {$id} already registered as aliases for " . $this->getRealIdentifier($id));
}
$this->aliases[$id] = $target;
}

/**
* @param string $id
* @param bool $recursive
*/
public function forget(string $id, bool $recursive = false): void
{
if (array_key_exists($id, $this->definitions)) {
$this->definitions[$id] = null;
unset($this->definitions[$id]);
if (array_key_exists($id, $this->components)) {
$this->components[$id] = null;
unset($this->components[$id]);
}
}
if (array_key_exists($id, $this->aliases)) {
if ($recursive === true) {
$this->forget($this->aliases[$id], $recursive);
}
$this->aliases[$id] = null;
unset($this->aliases[$id]);
}
}

/**
* @param $id
*/
public function get($id)
public function get($name)
{
$id = $this->getRealIdentifier($name);
if (!array_key_exists($id, $this->components)) {
if ($this->has($id) === true) {
if (is_callable($this->definitions[$id]['target'])) {
$this->components[$id] = $this->invoke(
$this->definitions[$id]['target'],
$this->definitions[$id]['params']
);
if (array_key_exists($id, $this->definitions) === true) {
$definition = $this->definitions[$id];
if (is_callable($definition['target'])) {
$this->components[$id] = $this->invoke($definition['target'], $definition['params']);
} else {
$this->components[$id] = $this->make(
$this->definitions[$id]['target'],
$this->definitions[$id]['params']
);
$this->components[$id] = $this->make($definition['target'], $definition['params']);
}
} else {
throw new NotFoundException("{$id} does not found.");
Expand All @@ -58,12 +97,24 @@ public function get($id)
return $this->components[$id];
}

/**
* @param string $name
* @return mixed
*/
public function getRealIdentifier(string $name): string
{
while (array_key_exists($name, $this->aliases) === true) {
$name = $this->aliases[$name];
}
return $name;
}

/**
* @param $id
*/
public function has($id)
{
return array_key_exists($id, $this->definitions);
return array_key_exists($this->getRealIdentifier($id), $this->definitions);
}

/**
Expand Down Expand Up @@ -127,9 +178,9 @@ public function make(string $class, array $params = [])
* @param $target
* @param array $params
*/
public function map($id, $target, array $params = [])
public function map($id, $target, array $params = []): void
{
if ($this->has($id) === true) {
if (array_key_exists($id, $this->definitions) === true) {
throw new ContainerException("ID {$id} already registered.");
}
if ($id === Closure::class) {
Expand All @@ -155,17 +206,16 @@ public function raw($id)
* @param $target
* @param array $params
*/
public function remap($id, $target, array $params = [])
public function remap($id, $target, array $params = []): void
{
if ($this->has($id) === false) {
if (array_key_exists($id, $this->definitions) === false) {
throw new ContainerException("ID {$id} hasn't been registered yet.");
}
if ($id === Closure::class) {
throw new ContainerException("{$id} names cannot be registered.");
}
if (array_key_exists($id, $this->components)) {
$this->components[$id] = null;
unset($this->components[$id]);
$this->forget($id, false);
}
$this->definitions[$id] = compact('target', 'params');
}
Expand Down
25 changes: 21 additions & 4 deletions src/Contracts/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,41 @@ interface Container extends ContainerInterface
*/
public function __get($id);

/**
* @param string $id
* @param string $target
*/
public function alias(string $id, string $target): void;

/**
* @param string $id
* @param bool $recursive
*/
public function forget(string $id, bool $recursive): void;

/**
* @param string $name
*/
public function getRealIdentifier(string $name): string;

/**
* @param $callback
* @param array $params
*/
public function invoke(callable $callback, array $params = []);
public function invoke(callable $callback, array $params);

/**
* @param string $class
* @param array $params
*/
public function make(string $class, array $params = []);
public function make(string $class, array $params);

/**
* @param $id
* @param $target
* @param array $params
*/
public function map($id, $target, array $params = []);
public function map($id, $target, array $params): void;

/**
* @param $id
Expand All @@ -40,5 +57,5 @@ public function raw($id);
* @param $target
* @param array $params
*/
public function remap($id, $target, array $params = []);
public function remap($id, $target, array $params): void;
}

0 comments on commit e785599

Please sign in to comment.