1: <?php
2:
3: namespace Charcoal\App\Route;
4:
5: use InvalidArgumentException;
6:
7:
8: use Psr\Http\Message\RequestInterface;
9: use Psr\Http\Message\ResponseInterface;
10:
11:
12: use Pimple\Container;
13:
14:
15: use Slim\Http\Uri;
16:
17:
18: use Charcoal\Config\ConfigurableInterface;
19: use Charcoal\Config\ConfigurableTrait;
20:
21:
22: use Charcoal\App\AppInterface;
23: use Charcoal\App\Template\TemplateInterface;
24:
25:
26: use Charcoal\App\Route\RouteInterface;
27: use Charcoal\App\Route\TemplateRouteConfig;
28:
29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48:
49: class TemplateRoute implements
50: ConfigurableInterface,
51: RouteInterface
52: {
53: use ConfigurableTrait;
54:
55: 56: 57: 58: 59: 60: 61: 62: 63:
64: public function __construct(array $data)
65: {
66: $this->setConfig($data['config']);
67: }
68:
69: 70: 71: 72: 73: 74:
75: public function createConfig($data = null)
76: {
77: return new TemplateRouteConfig($data);
78: }
79:
80: 81: 82: 83: 84: 85:
86: public function __invoke(
87: Container $container,
88: RequestInterface $request,
89: ResponseInterface $response
90: ) {
91: $config = $this->config();
92:
93:
94: if (!empty($config['redirect'])) {
95: $redirect = $container['translator']->translation($config['redirect']);
96: $uri = $this->parseRedirect((string)$redirect, $request);
97:
98: if ($uri) {
99: return $response->withRedirect($uri, $config['redirect_mode']);
100: }
101: }
102:
103: $templateContent = $this->templateContent($container, $request);
104:
105: $response->getBody()->write($templateContent);
106:
107: return $response;
108: }
109:
110: 111: 112: 113: 114:
115: protected function templateContent(
116: Container $container,
117: RequestInterface $request
118: ) {
119: $config = $this->config();
120:
121: if ($this->cacheEnabled()) {
122: $cachePool = $container['cache'];
123: $cacheKey = str_replace('/', '.', 'template.'.$this->cacheIdent());
124: $cacheItem = $cachePool->getItem($cacheKey);
125:
126: $template = $cacheItem->get();
127: if ($cacheItem->isMiss()) {
128: $template = $this->renderTemplate($container, $request);
129:
130: $cachePool->save($cacheItem->set($template, $this->cacheTtl()));
131: }
132: } else {
133: $template = $this->renderTemplate($container, $request);
134: }
135:
136: return $template;
137: }
138:
139: 140: 141: 142: 143:
144: protected function renderTemplate(Container $container, RequestInterface $request)
145: {
146: $config = $this->config();
147: $template = $this->createTemplate($container, $request);
148:
149: return $container['view']->render($config['template'], $template);
150: }
151:
152: 153: 154: 155: 156:
157: protected function createTemplate(Container $container, RequestInterface $request)
158: {
159: $config = $this->config();
160:
161: $templateFactory = $container['template/factory'];
162: $templateFactory->setDefaultClass($config['default_controller']);
163:
164: $template = $templateFactory->create($config['controller']);
165: $template->init($request);
166:
167:
168: $template->setData($config['template_data']);
169:
170: return $template;
171: }
172:
173: 174: 175: 176: 177:
178: protected function parseRedirect($redirection, RequestInterface $request)
179: {
180: $uri = $request->getUri()->withUserInfo('');
181: $parts = parse_url($redirection);
182:
183: if (!empty($parts)) {
184: if (isset($parts['host'])) {
185: $uri = Uri::createFromString($redirection);
186: } else {
187: if (isset($parts['path'])) {
188: $uri = $uri->withPath($parts['path']);
189: }
190:
191: if (isset($parts['query'])) {
192: $uri = $uri->withQuery($parts['query']);
193: }
194:
195: if (isset($parts['fragment'])) {
196: $uri = $uri->withFragment($parts['fragment']);
197: }
198: }
199:
200: if ((string)$uri !== (string)$request->getUri()) {
201: return $uri;
202: }
203: }
204:
205: return null;
206: }
207:
208: 209: 210:
211: protected function cacheEnabled()
212: {
213: $config = $this->config();
214: return $config['cache'];
215: }
216:
217: 218: 219:
220: protected function cacheTtl()
221: {
222: $config = $this->config();
223: return $config['cache_ttl'];
224: }
225:
226: 227: 228:
229: protected function cacheIdent()
230: {
231: $config = $this->config;
232: return $config['template'];
233: }
234: }
235: