|
1
|
|
|
<?php |
|
2
|
|
|
namespace Germania\Responder; |
|
3
|
|
|
|
|
4
|
|
|
use Twig\Environment as TwigEnvironment; |
|
5
|
|
|
|
|
6
|
|
|
use Slim\Psr7\Factory\ResponseFactory; |
|
7
|
|
|
use Psr\Http\Message\ResponseFactoryInterface; |
|
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
9
|
|
|
|
|
10
|
|
|
class TwigResponder implements ResponderInterface |
|
11
|
|
|
{ |
|
12
|
|
|
|
|
13
|
1 |
|
use ResponseFactoryTrait; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var TwigEnvironment |
|
17
|
|
|
*/ |
|
18
|
|
|
public $twig; |
|
19
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var strinfg |
|
23
|
|
|
*/ |
|
24
|
|
|
public $template_field = "template"; |
|
25
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var array |
|
29
|
|
|
*/ |
|
30
|
|
|
public $default_context = array(); |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var string|bool |
|
35
|
|
|
*/ |
|
36
|
|
|
public $default_template = false; |
|
37
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param TwigEnvironment $twig Twig environment |
|
42
|
|
|
* @param string $template_field Array field taht will contain template |
|
43
|
|
|
* @param array $default_context Default template context |
|
44
|
|
|
* @param ResponseFactoryInterface|null $response_factory Optional: PSR-17 Response Factory |
|
45
|
|
|
*/ |
|
46
|
2 |
|
public function __construct( TwigEnvironment $twig, string $template_field, array $default_context = array(), ResponseFactoryInterface $response_factory = null ) |
|
47
|
|
|
{ |
|
48
|
2 |
|
$this->setTwig($twig); |
|
49
|
2 |
|
$this->setTemplateField($template_field); |
|
50
|
2 |
|
$this->setDefaultContext($default_context); |
|
51
|
2 |
|
$this->setResponseFactory($response_factory ?: new ResponseFactory); |
|
52
|
2 |
|
} |
|
53
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param TwigEnvironment $twig |
|
58
|
|
|
*/ |
|
59
|
6 |
|
public function setTwig(TwigEnvironment $twig ) |
|
60
|
|
|
{ |
|
61
|
6 |
|
$this->twig = $twig; |
|
62
|
6 |
|
return $this; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @return mixed |
|
68
|
|
|
*/ |
|
69
|
|
|
public function getDefaultTemplate( ) |
|
70
|
|
|
{ |
|
71
|
|
|
return $this->default_template; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param mixed $default_template |
|
77
|
|
|
*/ |
|
78
|
|
|
public function setDefaultTemplate( $default_template ) |
|
79
|
|
|
{ |
|
80
|
|
|
$this->default_template = $default_template; |
|
81
|
|
|
return $this; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param array $default_context |
|
88
|
|
|
*/ |
|
89
|
6 |
|
public function setDefaultContext( array $default_context ) |
|
90
|
|
|
{ |
|
91
|
6 |
|
$this->default_context = $default_context; |
|
92
|
6 |
|
return $this; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param string $field |
|
98
|
|
|
*/ |
|
99
|
2 |
|
public function setTemplateField( string $field ) |
|
100
|
|
|
{ |
|
101
|
2 |
|
$this->template_field = $field; |
|
|
|
|
|
|
102
|
2 |
|
return $this; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
|
|
106
|
|
|
|
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @inheritDoc |
|
110
|
|
|
*/ |
|
111
|
|
|
public function __invoke( $context ) : ResponseInterface |
|
112
|
|
|
{ |
|
113
|
|
|
return $this->createResponse( $context ); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @inheritDoc |
|
119
|
|
|
* @param array|ArrayObject Context |
|
120
|
|
|
* |
|
121
|
|
|
* @throws ResponderInvalidArgumentException |
|
122
|
|
|
* @throws ResponderRuntimeException |
|
123
|
|
|
*/ |
|
124
|
8 |
|
public function createResponse( $context ) : ResponseInterface |
|
125
|
|
|
{ |
|
126
|
8 |
|
if ($context instanceof \ArrayObject): |
|
127
|
2 |
|
$context = $context->getArrayCopy(); |
|
128
|
6 |
|
elseif (!is_array($context)): |
|
129
|
4 |
|
$context_type = is_object($context) ? get_class($context) : gettype($context); |
|
130
|
4 |
|
$msg = sprintf( |
|
131
|
4 |
|
"Expected Array or ArrayObject, got '%s'.", |
|
132
|
|
|
$context_type |
|
133
|
|
|
); |
|
134
|
4 |
|
throw new ResponderInvalidArgumentException($msg); |
|
135
|
|
|
endif; |
|
136
|
|
|
|
|
137
|
|
|
|
|
138
|
|
|
// Merge with defaults |
|
139
|
4 |
|
$context = array_merge($this->default_context, $context); |
|
140
|
4 |
|
$template = $context[ $this->template_field ] ?? $this->getDefaultTemplate(); |
|
141
|
|
|
|
|
142
|
4 |
|
if (!$template) { |
|
143
|
|
|
$msg = sprintf("Expected '%s' element.", $this->template_field); |
|
144
|
|
|
throw new ResponderRuntimeException($msg); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
try { |
|
148
|
4 |
|
$website_html = $this->twig->render($template, $context); |
|
149
|
|
|
|
|
150
|
4 |
|
$response = $this->getResponseFactory()->createResponse(); |
|
151
|
4 |
|
$response->getBody()->write($website_html); |
|
152
|
|
|
} |
|
153
|
|
|
catch (\Throwable $e) { |
|
154
|
|
|
throw new ResponderRuntimeException("Caught exception during response creation", 1, $e); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
4 |
|
return $response; |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..