Skip to content
This repository has been archived by the owner on Jan 27, 2022. It is now read-only.

Commit

Permalink
Create bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
iamfredric committed May 2, 2019
1 parent 19c86fa commit f354c33
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/Instantiator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@

class Instantiator
{
/**
* @var array
*/
private static $bindings = [];

/**
* @var ReflectionClass
*/
Expand All @@ -32,6 +37,16 @@ public function __construct($classname)
$this->classname = $classname;
}

/**
* Bind a classname to a custom callable
*
* @return void
*/
public static function bind($classname, callable $callable)
{
self::$bindings[$classname] = $callable;
}

/**
* News up the class object and resolves its dependencies
*
Expand All @@ -47,6 +62,18 @@ public function call()
return InstantiationsBinder::resolve($this->classname);
}

// If the classname i binded, the instance will be resolved from
// the defined callable binding
if (isset(self::$bindings[$this->classname])) {
return self::$bindings[$this->classname]($this->classname);
}

// If the parent classname i binded, the instance will be resolved from
// the defined callable binding
if ($this->reflection->getParentClass() && isset(self::$bindings[$this->reflection->getParentClass()->getName()])) {
return self::$bindings[$this->reflection->getParentClass()->getName()]($this->classname);
}

// If the parent instance bindings is defined, the instance will be
// resolved from parent class bindings
if ($this->reflection->getParentClass() and InstantiationsBinder::has($this->reflection->getParentClass()->getName())) {
Expand Down

0 comments on commit f354c33

Please sign in to comment.