1 | <?php |
||
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() |
||
115 | } |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.