Skip to content

Instantly share code, notes, and snippets.

@weburnit
Last active October 9, 2015 09:35
Show Gist options
  • Save weburnit/ae59b6d63c91d7ab6329 to your computer and use it in GitHub Desktop.
Save weburnit/ae59b6d63c91d7ab6329 to your computer and use it in GitHub Desktop.
<?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');
}
}
}
}
<?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;
}
}
<?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;
}
}
<?php
interface InterpreterInterface
{
public function interpret(RequestInterface $request);
public function getContainerKey();
}
<?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