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