1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Response.php - The Jaxon Response |
5
|
|
|
* |
6
|
|
|
* This class collects commands to be sent back to the browser in response to a jaxon request. |
7
|
|
|
* Commands are encoded and packaged in json format. |
8
|
|
|
* |
9
|
|
|
* @package jaxon-core |
|
|
|
|
10
|
|
|
* @author Jared White |
|
|
|
|
11
|
|
|
* @author J. Max Wilson |
|
|
|
|
12
|
|
|
* @author Joseph Woolley |
|
|
|
|
13
|
|
|
* @author Steffen Konerow |
|
|
|
|
14
|
|
|
* @author Thierry Feuzeu <[email protected]> |
15
|
|
|
* @copyright Copyright (c) 2005-2007 by Jared White & J. Max Wilson |
|
|
|
|
16
|
|
|
* @copyright Copyright (c) 2008-2010 by Joseph Woolley, Steffen Konerow, Jared White & J. Max Wilson |
|
|
|
|
17
|
|
|
* @copyright 2016 Thierry Feuzeu <[email protected]> |
18
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License |
19
|
|
|
* @link https://github.com/jaxon-php/jaxon-core |
20
|
|
|
*/ |
|
|
|
|
21
|
|
|
|
22
|
|
|
namespace Jaxon\Response; |
23
|
|
|
|
24
|
|
|
use Jaxon\App\Dialog\DialogManager; |
25
|
|
|
use Jaxon\Plugin\Manager\PluginManager; |
26
|
|
|
use Jaxon\Plugin\Response\DataBag\DataBagContext; |
27
|
|
|
use Jaxon\Plugin\Response\DataBag\DataBagPlugin; |
28
|
|
|
use Jaxon\Plugin\Response\JQuery\DomSelector; |
29
|
|
|
use Jaxon\Plugin\Response\JQuery\JQueryPlugin; |
30
|
|
|
use Jaxon\Plugin\Response\Pagination\Paginator; |
31
|
|
|
use Jaxon\Plugin\Response\Pagination\PaginatorPlugin; |
32
|
|
|
use Jaxon\Plugin\ResponsePlugin; |
33
|
|
|
use Nyholm\Psr7\Factory\Psr17Factory; |
34
|
|
|
use Nyholm\Psr7\Stream; |
35
|
|
|
use Psr\Http\Message\ResponseInterface as PsrResponseInterface; |
36
|
|
|
use Psr\Http\Message\ServerRequestInterface as PsrRequestInterface; |
37
|
|
|
|
38
|
|
|
use function gmdate; |
39
|
|
|
use function json_encode; |
40
|
|
|
use function trim; |
41
|
|
|
|
42
|
|
|
class Response implements ResponseInterface |
|
|
|
|
43
|
|
|
{ |
44
|
|
|
use Traits\CommandTrait; |
45
|
|
|
use Traits\HtmlDomTrait; |
46
|
|
|
use Traits\ScriptTrait; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var PluginManager |
50
|
|
|
*/ |
51
|
|
|
protected $xPluginManager; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var DialogManager |
55
|
|
|
*/ |
56
|
|
|
protected $xDialogManager; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var Psr17Factory |
60
|
|
|
*/ |
61
|
|
|
protected $xPsr17Factory; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var PsrRequestInterface |
65
|
|
|
*/ |
66
|
|
|
protected $xRequest; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* The constructor |
70
|
|
|
* |
71
|
|
|
* @param Psr17Factory $xPsr17Factory |
|
|
|
|
72
|
|
|
* @param PsrRequestInterface $xRequest |
|
|
|
|
73
|
|
|
* @param PluginManager $xPluginManager |
|
|
|
|
74
|
|
|
* @param DialogManager $xDialogManager |
|
|
|
|
75
|
|
|
*/ |
76
|
|
|
public function __construct(Psr17Factory $xPsr17Factory, PsrRequestInterface $xRequest, |
|
|
|
|
77
|
|
|
PluginManager $xPluginManager, DialogManager $xDialogManager) |
78
|
|
|
{ |
79
|
|
|
$this->xPsr17Factory = $xPsr17Factory; |
80
|
|
|
$this->xRequest = $xRequest; |
|
|
|
|
81
|
|
|
$this->xPluginManager = $xPluginManager; |
82
|
|
|
$this->xDialogManager = $xDialogManager; |
83
|
|
|
} |
|
|
|
|
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return DialogManager |
87
|
|
|
*/ |
88
|
|
|
protected function dialog(): DialogManager |
89
|
|
|
{ |
90
|
|
|
return $this->xDialogManager; |
91
|
|
|
} |
|
|
|
|
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @inheritDoc |
95
|
|
|
*/ |
|
|
|
|
96
|
|
|
public function getContentType(): string |
97
|
|
|
{ |
98
|
|
|
return 'application/json'; |
99
|
|
|
} |
|
|
|
|
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @inheritDoc |
103
|
|
|
*/ |
|
|
|
|
104
|
|
|
public function getOutput(): string |
105
|
|
|
{ |
106
|
|
|
return json_encode(['jxn' => ['commands' => $this->aCommands]]); |
107
|
|
|
} |
|
|
|
|
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Provides access to registered response plugins |
111
|
|
|
* |
112
|
|
|
* Pass the plugin name as the first argument and the plugin object will be returned. |
113
|
|
|
* |
114
|
|
|
* @param string $sName The name of the plugin |
|
|
|
|
115
|
|
|
* |
116
|
|
|
* @return null|ResponsePlugin |
117
|
|
|
*/ |
118
|
|
|
public function plugin(string $sName): ?ResponsePlugin |
119
|
|
|
{ |
120
|
|
|
return $this->xPluginManager->getResponsePlugin($sName, $this); |
121
|
|
|
} |
|
|
|
|
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Magic PHP function |
125
|
|
|
* |
126
|
|
|
* Used to permit plugins to be called as if they were native members of the Response instance. |
127
|
|
|
* |
128
|
|
|
* @param string $sPluginName The name of the plugin |
|
|
|
|
129
|
|
|
* |
130
|
|
|
* @return null|ResponsePlugin |
131
|
|
|
*/ |
132
|
|
|
public function __get(string $sPluginName) |
133
|
|
|
{ |
134
|
|
|
return $this->plugin($sPluginName); |
135
|
|
|
} |
|
|
|
|
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Convert to string |
139
|
|
|
* |
140
|
|
|
* @param mixed $xData |
|
|
|
|
141
|
|
|
* |
142
|
|
|
* @return string |
143
|
|
|
*/ |
144
|
|
|
protected function str($xData): string |
145
|
|
|
{ |
146
|
|
|
return trim((string)$xData, " \t\n"); |
147
|
|
|
} |
|
|
|
|
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Create a JQuery DomSelector, and link it to the current response. |
151
|
|
|
* |
152
|
|
|
* This is a shortcut to the JQuery plugin. |
153
|
|
|
* |
154
|
|
|
* @param string $sPath The jQuery selector path |
155
|
|
|
* @param mixed $xContext A context associated to the selector |
|
|
|
|
156
|
|
|
* |
157
|
|
|
* @return DomSelector |
158
|
|
|
*/ |
159
|
|
|
public function jq(string $sPath = '', $xContext = null): DomSelector |
160
|
|
|
{ |
161
|
|
|
/** @var JQueryPlugin */ |
|
|
|
|
162
|
|
|
$xPlugin = $this->plugin('pg'); |
163
|
|
|
return $xPlugin->selector($sPath, $xContext); |
164
|
|
|
} |
|
|
|
|
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Get the databag with a given name |
168
|
|
|
* |
169
|
|
|
* @param string $sName |
|
|
|
|
170
|
|
|
* |
171
|
|
|
* @return DataBagContext |
172
|
|
|
*/ |
173
|
|
|
public function bag(string $sName): DataBagContext |
174
|
|
|
{ |
175
|
|
|
/** @var DataBagPlugin */ |
|
|
|
|
176
|
|
|
$xPlugin = $this->plugin('pg'); |
177
|
|
|
return $xPlugin->bag($sName); |
178
|
|
|
} |
|
|
|
|
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Render an HTML pagination control. |
182
|
|
|
* |
183
|
|
|
* @param int $nCurrentPage The current page number |
|
|
|
|
184
|
|
|
* @param int $nItemsPerPage The number of items per page |
|
|
|
|
185
|
|
|
* @param int $nTotalItems The total number of items |
|
|
|
|
186
|
|
|
* |
187
|
|
|
* @return Paginator |
188
|
|
|
*/ |
189
|
|
|
public function paginator(int $nCurrentPage, int $nItemsPerPage, int $nTotalItems): Paginator |
190
|
|
|
{ |
191
|
|
|
/** @var PaginatorPlugin */ |
|
|
|
|
192
|
|
|
$xPlugin = $this->plugin('pg'); |
193
|
|
|
return $xPlugin->paginator($nCurrentPage, $nItemsPerPage, $nTotalItems); |
|
|
|
|
194
|
|
|
} |
|
|
|
|
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Convert this response to a PSR7 response object |
198
|
|
|
* |
199
|
|
|
* @return PsrResponseInterface |
200
|
|
|
*/ |
201
|
|
|
public function toPsr(): PsrResponseInterface |
202
|
|
|
{ |
203
|
|
|
$xPsrResponse = $this->xPsr17Factory->createResponse(200); |
204
|
|
|
if($this->xRequest->getMethod() === 'GET') |
205
|
|
|
{ |
206
|
|
|
$xPsrResponse = $xPsrResponse |
207
|
|
|
->withHeader('Expires', 'Mon, 26 Jul 1997 05:00:00 GMT') |
|
|
|
|
208
|
|
|
->withHeader('Last-Modified', gmdate("D, d M Y H:i:s") . ' GMT') |
|
|
|
|
209
|
|
|
->withHeader('Cache-Control', 'no-cache, must-revalidate') |
|
|
|
|
210
|
|
|
->withHeader('Pragma', 'no-cache'); |
|
|
|
|
211
|
|
|
} |
212
|
|
|
return $xPsrResponse |
213
|
|
|
->withHeader('content-type', $this->getContentType()) |
|
|
|
|
214
|
|
|
->withBody(Stream::create($this->getOutput())); |
|
|
|
|
215
|
|
|
} |
|
|
|
|
216
|
|
|
} |
217
|
|
|
|