Skip to content

Commit

Permalink
Dependency Injection support for __construct function
Browse files Browse the repository at this point in the history
  • Loading branch information
kokororin committed Nov 29, 2017
1 parent 4a7ff2b commit 2065303
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/Http/Route.php
Expand Up @@ -189,7 +189,14 @@ public function dispatch()
if (isset($this->controllers[$this->controller])) {
$class = $this->controllers[$this->controller];
} else {
$class = new $controllerClassName();
$constructInstances = $this->getMethodInstances($controllerClassName);
if (!$constructInstances) {
$class = new $controllerClassName();
} else {
$reflect = new ReflectionClass($controllerClassName);
$class = $reflect->newInstanceArgs($constructInstances);
}

$this->controllers[$this->controller] = $class;
}

Expand Down Expand Up @@ -298,15 +305,20 @@ public function getParams()
/**
* Returns the request params instances
*
* @param string $className
* @param mixed $class
* @param string $methodName
* @return mixed
*
* @throws \Kotori\Exception\NotFoundException
*/
private function getMethodInstances($className, $methodName = '__construct')
private function getMethodInstances($class, $methodName = '__construct')
{
$reflectClass = new ReflectionClass($className);
if (is_object($class)) {
$reflectClass = new ReflectionClass(get_class($class));
} else {
$reflectClass = new ReflectionClass($class);
}

$instances = [];

if ($reflectClass->hasMethod($methodName)) {
Expand Down

0 comments on commit 2065303

Please sign in to comment.