-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fc2ed50
commit bdc7499
Showing
4 changed files
with
88 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
/.idea | ||
|
||
/vendor/ | ||
composer.lock |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,6 @@ | ||
<?php | ||
<?php | ||
|
||
require 'vendor/autoload.php'; | ||
|
||
use function Http\Response\send; | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
namespace DuskPHP; | ||
|
||
use GuzzleHttp\Psr7\Response; | ||
use Interop\Http\ServerMiddleware\DelegateInterface; | ||
use Interop\Http\ServerMiddleware\MiddlewareInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
|
||
/** | ||
* Class Dispacher | ||
* @package DuskPHP | ||
*/ | ||
class Dispatcher implements DelegateInterface | ||
{ | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $middlewares = []; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
private $index = 0; | ||
|
||
private $response; | ||
|
||
function __construct() | ||
{ | ||
$this->response = new Response(); | ||
} | ||
|
||
/** | ||
* Save a new middleware | ||
* | ||
* @param callable|MiddlewareInterface $middleware | ||
*/ | ||
public function pipe($middleware) | ||
{ | ||
$this->middlewares[] =$middleware; | ||
} | ||
|
||
/** | ||
* Dispatch the next available middleware and return the response. | ||
* | ||
* @param ServerRequestInterface $request | ||
* | ||
* @return ResponseInterface | ||
*/ | ||
public function process(ServerRequestInterface $request) | ||
{ | ||
$middleware = $this->getMiddleware(); | ||
$this->index++; | ||
if (is_null($middleware)) | ||
return $this->response; | ||
if(is_callable($middleware)) | ||
return $middleware($request, $this->response, [$this, 'process']); | ||
if ($middleware instanceof MiddlewareInterface) | ||
return $middleware->process($request, $this); | ||
} | ||
|
||
private function getMiddleware() { | ||
if (isset($this->middlewares[$this->index])) | ||
return $this->middlewares[$this->index]; | ||
return null; | ||
} | ||
|
||
} |