Skip to content

Commit

Permalink
Allow pipelining of responses
Browse files Browse the repository at this point in the history
  • Loading branch information
siosphere committed Aug 21, 2017
1 parent 8aa6aaa commit 5e758cf
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 20 deletions.
20 changes: 16 additions & 4 deletions Virge/Router/Component/Response.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
<?php
namespace Virge\Router\Component;

use Virge\Router\Service\PipelineService;
use Virge\Virge;

/**
*
* @author Michael Kramer
*/

class Response extends \Virge\Core\Model {
class Response extends \Virge\Core\Model
{
protected $body;

protected $headers = array();
Expand All @@ -25,7 +28,11 @@ public function __construct($body = null, $status_code = 200){
/**
* Send the response
*/
public function send() {
public function send($usePipline = true)
{
if($usePipline) {
return $this->getPipelineService()->prepareResponse($this);
}
$this->_sendHeaders();
$this->_sendBody();
}
Expand All @@ -38,7 +45,7 @@ protected function _sendHeaders() {
if(empty($this->headers)) {
$this->headers[] = 'Content-Type: text/html';
}

foreach($this->headers as $header) {
header($header);
}
Expand All @@ -50,4 +57,9 @@ protected function _sendHeaders() {
protected function _sendBody() {
echo $this->getBody();
}

protected function getPipelineService() : PipelineService
{
return Virge::service(PipelineService::class);
}
}
20 changes: 20 additions & 0 deletions Virge/Router/Component/Response/Pipe.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
namespace Virge\Router\Component\Response;

use Virge\Router\Component\Response;
use Virge\Router\Service\PipelineService;
use Virge\Virge;

abstract class Pipe
{
abstract public function prepareResponse(Response $response);

public function pipe(Pipe $pipe) {

}

protected function getPipelineService() : PipelineService
{
return Virge::service(PipelineService::class);
}
}
29 changes: 29 additions & 0 deletions Virge/Router/Service/PipelineService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
namespace Virge\Router\Service;

use Virge\Router\Component\Response;
use Virge\Router\Component\Response\Pipe;

/**
* Used to pipeline a response to modify and then ultimately send the response
*/
class PipelineService
{
protected $pipes = [];

public function prepareResponse(Response $response)
{
foreach($this->pipes as $pipe) {
$pipe->prepareResponse($response);
}

$response->send(false);
}

public function addPipe(Pipe $pipe)
{
$this->pipes[] = $pipe;

return $pipe;
}
}
23 changes: 10 additions & 13 deletions Virge/Router/Service/RouterService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use Virge\Router\Component\Request;
use Virge\Router\Component\Response;
use Virge\Router\Component\Response\Pipe;
use Virge\Router\Component\Route;

use Virge\Router\Exception\NotFoundException;
Expand Down Expand Up @@ -34,14 +35,7 @@ public function route($uri = null) {

$method = $resolverConfig['method'];
if(false !== ($response = call_user_func_array( array( $resolver, $method), array($request) ))) {

if($response instanceof Response) {
$response->send();
} else {
$response = new Response($response);
$response->send();
}
return true;
return $this->sendResponse($response);
}
}

Expand All @@ -56,16 +50,19 @@ public function route($uri = null) {

$route->setActive(true);

if(is_callable($route->getContoller())){
return call_user_func($route->getController(), $request);
if(is_callable($route->getController())){
return $this->sendResponse(call_user_func($route->getController(), $request));
}

$controllerClassname = $route->getController();
$controller = new $controllerClassname;
$method = $route->getMethod();

$response = call_user_func_array(array($controller, $method), array($request));

$this->sendResponse(call_user_func_array(array($controller, $method), array($request)));
}

protected function sendResponse($response)
{
if($response instanceof Response) {
$response->send();
} else {
Expand Down
11 changes: 9 additions & 2 deletions Virge/Router/config/services.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
<?php

use Virge\Router\Service\{
PipelineService,
RouterService,
TemplateService
};
use Virge\Virge;

/**
* Registers all given handlers with Virge that this Capsule contains
* @author Michael Kramer
*/
Virge::registerService("router", "\\Virge\\Router\\Service\\RouterService");
Virge::registerService("templating", "\\Virge\\Router\\Service\\TemplateService");

Virge::registerService(PipelineService::class, PipelineService::class);
Virge::registerService("router", RouterService::class);
Virge::registerService("templating", TemplateService::class);
16 changes: 15 additions & 1 deletion Virge/Routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@

use Virge\Router\Component\{
Request,
Response\Pipe,
Route
};
use Virge\Router\Service\PipelineService;
use Virge\Virge;

/**
*
* @author Michael Kramer
*/
class Routes {
class Routes
{

protected static $_routes = array();

Expand Down Expand Up @@ -64,6 +68,11 @@ public static function add($url, $controller, $method = 'run', $params = array()
));
}

public static function pipe(Pipe $pipe) : Pipe
{
return self::getPipelineService()->addPipe($pipe);
}

/**
* Run a before callable function
* @param string $name
Expand Down Expand Up @@ -93,5 +102,10 @@ public static function addBefore($name, $callable) {
public static function getRoutes() {
return self::$_routes;
}

protected static function getPipelineService() : PipelineService
{
return Virge::service(PipelineService::class);
}

}

0 comments on commit 5e758cf

Please sign in to comment.