Last active
August 29, 2015 14:01
-
-
Save dhrrgn/26a19317daf9dcf0a8df to your computer and use it in GitHub Desktop.
A WIP implementation of the Responder Layer of Paul Jones' ADR Pattern: https://github.com/pmjones/mvc-refinement
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 | |
namespace Core\Responder; | |
class JsonResponder extends Responder | |
{ | |
/** | |
* @return \Orno\Http\Response | |
*/ | |
protected function respond() | |
{ | |
$this->response->setContent(json_encode($this->parameters)); | |
$this->response->headers->set('Content-Type', 'application/json'); | |
return $this->response; | |
} | |
} |
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 | |
namespace Core\Responder; | |
use Core\Support\ParameterBagTrait; | |
use Orno\Http\Response; | |
abstract class Responder | |
{ | |
use ParameterBagTrait; | |
/** | |
* @return Response | |
*/ | |
abstract protected function respond(); | |
/** | |
* @var Response | |
*/ | |
protected $response; | |
/** | |
* @param Response $response | |
*/ | |
public function __construct(Response $response) | |
{ | |
$this->response = $response; | |
} | |
/** | |
* @return Response | |
*/ | |
public function __invoke() | |
{ | |
$response = $this->respond(); | |
$response->setStatusCode($this->getHttpStatus()); | |
return $response; | |
} | |
/** | |
* Gets the HTTP Status Code for the Response. | |
* @return int | |
*/ | |
protected function getHttpStatus() | |
{ | |
return 200; | |
} | |
} |
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 | |
namespace Core\Responder; | |
use Core\Display\ViewManager; | |
use Core\Support\ParameterBagTrait; | |
use Orno\Http\Response; | |
abstract class ViewResponder extends Responder | |
{ | |
/** | |
* @var \Core\Display\ViewManager | |
*/ | |
protected $viewManager; | |
/** | |
* @var \Core\Display\View | |
*/ | |
protected $view; | |
/** | |
* @param Response $response | |
* @param ViewManager $viewManager | |
*/ | |
public function __construct(Response $response, ViewManager $viewManager) | |
{ | |
parent::__construct($response); | |
$this->viewManager = $viewManager; | |
} | |
/** | |
* Loads the given View and renders it, returning the response. | |
* @param string $viewFile | |
* @return Response|void | |
*/ | |
public function __invoke($viewFile) | |
{ | |
$this->view = $this->viewManager->create($viewFile); | |
$this->view->set($this->parameters); | |
return parent::__invoke(); | |
} | |
/** | |
* Render the view and set the response content. | |
* @return Response | |
*/ | |
protected function respond() | |
{ | |
$this->response->setContent($this->view->render()); | |
return $this->response; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Possibly. Not sure. This is only the Responder Layer (using our custom View layer). Maybe.