|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the prooph/php-ddd-cargo-sample. |
|
4
|
|
|
* (c) Alexander Miertsch <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
* |
|
9
|
|
|
* Date: 8/8/15 - 12:37 AM |
|
10
|
|
|
*/ |
|
11
|
|
|
namespace Codeliner\CargoUI; |
|
12
|
|
|
|
|
13
|
|
|
use Psr\Http\Message\RequestInterface; |
|
14
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class Main |
|
18
|
|
|
* |
|
19
|
|
|
* This middleware is the main entry point to the CargoUI. |
|
20
|
|
|
* It compiles riot.js tags and put them into a HTML5 layout together |
|
21
|
|
|
* with some js libs and bootstrap css. |
|
22
|
|
|
* If enabled it caches the compiled layout to respond fast on subsequent requests |
|
23
|
|
|
* and let the riot SPA handle the client logic. |
|
24
|
|
|
* |
|
25
|
|
|
* @package Codeliner\CargoUI |
|
26
|
|
|
* @author Alexander Miertsch <[email protected]> |
|
27
|
|
|
*/ |
|
28
|
|
|
final class Main |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* @var bool |
|
32
|
|
|
*/ |
|
33
|
|
|
private $cacheEnabled; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
private $layout; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var string |
|
42
|
|
|
*/ |
|
43
|
|
|
private $layoutCacheFile = 'data/cache/layout.phtml'; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var RiotCompiler |
|
47
|
|
|
*/ |
|
48
|
|
|
private $riotCompiler; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param string $layout |
|
52
|
|
|
* @param bool $cacheEnabled |
|
53
|
|
|
* @param RiotCompiler $riotCompiler |
|
54
|
|
|
*/ |
|
55
|
|
|
public function __construct($layout, $cacheEnabled, RiotCompiler $riotCompiler) |
|
56
|
|
|
{ |
|
57
|
|
|
$this->layout = (string)$layout; |
|
58
|
|
|
$this->cacheEnabled = (bool)$cacheEnabled; |
|
59
|
|
|
$this->riotCompiler = $riotCompiler; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Render layout and write it to the response body |
|
64
|
|
|
* |
|
65
|
|
|
* @param RequestInterface $request |
|
66
|
|
|
* @param ResponseInterface $response |
|
67
|
|
|
* @param callable|null $next |
|
68
|
|
|
* @return ResponseInterface |
|
69
|
|
|
*/ |
|
70
|
|
|
public function __invoke(RequestInterface $request, ResponseInterface $response, callable $next = null) |
|
|
|
|
|
|
71
|
|
|
{ |
|
72
|
|
|
$layout = $this->renderLayout(); |
|
73
|
|
|
|
|
74
|
|
|
$response->getBody()->write($layout); |
|
75
|
|
|
|
|
76
|
|
|
return $response; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
private function renderLayout() |
|
80
|
|
|
{ |
|
81
|
|
|
if ($this->cacheEnabled && file_exists($this->layoutCacheFile)) { |
|
82
|
|
|
return file_get_contents($this->layoutCacheFile); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
$layout = ""; |
|
86
|
|
|
try { |
|
87
|
|
|
ob_start(); |
|
88
|
|
|
$includeReturn = include $this->layout; |
|
89
|
|
|
$layout = ob_get_clean(); |
|
90
|
|
|
} catch (\Exception $ex) { |
|
91
|
|
|
ob_end_clean(); |
|
92
|
|
|
throw $ex; |
|
93
|
|
|
} |
|
94
|
|
|
if ($includeReturn === false && empty($layout)) { |
|
95
|
|
|
throw new \UnexpectedValueException(sprintf( |
|
96
|
|
|
'Unable to render layout "%s"; file include failed', |
|
97
|
|
|
$this->layout |
|
98
|
|
|
)); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
if ($this->cacheEnabled) { |
|
102
|
|
|
file_put_contents($this->layoutCacheFile, $layout); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return $layout; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @return string |
|
110
|
|
|
*/ |
|
111
|
|
|
private function compileRiot() |
|
|
|
|
|
|
112
|
|
|
{ |
|
113
|
|
|
return $this->riotCompiler->compileToRiotStatements(); |
|
114
|
|
|
} |
|
115
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.