|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* PsrAjaxMiddleware.php |
|
5
|
|
|
* |
|
6
|
|
|
* A Psr7 middleware to process Jaxon ajax requests. |
|
7
|
|
|
* |
|
8
|
|
|
* @package jaxon-core |
|
|
|
|
|
|
9
|
|
|
* @author Thierry Feuzeu <[email protected]> |
|
10
|
|
|
* @copyright 2022 Thierry Feuzeu <[email protected]> |
|
11
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License |
|
12
|
|
|
* @link https://github.com/jaxon-php/jaxon-core |
|
13
|
|
|
*/ |
|
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
namespace Jaxon\Request\Handler\Psr; |
|
16
|
|
|
|
|
17
|
|
|
use Jaxon\Di\Container; |
|
18
|
|
|
use Jaxon\Exception\RequestException; |
|
19
|
|
|
use Jaxon\Request\Handler\RequestHandler; |
|
20
|
|
|
use Jaxon\Response\Manager\ResponseManager; |
|
21
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
22
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
23
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
|
24
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
|
25
|
|
|
|
|
26
|
|
|
class PsrAjaxMiddleware implements MiddlewareInterface |
|
|
|
|
|
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var Container |
|
30
|
|
|
*/ |
|
31
|
|
|
private $di; |
|
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var RequestHandler |
|
35
|
|
|
*/ |
|
36
|
|
|
private $xRequestHandler; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var ResponseManager |
|
40
|
|
|
*/ |
|
41
|
|
|
private $xResponseManager; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* The constructor |
|
45
|
|
|
* |
|
46
|
|
|
* @param Container $di |
|
|
|
|
|
|
47
|
|
|
* @param RequestHandler $xRequestHandler |
|
|
|
|
|
|
48
|
|
|
* @param ResponseManager $xResponseManager |
|
|
|
|
|
|
49
|
|
|
*/ |
|
50
|
|
|
public function __construct(Container $di, RequestHandler $xRequestHandler, ResponseManager $xResponseManager) |
|
|
|
|
|
|
51
|
|
|
{ |
|
52
|
|
|
$this->di = $di; |
|
|
|
|
|
|
53
|
|
|
$this->xRequestHandler = $xRequestHandler; |
|
54
|
|
|
$this->xResponseManager = $xResponseManager; |
|
55
|
|
|
} |
|
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
/** |
|
|
|
|
|
|
58
|
|
|
* @inheritDoc |
|
59
|
|
|
* @throws RequestException |
|
60
|
|
|
*/ |
|
|
|
|
|
|
61
|
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
|
62
|
|
|
{ |
|
63
|
|
|
// Save the request in the container. This will override the default request, |
|
64
|
|
|
// and the other classes will get this request from there. |
|
65
|
|
|
$this->di->val(ServerRequestInterface::class, $request); |
|
66
|
|
|
|
|
67
|
|
|
if(!$this->xRequestHandler->canProcessRequest()) |
|
68
|
|
|
{ |
|
69
|
|
|
// Unable to find a plugin to process the request |
|
70
|
|
|
return $handler->handle($request); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
// Process the request |
|
74
|
|
|
$this->xRequestHandler->processRequest(); |
|
75
|
|
|
// Return a Psr7 response |
|
76
|
|
|
return $this->xResponseManager->getResponse()->toPsr(); |
|
77
|
|
|
} |
|
|
|
|
|
|
78
|
|
|
} |
|
79
|
|
|
|