Last active
December 21, 2015 18:09
-
-
Save johnpancoast/6345712 to your computer and use it in GitHub Desktop.
Framework Example
This file contains hidden or 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 | |
// handle a request, generate a response | |
interface RequestHandlerInterface | |
{ | |
public function setInputHandler(InputHandlerInterface $inputHandler); | |
public function getInputHandler(); | |
public function setOutputHandler(OutputHandlerInterface $outputHandler); | |
public function getOutputHandler(); | |
public function handleRequest(); | |
} | |
interface InputHandlerInterface | |
{ | |
// not sure yet | |
} | |
interface OutputHanlderInterface | |
{ | |
// not sure yet | |
public function preOutput(); | |
public function __toString(); | |
} | |
class OurOwnRequestHandler implements RequestHandlerInterface | |
{ | |
private $inputHandler; | |
public function setInputHandler($inputHandler) | |
{ | |
$this->inputHandler = $inputHandler; | |
} | |
public function getInputHandler() | |
{ | |
return $this->inputHandler; | |
} | |
public function setOutputHandler($outputHandler) | |
{ | |
$this->outputHandler = $outputHandler; | |
} | |
public function getOutputHandler() | |
{ | |
return $this->outputHandler; | |
} | |
public function handleRequest() | |
{ | |
// routing way 1 | |
// framework structure way 1 | |
} | |
} | |
class OtherRequestHandler | |
{ | |
public function handleRequest() | |
{ | |
// routing way 2 | |
// framework structure way 2 | |
} | |
} | |
class Framework | |
{ | |
private $handler; | |
public function build(RequestHandlerInterface $handler) | |
{ | |
$this->handler = $handler; | |
} | |
public function run() | |
{ | |
$this->handler->handleRequest(); | |
$output = $this->handler->getOutputHandler(); | |
$output->preOutput(); // http would set headers for example, cli maybe nothing. | |
return (string)$output; | |
} | |
} | |
index.php | |
$handler = new OurOwnRequestHandler(); | |
$handler->setInputHandler(HttpInputHandler::build()); | |
$handler->setOuputHandler(HttpOutputHandler::build()); | |
echo Framework::build($handler)->run(); | |
-or- | |
cli.php | |
$handler = new OurOwnRequestHandler(); | |
$handler->setInputHandler(CliInputHandler::build()); | |
$handler->setOuputhandler(CliOutputHandler::build()); | |
echo Framework::build($handler)->run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment