|
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 as Response; |
|
8
|
|
|
use Psr\Container\ContainerInterface; |
|
9
|
|
|
use Slim\Http\Body as ResponseBody; |
|
10
|
|
|
|
|
11
|
|
|
class WebsiteMiddleware |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var ContainerInterface |
|
16
|
|
|
*/ |
|
17
|
|
|
public $websites; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var Callable |
|
21
|
|
|
*/ |
|
22
|
|
|
public $render; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
public $template; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var array |
|
31
|
|
|
*/ |
|
32
|
|
|
public $defaults = array(); |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var LoggerInterface |
|
36
|
|
|
*/ |
|
37
|
|
|
public $logger; |
|
38
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param ContainerInterface $websites A ContainerInterface instance that returns a website for the current route name |
|
44
|
|
|
* @param Callable $render Rendering callable that receives template file and variables context |
|
45
|
|
|
* @param string $template Template file |
|
46
|
|
|
* @param array $defaults Default variables context |
|
47
|
|
|
* @param LoggerInterface $logger Optional: PSR-3 Logger |
|
48
|
|
|
*/ |
|
49
|
2 |
|
public function __construct( ContainerInterface $websites, Callable $render, $template, array $defaults, LoggerInterface $logger = null) |
|
50
|
|
|
{ |
|
51
|
2 |
|
$this->websites = $websites; |
|
52
|
2 |
|
$this->render = $render; |
|
53
|
2 |
|
$this->template = $template; |
|
54
|
2 |
|
$this->defaults = $defaults; |
|
55
|
|
|
|
|
56
|
2 |
|
$this->logger = $logger ?: new NullLogger; |
|
57
|
2 |
|
} |
|
58
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param Psr\Http\Message\ServerRequestInterface $request PSR7 request |
|
62
|
|
|
* @param Psr\Http\Message\ResponseInterface $response PSR7 response |
|
63
|
|
|
* @param callable $next Next middleware |
|
64
|
|
|
* |
|
65
|
|
|
* @return Psr\Http\Message\ResponseInterface |
|
66
|
|
|
*/ |
|
67
|
1 |
|
public function __invoke(Request $request, Response $response, $next) |
|
68
|
|
|
{ |
|
69
|
|
|
// --------------------------------------- |
|
70
|
|
|
// 1. Get current Website object, |
|
71
|
|
|
// add as attribute to Request. |
|
72
|
|
|
// The website will be available within route controllers. |
|
73
|
|
|
// --------------------------------------- |
|
74
|
1 |
|
$current_route_name = $request->getAttribute('route')->getName(); |
|
75
|
1 |
|
|
|
76
|
1 |
|
$website_factory = $this->websites; |
|
77
|
|
|
$website = $website_factory->get( $current_route_name ); |
|
78
|
1 |
|
|
|
79
|
|
|
$request = $request->withAttribute('website', $website); |
|
80
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
// --------------------------------------- |
|
83
|
|
|
// 2. Call next middleware. |
|
84
|
|
|
// Page content (not HTML head and stuff!) |
|
85
|
|
|
// should be created there... |
|
86
|
1 |
|
// --------------------------------------- |
|
87
|
|
|
$content_response = $next($request, $response); |
|
88
|
|
|
|
|
89
|
|
|
// --------------------------------------- |
|
90
|
|
|
// 3. Create template variables |
|
91
|
|
|
// --------------------------------------- |
|
92
|
|
|
|
|
93
|
1 |
|
$vars = array_merge( |
|
94
|
1 |
|
$this->defaults, [ |
|
95
|
1 |
|
'title' => $website->getTitle(), |
|
96
|
1 |
|
'id' => $website->getDomId(), |
|
97
|
1 |
|
'base_url' => $request->getUri()->getBaseUrl(), |
|
98
|
1 |
|
'content' => (string) $content_response->getBody() |
|
99
|
1 |
|
] |
|
100
|
1 |
|
); |
|
101
|
|
|
|
|
102
|
1 |
|
$vars['javascripts'] = isset($vars['javascripts']) |
|
103
|
1 |
|
? array_merge($vars['javascripts'], $website->getJavascripts() ?: array()) |
|
104
|
1 |
|
: ($website->getJavascripts() ?: array()); |
|
105
|
|
|
|
|
106
|
1 |
|
$vars['stylesheets'] = isset($vars['stylesheets']) |
|
107
|
1 |
|
? array_merge($vars['stylesheets'], $website->getStylesheets() ?: array()) |
|
108
|
1 |
|
: ($website->getStylesheets() ?: array()); |
|
109
|
|
|
|
|
110
|
|
|
|
|
111
|
|
|
// --------------------------------------- |
|
112
|
|
|
// 5. After that, render 'full page' template |
|
113
|
|
|
// and store in new Response Body. |
|
114
|
|
|
// --------------------------------------- |
|
115
|
|
|
|
|
116
|
1 |
|
// Output global website template |
|
117
|
|
|
$this->logger->debug("Render page template…"); |
|
118
|
1 |
|
|
|
119
|
1 |
|
$render = $this->render; |
|
120
|
|
|
$rendered_result = $render( $this->template, $vars); |
|
121
|
1 |
|
|
|
122
|
|
|
$this->logger->debug("Finish page template render; write response"); |
|
123
|
|
|
|
|
124
|
|
|
$full_html_response_body = new ResponseBody(fopen('php://temp', 'r+')); |
|
125
|
|
|
$full_html_response_body->write($rendered_result); |
|
126
|
|
|
|
|
127
|
|
|
|
|
128
|
1 |
|
// --------------------------------------- |
|
129
|
1 |
|
// 6. Replace old Response Body with new one |
|
130
|
|
|
// --------------------------------------- |
|
131
|
1 |
|
|
|
132
|
1 |
|
return $content_response->withHeader('Content-Type', 'text/html') |
|
133
|
|
|
->withBody($full_html_response_body); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
|
|
137
|
|
|
|
|
138
|
|
|
} |
|
139
|
|
|
|