|
1
|
|
|
<?php |
|
2
|
|
|
namespace Germania\PsrWebsites; |
|
3
|
|
|
|
|
4
|
|
|
use Psr\Log\LoggerInterface; |
|
5
|
|
|
use Psr\Log\NullLogger; |
|
6
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request; |
|
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
8
|
|
|
|
|
9
|
|
|
class WebsiteController |
|
10
|
|
|
{ |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @var Callable |
|
14
|
|
|
*/ |
|
15
|
|
|
public $render; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var array |
|
19
|
|
|
*/ |
|
20
|
|
|
public $defaults = array(); |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var LoggerInterface |
|
24
|
|
|
*/ |
|
25
|
|
|
public $logger; |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param Callable $render Rendering callable that receives template file and variables context |
|
30
|
|
|
* @param array $defaults Default variables context |
|
31
|
|
|
* @param LoggerInterface|null $logger Optional: PSR-3 Logger |
|
32
|
|
|
*/ |
|
33
|
2 |
|
public function __construct( Callable $render, array $defaults, LoggerInterface $logger = null) |
|
34
|
|
|
{ |
|
35
|
2 |
|
$this->render = $render; |
|
36
|
2 |
|
$this->defaults = $defaults; |
|
37
|
2 |
|
$this->logger = $logger ?: new NullLogger; |
|
38
|
2 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request PSR7 request |
|
44
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response PSR7 response |
|
45
|
|
|
* |
|
46
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
|
47
|
|
|
*/ |
|
48
|
1 |
|
public function __invoke(Request $request, ResponseInterface $response, $args) { |
|
49
|
|
|
|
|
50
|
|
|
// Shortcuts |
|
51
|
1 |
|
$logger = $this->logger; |
|
52
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
// --------------------------------------- |
|
55
|
|
|
// 1. Retrieve current website object |
|
56
|
|
|
// --------------------------------------- |
|
57
|
1 |
|
$website = $request->getAttribute('website'); |
|
58
|
1 |
|
$website_content_file = $website->getContentFile(); |
|
59
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
// --------------------------------------- |
|
62
|
|
|
// 2. Render page content include |
|
63
|
|
|
// --------------------------------------- |
|
64
|
1 |
|
$logger->debug("Render page content…"); |
|
65
|
|
|
|
|
66
|
1 |
|
$render = $this->render; |
|
67
|
1 |
|
$content = $render( $website_content_file, array_merge( |
|
68
|
1 |
|
$this->defaults, [ |
|
69
|
1 |
|
'request' => $request, |
|
70
|
1 |
|
'response' => $response, |
|
71
|
1 |
|
'args' => $args, |
|
72
|
|
|
'logger' => $logger |
|
73
|
1 |
|
] |
|
74
|
1 |
|
)); |
|
75
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
// --------------------------------------- |
|
78
|
|
|
// 3. Write response |
|
79
|
|
|
// --------------------------------------- |
|
80
|
|
|
|
|
81
|
|
|
$log_info = [ |
|
82
|
|
|
'file' => $website_content_file |
|
83
|
1 |
|
]; |
|
84
|
|
|
|
|
85
|
1 |
|
if ($content instanceOf ResponseInterface): |
|
86
|
|
|
$logger->debug("Finish page content: result was ResponseInterface instance", $log_info); |
|
87
|
|
|
return $content; |
|
88
|
|
|
endif; |
|
89
|
|
|
|
|
90
|
1 |
|
$logger->debug("Finish page content: result is string, write to response", $log_info); |
|
91
|
1 |
|
$response->write( $content ); |
|
92
|
1 |
|
return $response; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|