1: <?php
2:
3: namespace Charcoal\App\Route;
4:
5: use InvalidArgumentException;
6:
7: // Dependency from 'charcoal-app'
8: use Charcoal\App\App;
9:
10: // Dependencies from 'charcoal-translator'
11: use Charcoal\Translator\Translation;
12:
13: // Local namespace dependencies
14: use Charcoal\App\Route\RouteConfig;
15:
16: /**
17: *
18: */
19: class TemplateRouteConfig extends RouteConfig
20: {
21: /**
22: * The template ident (to load).
23: * @var string $template
24: */
25: private $template;
26:
27: /**
28: * The view engine ident to use.
29: * Ex: "mustache", ""
30: * @var string $engine
31: */
32: private $engine;
33:
34: /**
35: * Additional template data.
36: * @var array $templateData
37: */
38: private $templateData = [];
39:
40: /**
41: * Redirect URL.
42: * @var string $redirect
43: */
44: private $redirect;
45:
46: /**
47: * Redirect Mode (HTTP status code).
48: * @var integer $redirectMode
49: */
50: private $redirectMode = 301;
51:
52: /**
53: * Enable route-level caching for this template.
54: * @var boolean $cache
55: */
56: private $cache = false;
57:
58: /**
59: * If using cache, the time-to-live, in seconds, of the cache. (0 = no limit).
60: * @var integer $cache_ttl
61: */
62: private $cache_ttl = 0;
63:
64: /**
65: * @param string|null $template The template identifier.
66: * @throws InvalidArgumentException If the tempalte parameter is not null or not a string.
67: * @return TemplateRouteConfig Chainable
68: */
69: public function setTemplate($template)
70: {
71: if ($template === null) {
72: $this->template = null;
73: return $this;
74: }
75: if (!is_string($template)) {
76: throw new InvalidArgumentException(
77: 'Template must be a string (the template ident)'
78: );
79: }
80: $this->template = $template;
81: return $this;
82: }
83:
84: /**
85: * @return string
86: */
87: public function template()
88: {
89: if ($this->template === null) {
90: return $this->ident();
91: }
92: return $this->template;
93: }
94:
95: /**
96: * @return string
97: */
98: public function defaultController()
99: {
100: $config = App::instance()->config();
101:
102: if ($config->has('view.default_controller')) {
103: return $config->get('view.default_controller');
104: }
105: }
106:
107: /**
108: * @param string|null $engine The engine identifier (mustache, php, or mustache-php).
109: * @throws InvalidArgumentException If the engine is not null or not a string.
110: * @return TemplateRouteConfig Chainable
111: */
112: public function setEngine($engine)
113: {
114: if ($engine === null) {
115: $this->engine = null;
116: return $this;
117: }
118: if (!is_string($engine)) {
119: throw new InvalidArgumentException(
120: 'Engine must be a string (the engine ident)'
121: );
122: }
123: $this->engine = $engine;
124: return $this;
125: }
126:
127: /**
128: * @return string
129: */
130: public function engine()
131: {
132: if ($this->engine === null) {
133: return $this->defaultEngine();
134: }
135: return $this->engine;
136: }
137:
138: /**
139: * @return string
140: */
141: public function defaultEngine()
142: {
143: $config = App::instance()->config();
144:
145: if ($config->has('view.default_engine')) {
146: return $config->get('view.default_engine');
147: } else {
148: return 'mustache';
149: }
150: }
151:
152: /**
153: * Set the template data for the view.
154: *
155: * @param array $templateData The route template data.
156: * @return TemplateRouteConfig Chainable
157: */
158: public function setTemplateData(array $templateData)
159: {
160: if (!isset($this->templateData)) {
161: $this->templateData = [];
162: }
163:
164: $this->templateData = array_merge($this->templateData, $templateData);
165:
166: return $this;
167: }
168:
169: /**
170: * Get the template data for the view.
171: *
172: * @return array
173: */
174: public function templateData()
175: {
176: return $this->templateData;
177: }
178:
179: /**
180: * @param string|string[] $url Points to a route.
181: * @return TemplateRouteConfig Chainable
182: */
183: public function setRedirect($url)
184: {
185: $this->redirect = $url;
186:
187: return $this;
188: }
189:
190: /**
191: * Retrieve the redirection URL.
192: *
193: * @return string|string[]
194: */
195: public function redirect()
196: {
197: return $this->redirect;
198: }
199:
200: /**
201: * Set the redirect HTTP status mode. (Must be 3xx)
202: *
203: * @param mixed $redirectMode The HTTP status code.
204: * @throws InvalidArgumentException If the redirect mode is not 3xx.
205: * @return TemplateRouteConfig Chainable
206: */
207: public function setRedirectMode($redirectMode)
208: {
209: $redirectMode = (int)$redirectMode;
210: if ($redirectMode < 300 || $redirectMode >= 400) {
211: throw new InvalidArgumentException(
212: 'Invalid HTTP status for redirect mode'
213: );
214: }
215:
216: $this->redirectMode = $redirectMode;
217: return $this;
218: }
219:
220: /**
221: * @return integer
222: */
223: public function redirectMode()
224: {
225: return $this->redirectMode;
226: }
227:
228: /**
229: * @param boolean $cache The cache enabled flag.
230: * @return TemplateRouteConfig Chainable
231: */
232: public function setCache($cache)
233: {
234: $this->cache = !!$cache;
235: return $this;
236: }
237:
238: /**
239: * @return boolean
240: */
241: public function cache()
242: {
243: return $this->cache;
244: }
245:
246: /**
247: * @param integer $ttl The cache Time-To-Live, in seconds.
248: * @return TemplateRouteConfig Chainable
249: */
250: public function setCacheTtl($ttl)
251: {
252: $this->cache_ttl = (int)$ttl;
253: return $this;
254: }
255:
256: /**
257: * @return integer
258: */
259: public function cacheTtl()
260: {
261: return $this->cache_ttl;
262: }
263: }
264: