1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
MIT License |
4
|
|
|
Copyright (c) 2010 - 2018 Peter Petermann |
5
|
|
|
|
6
|
|
|
Permission is hereby granted, free of charge, to any person |
7
|
|
|
obtaining a copy of this software and associated documentation |
8
|
|
|
files (the "Software"), to deal in the Software without |
9
|
|
|
restriction, including without limitation the rights to use, |
10
|
|
|
copy, modify, merge, publish, distribute, sublicense, and/or sell |
11
|
|
|
copies of the Software, and to permit persons to whom the |
12
|
|
|
Software is furnished to do so, subject to the following |
13
|
|
|
conditions: |
14
|
|
|
|
15
|
|
|
The above copyright notice and this permission notice shall be |
16
|
|
|
included in all copies or substantial portions of the Software. |
17
|
|
|
|
18
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
19
|
|
|
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
20
|
|
|
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
21
|
|
|
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
22
|
|
|
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
23
|
|
|
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
24
|
|
|
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
25
|
|
|
OTHER DEALINGS IN THE SOFTWARE. |
26
|
|
|
|
27
|
|
|
*/ |
28
|
|
|
namespace King23\TwigIntegration; |
29
|
|
|
|
30
|
|
|
use King23\Controller\Controller; |
31
|
|
|
use Psr\Http\Message\ResponseFactoryInterface; |
32
|
|
|
use Psr\Http\Message\ResponseInterface; |
33
|
|
|
use Psr\Log\LoggerInterface; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Base controller for all controllers which need to use the Twig Templates |
37
|
|
|
*/ |
38
|
|
|
abstract class TwigController extends Controller |
39
|
|
|
{ |
40
|
|
|
/** |
41
|
|
|
* twig environment object, pulled from registry->twig |
42
|
|
|
*/ |
43
|
|
|
private $twig; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* assoc array containing all vars to use in template |
47
|
|
|
* |
48
|
|
|
* @var array |
49
|
|
|
*/ |
50
|
|
|
protected $_context = array(); |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var LoggerInterface |
54
|
|
|
*/ |
55
|
|
|
protected $log; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var ResponseFactoryInterface |
59
|
|
|
*/ |
60
|
|
|
protected $responseFactory; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* public contructor, call this from all derived classes |
64
|
|
|
* |
65
|
|
|
* @param TwigInterface $twig |
66
|
|
|
* @param LoggerInterface $log |
67
|
|
|
* @param ResponseFactoryInterface $responseFactory |
68
|
|
|
*/ |
69
|
|
|
public function __construct( |
70
|
|
|
TwigInterface $twig, |
71
|
|
|
LoggerInterface $log, |
72
|
|
|
ResponseFactoryInterface $responseFactory |
73
|
|
|
) { |
74
|
|
|
$this->twig = $twig; |
75
|
|
|
$this->log = $log; |
76
|
|
|
$this->responseFactory = $responseFactory; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return LoggerInterface |
81
|
|
|
*/ |
82
|
|
|
protected function getLogger() |
83
|
|
|
{ |
84
|
|
|
return $this->log; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* render template with context, will merge context with allready known |
89
|
|
|
* context |
90
|
|
|
* |
91
|
|
|
* @param string $template |
92
|
|
|
* @param array $context |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
|
|
protected function render($template, $context = []) |
96
|
|
|
{ |
97
|
|
|
$context = array_merge($this->_context, $context); |
98
|
|
|
$body = $this->twig->render($template, $context); |
99
|
|
|
return $body; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param string $template |
104
|
|
|
* @param array $context |
105
|
|
|
* @return ResponseInterface |
106
|
|
|
*/ |
107
|
|
|
protected function renderResponse(string $template, array $context = []) : ResponseInterface |
108
|
|
|
{ |
109
|
|
|
$body = $this->render($template, $context); |
110
|
|
|
$response = $this->responseFactory->createResponse(); |
111
|
|
|
$response->getBody()->write($body); |
112
|
|
|
return $response; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|