|
1
|
|
|
<?php |
|
2
|
|
|
|
|
|
|
|
|
|
3
|
|
|
namespace Jaxon\Di\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Jaxon\Di\Container; |
|
6
|
|
|
use Jaxon\Request\Factory\Psr\PsrFactory; |
|
7
|
|
|
use Jaxon\Request\Handler\Psr\PsrAjaxMiddleware; |
|
8
|
|
|
use Jaxon\Request\Handler\Psr\PsrConfigMiddleware; |
|
9
|
|
|
use Jaxon\Request\Handler\Psr\PsrRequestHandler; |
|
10
|
|
|
use Jaxon\Request\Handler\RequestHandler; |
|
11
|
|
|
use Jaxon\Response\Manager\ResponseManager; |
|
12
|
|
|
use Jaxon\Utils\Translation\Translator; |
|
13
|
|
|
use Nyholm\Psr7\Factory\Psr17Factory; |
|
14
|
|
|
use Nyholm\Psr7Server\ServerRequestCreator; |
|
15
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
16
|
|
|
|
|
17
|
|
|
trait PsrTrait |
|
|
|
|
|
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
private $sPsrConfig = 'jaxon.psr.config.file'; |
|
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Register the values into the container |
|
26
|
|
|
* |
|
27
|
|
|
* @return void |
|
28
|
|
|
*/ |
|
29
|
|
|
private function registerPsr() |
|
|
|
|
|
|
30
|
|
|
{ |
|
31
|
|
|
// The server request |
|
32
|
|
|
$this->set(Psr17Factory::class, function() { |
|
|
|
|
|
|
33
|
|
|
return new Psr17Factory(); |
|
34
|
|
|
}); |
|
35
|
|
|
$this->set(ServerRequestCreator::class, function($c) { |
|
36
|
|
|
$xRequestFactory = $c->g(Psr17Factory::class); |
|
37
|
|
|
return new ServerRequestCreator( |
|
38
|
|
|
$xRequestFactory, // ServerRequestFactory |
|
39
|
|
|
$xRequestFactory, // UriFactory |
|
40
|
|
|
$xRequestFactory, // UploadedFileFactory |
|
41
|
|
|
$xRequestFactory // StreamFactory |
|
42
|
|
|
); |
|
43
|
|
|
}); |
|
44
|
|
|
$this->set(ServerRequestInterface::class, function($c) { |
|
45
|
|
|
return $c->g(ServerRequestCreator::class)->fromGlobals(); |
|
46
|
|
|
}); |
|
47
|
|
|
// PSR factory |
|
48
|
|
|
$this->set(PsrFactory::class, function($c) { |
|
49
|
|
|
return new PsrFactory($c->g(Container::class)); |
|
50
|
|
|
}); |
|
51
|
|
|
// PSR request handler |
|
52
|
|
|
$this->set(PsrRequestHandler::class, function($c) { |
|
53
|
|
|
return new PsrRequestHandler($c->g(Container::class), $c->g(RequestHandler::class), |
|
54
|
|
|
$c->g(ResponseManager::class), $c->g(Translator::class)); |
|
55
|
|
|
}); |
|
56
|
|
|
// PSR config middleware |
|
57
|
|
|
$this->set(PsrConfigMiddleware::class, function($c) { |
|
58
|
|
|
return new PsrConfigMiddleware($c->g(Container::class), $c->g($this->sPsrConfig)); |
|
59
|
|
|
}); |
|
60
|
|
|
// PSR ajax middleware |
|
61
|
|
|
$this->set(PsrAjaxMiddleware::class, function($c) { |
|
62
|
|
|
return new PsrAjaxMiddleware($c->g(Container::class), $c->g(RequestHandler::class), |
|
63
|
|
|
$c->g(ResponseManager::class)); |
|
64
|
|
|
}); |
|
65
|
|
|
} |
|
|
|
|
|
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Get the request |
|
69
|
|
|
* |
|
70
|
|
|
* @return ServerRequestInterface |
|
71
|
|
|
*/ |
|
72
|
|
|
public function getRequest(): ServerRequestInterface |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->g(ServerRequestInterface::class); |
|
|
|
|
|
|
75
|
|
|
} |
|
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Get the PSR factory |
|
79
|
|
|
* |
|
80
|
|
|
* @return PsrFactory |
|
81
|
|
|
*/ |
|
82
|
|
|
public function getPsrFactory(): PsrFactory |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->g(PsrFactory::class); |
|
85
|
|
|
} |
|
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Get the PSR request handler |
|
89
|
|
|
* |
|
90
|
|
|
* @return PsrRequestHandler |
|
91
|
|
|
*/ |
|
92
|
|
|
public function getPsrRequestHandler(): PsrRequestHandler |
|
93
|
|
|
{ |
|
94
|
|
|
return $this->g(PsrRequestHandler::class); |
|
95
|
|
|
} |
|
|
|
|
|
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Get the PSR config middleware |
|
99
|
|
|
* |
|
100
|
|
|
* @param string $sConfigFile |
|
|
|
|
|
|
101
|
|
|
* |
|
102
|
|
|
* @return PsrConfigMiddleware |
|
103
|
|
|
*/ |
|
104
|
|
|
public function getPsrConfigMiddleware(string $sConfigFile): PsrConfigMiddleware |
|
105
|
|
|
{ |
|
106
|
|
|
$this->val($this->sPsrConfig, $sConfigFile); |
|
|
|
|
|
|
107
|
|
|
return $this->g(PsrConfigMiddleware::class); |
|
108
|
|
|
} |
|
|
|
|
|
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Get the PSR ajax request middleware |
|
112
|
|
|
* |
|
113
|
|
|
* @return PsrAjaxMiddleware |
|
114
|
|
|
*/ |
|
115
|
|
|
public function getPsrAjaxMiddleware(): PsrAjaxMiddleware |
|
116
|
|
|
{ |
|
117
|
|
|
return $this->g(PsrAjaxMiddleware::class); |
|
118
|
|
|
} |
|
|
|
|
|
|
119
|
|
|
} |
|
120
|
|
|
|