Permalink
Browse files

Rewrite server to use PSR-7 concentric middleware logic.

  • Loading branch information...
1 parent 4a23ca0 commit 3b0dda0734b94dca3b1c5ea8154bff51c36a1a76 @cdujeu cdujeu committed May 9, 2016
@@ -121,6 +121,25 @@ public static function parseRestParameters(ServerRequestInterface &$request){
}
/**
+ * @param ServerRequestInterface $request
+ * @param ResponseInterface $response
+ * @param callable|null $nextCallable
+ * @return ResponseInterface
+ * @throws AuthRequiredException
+ */
+ public static function registryActionMiddleware(ServerRequestInterface &$request, ResponseInterface &$response, callable $nextCallable = null){
+ $action = null;
+ if(ConfService::currentContextIsRestAPI()){
+ $action = Controller::parseRestParameters($request);
+ }
+ $response = Controller::run($request, $action);
+ if($nextCallable != null){
+ $response = call_user_func($nextCallable, $request, $response);
+ }
+ return $response;
+ }
+
+ /**
* @static
* @param array $parameters
* @param \DOMNode $callbackNode
@@ -20,13 +20,14 @@
*/
namespace Pydio\Core\Http;
+use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
-use Pydio\Core\Controller\Controller;
use Pydio\Core\Exception\PydioException;
use Pydio\Core\PluginFramework\PluginsService;
use Pydio\Core\Services\AuthService;
use Pydio\Core\Services\ConfService;
use Pydio\Core\Utils\Utils;
+use Zend\Diactoros\Response;
use Zend\Diactoros\ServerRequestFactory;
defined('AJXP_EXEC') or die('Access not allowed');
@@ -39,12 +40,20 @@ class Server
private $mode;
private $request;
private $requireAuth = false;
+ private $middleWares;
public function __construct($serverMode = PYDIO_SERVER_MODE_SESSION){
+
$this->mode = $serverMode;
if($this->mode == PYDIO_SERVER_MODE_REST){
$this->requireAuth = true;
}
+ $this->middleWares = new \SplStack();
+ $this->middleWares->setIteratorMode(\SplDoublyLinkedList::IT_MODE_LIFO | \SplDoublyLinkedList::IT_MODE_KEEP);
+
+ $this->middleWares->push(array("Pydio\\Core\\Controller\\Controller", "registryActionMiddleware"));
+ $this->middleWares->push(array($this, "simpleEmitterMiddleware"));
+
}
public function getRequest(){
@@ -54,15 +63,33 @@ public function getRequest(){
return $this->request;
}
-
- public function listen(){
-
- $action = null;
- if($this->mode == PYDIO_SERVER_MODE_REST){
- $action = Controller::parseRestParameters($this->request);
+ /**
+ * @param ServerRequestInterface $request
+ * @param ResponseInterface $response
+ * @return ResponseInterface
+ */
+ protected function nextCallable(&$request, &$response){
+ if($this->middleWares->valid()){
+ $callable = $this->middleWares->current();
+ $this->middleWares->next();
+ $response = call_user_func($callable, $request, $response, function($req, $res){
+ return $this->nextCallable($req, $res);
+ });
}
+ return $response;
+ }
+
+ /**
+ * @param $request
+ * @param $response
+ * @param callable|null $next
+ * @throws \Pydio\Core\Exception\AuthRequiredException
+ */
+ public function simpleEmitterMiddleware($request, $response, $next = null){
try{
- $response = Controller::run($this->getRequest(), $action);
+ if($next !== null){
+ $response = call_user_func($next, $request, $response);
+ }
if($response !== false && ($response->getBody()->getSize() || $response instanceof \Zend\Diactoros\Response\EmptyResponse)) {
$emitter = new \Zend\Diactoros\Response\SapiEmitter();
$emitter->emit($response);
@@ -72,6 +99,17 @@ public function listen(){
throw $authExc;
}
}
+ }
+
+ public function addMiddleware(callable $middleWareCallable){
+ $this->middleWares->push($middleWareCallable);
+ }
+
+ public function listen(){
+
+ $response = new Response();
+ $this->middleWares->rewind();
+ $this->nextCallable($this->request, $response);
}

0 comments on commit 3b0dda0

Please sign in to comment.