-
-
Save MustafaMagdi/2bb27aebf6ab078b1f3e5635c0282fac to your computer and use it in GitHub Desktop.
<?php | |
/** | |
* Class Container | |
*/ | |
class Container | |
{ | |
/** | |
* @var array | |
*/ | |
protected $instances = []; | |
/** | |
* @param $abstract | |
* @param null $concrete | |
*/ | |
public function set($abstract, $concrete = NULL) | |
{ | |
if ($concrete === NULL) { | |
$concrete = $abstract; | |
} | |
$this->instances[$abstract] = $concrete; | |
} | |
/** | |
* @param $abstract | |
* @param array $parameters | |
* | |
* @return mixed|null|object | |
* @throws Exception | |
*/ | |
public function get($abstract, $parameters = []) | |
{ | |
// if we don't have it, just register it | |
if (!isset($this->instances[$abstract])) { | |
$this->set($abstract); | |
} | |
return $this->resolve($this->instances[$abstract], $parameters); | |
} | |
/** | |
* resolve single | |
* | |
* @param $concrete | |
* @param $parameters | |
* | |
* @return mixed|object | |
* @throws Exception | |
*/ | |
public function resolve($concrete, $parameters) | |
{ | |
if ($concrete instanceof Closure) { | |
return $concrete($this, $parameters); | |
} | |
$reflector = new ReflectionClass($concrete); | |
// check if class is instantiable | |
if (!$reflector->isInstantiable()) { | |
throw new Exception("Class {$concrete} is not instantiable"); | |
} | |
// get class constructor | |
$constructor = $reflector->getConstructor(); | |
if (is_null($constructor)) { | |
// get new instance from class | |
return $reflector->newInstance(); | |
} | |
// get constructor params | |
$parameters = $constructor->getParameters(); | |
$dependencies = $this->getDependencies($parameters); | |
// get new instance with dependencies resolved | |
return $reflector->newInstanceArgs($dependencies); | |
} | |
/** | |
* get all dependencies resolved | |
* | |
* @param $parameters | |
* | |
* @return array | |
* @throws Exception | |
*/ | |
public function getDependencies($parameters) | |
{ | |
$dependencies = []; | |
foreach ($parameters as $parameter) { | |
// get the type hinted class | |
$dependency = $parameter->getClass(); | |
if ($dependency === NULL) { | |
// check if default value for a parameter is available | |
if ($parameter->isDefaultValueAvailable()) { | |
// get default value of parameter | |
$dependencies[] = $parameter->getDefaultValue(); | |
} else { | |
throw new Exception("Can not resolve class dependency {$parameter->name}"); | |
} | |
} else { | |
// get dependency resolved | |
$dependencies[] = $this->get($dependency->name); | |
} | |
} | |
return $dependencies; | |
} | |
} |
@jimgwhit here is an example
<?php
class User
{
public $id;
public $username;
public function where($column, $value)
{
if ($column == 'id') {
$this->id = $value;
if ($this->id == 1) $this->username = 'John Doe';
if ($this->id == 2) $this->username = 'James White';
if ($this->id == 3) $this->username = 'Martin';
}
return $this;
}
}
class HomeController
{
public function index(User $user)
{
var_dump($user);
}
}
$method = new ReflectionMethod('HomeController', 'index');
$parameters = $method->getParameters();
foreach ($parameters as $parameter)
{
$class = $parameter->getClass()->name;
$name = $parameter->getName();
}
$id = $_GET['user'] ?? 1;
// Find model by id
$model = new $class;
$model = $model->where('id', $id);
// Inject model into controller function
$method->invokeArgs(new HomeController, [
$model,
]);
?>
How to implement singleton pattern using this container. can you explain please..I want to store same instance(Singleton) of database using this container in the instances Array. How can i do that without losing typeHinting??
How to implement singleton pattern using this container. can you explain please..I want to store same instance(Singleton) of database using this container in the instances Array. How can i do that without losing typeHinting??
you can make an array to save dependencies that already resolved, and when you need to resolve another dependencies, you can look up to that array, if there any dependencies that match just return it.
nice solution, thanks! since
$dependency = $parameter->getClass();
is deprecated in the lastest php version, what would you suggest to fix that? 🤔
$dependency = $parameter->getType() && !$parameter->getType()->isBuiltin() ? new \ReflectionClass($parameter->getType()->getName()) : null;
This change fixes the error caused by the Deprecated: Function ReflectionParameter::getClass() is deprecated message after a new PHP update.
Can you give an example of using it in a controller to inject a model?