From 57bb6ff557c60ff7fdb2b3e394a199bb4c31fccf Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Thu, 24 Jul 2014 15:02:32 +0100 Subject: [PATCH] Fix syntax in dispatch cycle --- src/Core/Start.php | 10 ++++------ src/Dispatcher/Dispatcher.php | 12 +++++++++++- src/Dispatcher/DispatcherFactory.php | 2 +- src/Dispatcher/PageDispatcher.php | 11 +++++++++++ src/Response/ApiResponse.php | 11 ----------- src/Response/Dom/Document.php | 16 ++++++++++++++-- src/Response/ResponseCodeFactory.php | 23 ----------------------- src/Response/ResponseContent.php | 13 +++++++++++-- src/Response/ResponseContentFactory.php | 23 ----------------------- 9 files changed, 52 insertions(+), 69 deletions(-) delete mode 100644 src/Response/ApiResponse.php delete mode 100644 src/Response/ResponseCodeFactory.php delete mode 100644 src/Response/ResponseContentFactory.php diff --git a/src/Core/Start.php b/src/Core/Start.php index b3c397ad..f9ebc558 100644 --- a/src/Core/Start.php +++ b/src/Core/Start.php @@ -13,29 +13,27 @@ namespace Gt\Core; use \Gt\Request\Standardiser; use \Gt\Request\Request; -use \Gt\Response\Redirect; +use \Gt\Response\Response; use \Gt\Api\ApiFactory; use \Gt\Database\DatabaseFactory; use \Gt\Dispatcher\DispatcherFactory; final class Start { -private $config; - public function __construct($uri) { if(empty($_SERVER)) { throw new \Gt\Core\Exception\UndefinedVariableException( "\$_SERVER is not defined. Are you running from cli?"); } - $this->config = new Config(); + $config = new Config(); $standardiser = new Standardiser(); - $uriFixed = $standardiser->fixUri($uri, $this->config["request"]); + $uriFixed = $standardiser->fixUri($uri, $config["request"]); if($uri !== $uriFixed) { // Only perform permanent redirects on production applications. $code = 302; - if($this->config["app"]->production) { + if($config["app"]->production) { $code = 301; } diff --git a/src/Dispatcher/Dispatcher.php b/src/Dispatcher/Dispatcher.php index f29732ca..4d8b79f9 100644 --- a/src/Dispatcher/Dispatcher.php +++ b/src/Dispatcher/Dispatcher.php @@ -26,10 +26,20 @@ public function __construct(Request $request, Response $response, $this->dbFactory = $dbFactory; } +/** + * Creates a suitable ResponseContent object for the type of dispatcher. + * For a PageDispatcher, the ResponseContent will be a Gt\Response\Dom\Document + * @return ResponseContent The object to serialise as part of the HTTP response + */ +abstract function createResponseContent(); + +/** + * Performs the dispatch cycle. + */ public function process() { // Create and assign the Response content. This object may represent a // DOMDocument or ApiObject, depending on request type. - $content = ResponseContentFactory::create($this->request->getType()); + $content = $this->createResponseContent(); // Construct and assign ResponseCode object, which is a collection of // Code class instantiations in order of execution. diff --git a/src/Dispatcher/DispatcherFactory.php b/src/Dispatcher/DispatcherFactory.php index 74026bb9..74fe36ee 100644 --- a/src/Dispatcher/DispatcherFactory.php +++ b/src/Dispatcher/DispatcherFactory.php @@ -7,7 +7,7 @@ */ namespace Gt\Dispatcher; use Gt\Request\Request; -use Gt\Request\Response; +use Gt\Response\Response; use Gt\Api\ApiFactory; use Gt\Database\DatabaseFactory; diff --git a/src/Dispatcher/PageDispatcher.php b/src/Dispatcher/PageDispatcher.php index ee7eb5bf..0486e0f9 100644 --- a/src/Dispatcher/PageDispatcher.php +++ b/src/Dispatcher/PageDispatcher.php @@ -8,4 +8,15 @@ namespace Gt\Dispatcher; class PageDispatcher extends Dispatcher { +public function createResponseContent() { + $domDocument = new \Gt\Response\Dom\Document(); + + $htmlPath = $this->getPath(Dispatcher::PATH_HTML); + $htmlArray = []; + + $domDocument->load($htmlArray); + + return $domDocument; +} + }# \ No newline at end of file diff --git a/src/Response/ApiResponse.php b/src/Response/ApiResponse.php deleted file mode 100644 index 4b8a4967..00000000 --- a/src/Response/ApiResponse.php +++ /dev/null @@ -1,11 +0,0 @@ -domDocument = new DOMDocument("1.0", "utf-8"); + $this->domDocument = new \DOMDocument("1.0", "utf-8"); } -public function load($response) { + +public function serialize() { + // TODO: Serialize +} + +/** + * Synonym for unserialize. + */ +public function load($content) { + return $this->unserialize($content); +} + +public function unserialize($serialized) { libxml_use_internal_errors(true); $html = ""; diff --git a/src/Response/ResponseCodeFactory.php b/src/Response/ResponseCodeFactory.php deleted file mode 100644 index 8a6093ff..00000000 --- a/src/Response/ResponseCodeFactory.php +++ /dev/null @@ -1,23 +0,0 @@ -