Last active
October 9, 2015 09:35
-
-
Save weburnit/ae59b6d63c91d7ab6329 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class ApplicationEngine | |
{ | |
/** | |
* @var InterpreterInterface[] | |
*/ | |
private $interpreters = []; | |
private $kernelContainer; | |
public function __construct() | |
{ | |
$controllerInterpreter = new ControllerInterpreter(); | |
$getRequestInterpreter = new GetRequestInterpreter(); | |
$this->interpreters[] = $controllerInterpreter; | |
$this->interpreters[] = $getRequestInterpreter; | |
} | |
public function compileRequest(RequestInterface $request) | |
{ | |
foreach ($this->interpreters as $interpreter) { | |
if ($interpreter->getContainerKey()) { | |
$this->kernelContainer[$interpreter->getContainerKey()] = $interpreter->interpret($request); | |
} else { | |
throw new Exception('Your interpreter does return a proper key through getContainerKey'); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class ControllerInterpreter implements InterpreterInterface | |
{ | |
protected $controller; | |
protected $action; | |
protected $params; | |
public function getContainerKey() | |
{ | |
return 'controller'; | |
} | |
public function interpret(RequestInterface $request) | |
{ | |
$uri = $request->getURI(); | |
$extractRequest = explode('/', $uri); | |
$this->controller = current($extractRequest); | |
if (count($extractRequest) >= 2) { | |
$this->action = $extractRequest[1]; | |
} else { | |
$this->action = 'index'; | |
} | |
$paramInterpreter = new ParamInterpreter($this); | |
$paramInterpreter->interpret($request); | |
$this->params = $paramInterpreter->getParams(); | |
return $this; | |
} | |
/** | |
* @return string | |
*/ | |
public function getController() | |
{ | |
return $this->controller; | |
} | |
/** | |
* @return string | |
*/ | |
public function getAction() | |
{ | |
return $this->action; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class GetRequestInterpreter implements InterpreterInterface | |
{ | |
private $params = []; | |
public function getContainerKey() | |
{ | |
return 'get_request'; | |
} | |
public function interpret(RequestInterface $request) | |
{ | |
$uri = $request->getURI(); | |
//Extract data and get GET request | |
} | |
public function get($key) | |
{ | |
return isset($this->params[$key]) ? $this->params[$key] : null; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
interface InterpreterInterface | |
{ | |
public function interpret(RequestInterface $request); | |
public function getContainerKey(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class ParamInterpreter implements InterpreterInterface | |
{ | |
/** | |
* @var array | |
*/ | |
private $params; | |
/** | |
* @var ControllerInterpreter | |
*/ | |
private $controllerInterpreter; | |
public function getContainerKey() | |
{ | |
return 'param'; | |
} | |
public function __construct(ControllerInterpreter $controllerInterpreter) | |
{ | |
$this->controllerInterpreter = $controllerInterpreter; | |
} | |
public function interpret(RequestInterface $request) | |
{ | |
$uri = $request->getURI(); | |
$controller = $this->controllerInterpreter->getController(); | |
$action = $this->controllerInterpreter->getAction(); | |
$paramChain = str_replace($controller.'/'.$action, '', $uri); | |
//Extract paramChain into action param | |
} | |
/** | |
* @return mixed | |
*/ | |
public function getParams() | |
{ | |
return $this->params; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment