Skip to content

Commit

Permalink
injectable deps, traits, default controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
henderjon committed Jun 23, 2016
1 parent cd43f66 commit 66d5ac3
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 418 deletions.
156 changes: 0 additions & 156 deletions src/Controllers/FrontController.php

This file was deleted.

66 changes: 18 additions & 48 deletions src/Dispatcher/AbstractDispatchableController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,33 @@

use Psr\Log;
use Chevron\Kernel\Router\RouteInterface;
use Chevron\Kernel\Traits\DiAwareTrait;
use Chevron\Kernel\Traits\RouteAwareTrait;
use Chevron\Kernel\Traits\InjectableMethodParamsInvocationTrait;
use Chevron\Kernel\Traits\RedirectableControllerTrait;

abstract class AbstractDispatchableController implements DispatchableInterface {

use Log\LoggerAwareTrait;

protected $di, $route;
use InjectableMethodParamsInvocationTrait;
use RedirectableControllerTrait;
use DiAwareTrait;
use RouteAwareTrait;

function __construct( $di, $route ){
$this->di = $di;
$this->route = $route;
$this->setDi($di);
$this->setRoute($route);
}

abstract function init();

function getDi(){
return $this->di;
}

function getRoute(){
return $this->route;
}

function __invoke(){
$action = $this->route->getAction();
$action = $this->getRoute()->getAction();

if(method_exists($this, $action)){
return call_user_func_array([$this, $action], func_get_args());
return $this->callMethodFromReflectiveDiMethodParams($this->getDi(), $this, $action, func_get_args());
}

$this->logException(new ActionNotFoundException);
}

Expand All @@ -42,44 +42,14 @@ protected function logException(\Exception $e){
"e.code" => $e->getCode(),
"e.file" => $e->getFile(),
"e.line" => $e->getLine(),
"route.controller" => $this->route->getController(),
"route.action" => $this->route->getAction(),
"route.format" => $this->route->getFormat(),
"route.params" => $this->route->getParams(),
"route.controller" => $this->getRoute()->getController(),
"route.action" => $this->getRoute()->getAction(),
"route.format" => $this->getRoute()->getFormat(),
"route.params" => $this->getRoute()->getParams(),
"info.class" => get_class($this),
]);
}
throw $e;
}

/****************************
EXAMPLE ERROR HANDLING
****************************/

// protected $response;

// function init(){
// // init something fancy
// $this->response = $this->di->get("response");
// }

/**
* handle errors
*
* 404 -- "404 means \"00PS\" in H@X0R.\n\n"
* 500 -- "OH NOES!! Something very wrong is happening.\n\n"
*/
// function __invoke($code = 404, \Exception $e = null){
// $this->setErrorHeaders(intval($code));
// if($e){ $this->logException($e); }
// return function(){ };
// }

// protected function setErrorHeaders($status){
// if($this->response InstanceOf HeadersInterface){
// $response->setContentType($this->route->getFormat());
// $response->setStatusCode($status);
// }
// }

}
10 changes: 6 additions & 4 deletions src/Router/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
*/
class Route implements RouteInterface{

const CONTROLLER_KEY = "controller";
const DEFAULT_CONTROLLER = "index";

const ACTION_KEY = "action";
const CONTROLLER_KEY = "controller";

const FORMAT_KEY = "format";
const ACTION_KEY = "action";

const FORMAT_KEY = "format";

/**
* the value to return as the requested controller.
Expand Down Expand Up @@ -46,7 +48,7 @@ class Route implements RouteInterface{
* @return \Chevron\Router\Route
*/
function __construct($controller, $action = null, $format = null, array $params = []){
$this->controller = $controller;
$this->controller = $controller ?: self::DEFAULT_CONTROLLER;

if($action){
$this->setAction($action);
Expand Down
20 changes: 20 additions & 0 deletions src/Traits/DiAwareTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Chevron\Kernel\Traits;

trait DiAwareTrait {

protected $di;

/**
*
*/
public function setDi($di){
$this->di = $di;
}

public function getDi(){
return $this->di;
}

}
50 changes: 50 additions & 0 deletions src/Traits/InjectableMethodParamsInvocationTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Chevron\Kernel\Traits;

trait InjectableMethodParamsInvocationTrait {

/**
* @param DiInterface $di
* @param \ReflectionMethod $ref
* @param array $initials
* @return array
*/
protected function getReflectiveDiMethodParams( $di, \ReflectionMethod $ref, array $initials = [ ] ) {
/** @var \ReflectionParameter[] $cParams */
$cParams = array_slice($ref->getParameters(), count($initials));
$arguments = $initials;
foreach( $cParams as $cParam ) {
$arguments[] = $di->get($cParam->getName());
}
return $arguments;
}
/**
* @param DiInterface $di
* @param string $className
* @param array $initials
* @return object
*/
protected function constructInstanceFromReflectiveDiMethodParams( $di, $className, array $initials = [ ] ) {
$inst = new \ReflectionClass($className);
$ref = $inst->getConstructor();
if( $ref instanceof \ReflectionMethod ) {
$args = $this->getReflectiveDiMethodParams($di, $ref, $initials);
return $inst->newInstanceArgs($args);
}
return new $className;
}
/**
* @param DiInterface $di
* @param object $class
* @param string $method
* @param array $initials
* @return mixed
*/
protected function callMethodFromReflectiveDiMethodParams( $di, $class, $method, array $initials = [ ] ) {
$ref = new \ReflectionMethod($class, $method);
$args = $this->getReflectiveDiMethodParams($di, $ref, $initials);
return call_user_func_array([ $class, $method ], $args);
}

}
27 changes: 27 additions & 0 deletions src/Traits/RedirectableControllerTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Chevron\Kernel\Traits;

trait RedirectableControllerTrait {

/**
* @return \Capstone\Di\DiInterface
*/
abstract public function getDi();

public function redirect( $url ) {
/**
* @var \Capstone\HTTP\Utils\Fulfillment $fulfillment
* @var \Capstone\Stubs\LayoutWidget $layout
*/
$fulfillment = $this->getDi()->get("fulfillment");
// $layout = $this->getDi()->get("layout");

// $layout->setLayout('raw');
$fulfillment->setRedirect($url);

return function () use ($url) {
echo "Redirecting to: {$url}";
};
}
}
Loading

0 comments on commit 66d5ac3

Please sign in to comment.