|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* apparat-server |
|
5
|
|
|
* |
|
6
|
|
|
* @category Apparat |
|
7
|
|
|
* @package Apparat\Server |
|
8
|
|
|
* @subpackage Apparat\Server\Ports |
|
9
|
|
|
* @author Joschi Kuphal <[email protected]> / @jkphl |
|
10
|
|
|
* @copyright Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl |
|
11
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
/*********************************************************************************** |
|
15
|
|
|
* The MIT License (MIT) |
|
16
|
|
|
* |
|
17
|
|
|
* Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl |
|
18
|
|
|
* |
|
19
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of |
|
20
|
|
|
* this software and associated documentation files (the "Software"), to deal in |
|
21
|
|
|
* the Software without restriction, including without limitation the rights to |
|
22
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
|
23
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so, |
|
24
|
|
|
* subject to the following conditions: |
|
25
|
|
|
* |
|
26
|
|
|
* The above copyright notice and this permission notice shall be included in all |
|
27
|
|
|
* copies or substantial portions of the Software. |
|
28
|
|
|
* |
|
29
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
30
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
|
31
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
|
32
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
|
33
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
|
34
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
|
35
|
|
|
***********************************************************************************/ |
|
36
|
|
|
|
|
37
|
|
|
namespace Apparat\Server\Ports\Responder; |
|
38
|
|
|
|
|
39
|
|
|
use Apparat\Kernel\Ports\Kernel; |
|
40
|
|
|
use Apparat\Server\Application\Factory\PayloadFactory; |
|
41
|
|
|
use Apparat\Server\Domain\Payload\PayloadInterface; |
|
42
|
|
|
use Apparat\Server\Ports\View\ViewInterface; |
|
43
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Abstract responder |
|
47
|
|
|
* |
|
48
|
|
|
* @package Apparat\Server |
|
49
|
|
|
* @subpackage Apparat\Server\Ports |
|
50
|
|
|
*/ |
|
51
|
|
|
abstract class AbstractResponder implements ResponderInterface |
|
52
|
|
|
{ |
|
53
|
|
|
/** |
|
54
|
|
|
* Action name |
|
55
|
|
|
* |
|
56
|
|
|
* @var string |
|
57
|
|
|
*/ |
|
58
|
|
|
const ACTION = 'Default/Abstract'; |
|
59
|
|
|
/** |
|
60
|
|
|
* View |
|
61
|
|
|
* |
|
62
|
|
|
* @var ViewInterface |
|
63
|
|
|
*/ |
|
64
|
|
|
protected $view; |
|
65
|
|
|
/** |
|
66
|
|
|
* Response |
|
67
|
|
|
* |
|
68
|
|
|
* @var ResponseInterface |
|
69
|
|
|
*/ |
|
70
|
|
|
protected $response; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Constructor |
|
74
|
|
|
* |
|
75
|
|
|
* @param ResponseInterface $response |
|
76
|
|
|
* @param ViewInterface $view |
|
77
|
|
|
*/ |
|
78
|
79 |
|
public function __construct(ResponseInterface $response, ViewInterface $view) |
|
79
|
|
|
{ |
|
80
|
79 |
|
$this->response = $response; |
|
81
|
79 |
|
$this->view = $view->setAction(static::ACTION); |
|
82
|
79 |
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Run the responder and process the payload |
|
86
|
|
|
* |
|
87
|
|
|
* @param PayloadInterface $payload Domain payload |
|
88
|
|
|
* @return ResponseInterface Response |
|
89
|
|
|
* @see https://github.com/pmjones/adr/blob/master/example-code/Web/AbstractResponder.php |
|
90
|
|
|
* @see https://github.com/pmjones/adr/blob/master/example-code/Web/Blog/Responder/BlogBrowseResponder.php |
|
91
|
|
|
*/ |
|
92
|
6 |
|
public function __invoke(PayloadInterface $payload) |
|
93
|
|
|
{ |
|
94
|
6 |
|
$payloadClass = (new \ReflectionClass($payload))->getShortName(); |
|
95
|
6 |
|
$method = strtolower($payloadClass); |
|
96
|
|
|
|
|
97
|
|
|
// If there's no processor for this type of payload |
|
98
|
6 |
|
if (!is_callable([$this, $method])) { |
|
99
|
|
|
/** @var PayloadFactory $payloadFactory */ |
|
100
|
1 |
|
$payloadFactory = Kernel::create(PayloadFactory::class); |
|
101
|
1 |
|
$error = $payloadFactory->error(500, sprintf('Unrecognized payload type "%s"', $payloadClass)); |
|
102
|
1 |
|
return $this->error($error); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
5 |
|
return $this->$method($payload); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Process an error payload |
|
110
|
|
|
* |
|
111
|
|
|
* @param PayloadInterface $payload Error payload |
|
112
|
|
|
* @return ResponseInterface Response |
|
113
|
|
|
*/ |
|
114
|
7 |
|
protected function error(PayloadInterface $payload) |
|
115
|
|
|
{ |
|
116
|
|
|
// Ensure the error template is used |
|
117
|
7 |
|
$this->view->setAction('Error/'.(new \ReflectionClass($payload))->getShortName()); |
|
118
|
|
|
|
|
119
|
|
|
// Set the HTTP status code |
|
120
|
7 |
|
$this->response = $this->response->withStatus($payload->get('status'), $payload->get('description')); |
|
121
|
|
|
|
|
122
|
|
|
// Add HTTP headers |
|
123
|
7 |
|
foreach ((array)$payload->get('header') as $name => $value) { |
|
124
|
2 |
|
$this->response = $this->response->withHeader($name, $value); |
|
125
|
7 |
|
} |
|
126
|
|
|
|
|
127
|
7 |
|
$this->view->assign('error', $payload->get()); |
|
128
|
7 |
|
$this->response->getBody()->write($this->view->render()); |
|
129
|
7 |
|
return $this->response; |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|