Skip to content

Commit

Permalink
Dependency Injection for controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
kokororin committed Nov 29, 2017
1 parent 666d588 commit 4a7ff2b
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 4 deletions.
8 changes: 5 additions & 3 deletions example/app/controllers/Hello.php
Expand Up @@ -4,7 +4,8 @@
use app\libraries\Captcha;
use Kotori\Core\Controller;
use Kotori\Facade\Cache;
use Kotori\Facade\Request;
use Kotori\Facade\Request as FacadeRequest;
use Kotori\Http\Request;

class Hello extends Controller
{
Expand Down Expand Up @@ -44,9 +45,10 @@ public function addNews()
/**
* @route(method = "post", uri = "add")
*/
public function insertNews()
public function insertNews(Request $request)
{
print_r(Request::post());
print_r(FacadeRequest::post());
print_r($request->post());
}

/**
Expand Down
21 changes: 21 additions & 0 deletions src/Core/Container.php
Expand Up @@ -98,6 +98,27 @@ public static function get($abstract)
return self::getInstance()->containers[$abstract];
}

/**
* Get the object instance in the container by class name
*
* @param string $className
* @return object
*
* @throws \Kotori\Exception\ContainerException
*/
public static function getByClassName($className)
{
$bind = self::getInstance()->bind;

foreach ($bind as $abstract => $abstractClassName) {
if ($className == $abstractClassName) {
return self::get($abstract);
}
}

throw new ContainerException('Cannot find "' . $className . '" in container');
}

/**
* bind object maps for the container
*
Expand Down
51 changes: 50 additions & 1 deletion src/Http/Route.php
Expand Up @@ -236,7 +236,14 @@ public function dispatch()

Middleware::register('before_action');
// Call the requested method
call_user_func_array($callback, $this->params);

$methodInstances = $this->getMethodInstances($callback[0], $callback[1]);
if (!$methodInstances) {
call_user_func_array($callback, $this->params);
} else {
call_user_func_array($callback, $methodInstances);
}

Middleware::register('after_action');
}

Expand Down Expand Up @@ -288,6 +295,48 @@ public function getParams()
return array_merge($params);
}

/**
* Returns the request params instances
*
* @param string $className
* @param string $methodName
* @return mixed
*
* @throws \Kotori\Exception\NotFoundException
*/
private function getMethodInstances($className, $methodName = '__construct')
{
$reflectClass = new ReflectionClass($className);
$instances = [];

if ($reflectClass->hasMethod($methodName)) {
$reflectMethod = $reflectClass->getMethod($methodName);

$params = $reflectMethod->getParameters();

$hasDI = false;

if (count($params) > 0) {
foreach ($params as $param) {
$paramClass = $param->getClass();
if ($paramClass) {
$paramClassName = $paramClass->getName();
array_push($instances, Container::getByClassName($paramClassName));
$hasDI = true;
} elseif ($hasDI) {
throw new NotFoundException('Dependency Injection cannot work with normal params');
}
}
}
}

if ($hasDI) {
return $instances;
}

return false;
}

/**
* Returns the URI
*
Expand Down

0 comments on commit 4a7ff2b

Please sign in to comment.