1: <?php
2:
3: namespace Charcoal\View;
4:
5: use \InvalidArgumentException;
6:
7: // Module `charcoal-config` dependencies
8: use \Charcoal\Config\AbstractConfig;
9:
10: /**
11: * View configuration.
12: */
13: class ViewConfig extends AbstractConfig
14: {
15: /**
16: * @var array $paths
17: */
18: private $paths = [];
19:
20: /**
21: * @var array $engines
22: */
23: private $engines = [];
24:
25: /**
26: * @var string $defaultEngine
27: */
28: private $defaultEngine;
29:
30: /**
31: * @return array
32: */
33: public function defaults()
34: {
35: return [
36: 'paths' => [],
37: 'engines' => [
38: 'mustache' => [],
39: 'php' => [],
40: 'php-mustache' => [],
41: 'twig' => []
42: ],
43: 'default_engine' => 'mustache'
44: ];
45: }
46:
47: /**
48: * @param array $paths The paths to search into.
49: * @return ViewConfig Chainable
50: */
51: public function setPaths(array $paths)
52: {
53: $this->paths = [];
54: foreach ($paths as $p) {
55: $this->addPath($p);
56: }
57: return $this;
58: }
59:
60: /**
61: * @param string $path A path to add to the paths list.
62: * @throws InvalidArgumentException If the path is not a string.
63: * @return ViewConfig Chainable
64: */
65: public function addPath($path)
66: {
67: if (!is_string($path)) {
68: throw new InvalidArgumentException(
69: 'Template path must be a string'
70: );
71: }
72: $this->paths[] = $path;
73: return $this;
74: }
75:
76: /**
77: * @return array
78: */
79: public function paths()
80: {
81: return $this->paths;
82: }
83:
84: /**
85: * @param array $engines The various engines configuration.
86: * @return ViewConfig Chainable
87: */
88: public function setEngines(array $engines)
89: {
90: $this->engines = [];
91: foreach ($engines as $engineIdent => $engineConfig) {
92: $this->addEngine($engineIdent, $engineConfig);
93: }
94: return $this;
95: }
96:
97: /**
98: * @param string $engineIdent The engine identifier.
99: * @param array $engineConfig The engine configuration data.
100: * @throws InvalidArgumentException If the engine ident is not a string.
101: * @return ViewConfig Chainable
102: */
103: public function addEngine($engineIdent, array $engineConfig)
104: {
105: if (!is_string($engineIdent)) {
106: throw new InvalidArgumentException(
107: 'Can not add engine to view config: engine identifier must be a string.'
108: );
109: }
110: $this->engines[$engineIdent] = $engineConfig;
111: return $this;
112: }
113:
114: /**
115: * @return array
116: */
117: public function engines()
118: {
119: return $this->engines;
120: }
121:
122: /**
123: * Get an engine's configuration.
124: *
125: * @param string|null $engineIdent The engine identifier to get the configuration of.
126: * @throws InvalidArgumentException If the engine ident is not a string or does not match any engines.
127: * @return array
128: */
129: public function engine($engineIdent = null)
130: {
131: if ($engineIdent === null) {
132: $engineIdent = $this->defaultEngine();
133: }
134: if (!is_string($engineIdent)) {
135: throw new InvalidArgumentException(
136: 'Invalid engine identifier (must be a string)'
137: );
138: }
139:
140: if (!isset($this->engines[$engineIdent])) {
141: throw new InvalidArgumentException(
142: sprintf('No configured engines matching "%s"', $engineIdent)
143: );
144: }
145: return $this->engines[$engineIdent];
146: }
147:
148: /**
149: * @param string $engineIdent The default engine (identifier).
150: * @throws InvalidArgumentException If the engine ident is not a string.
151: * @return ViewConfig Chainable
152: */
153: public function setDefaultEngine($engineIdent)
154: {
155: if (!is_string($engineIdent)) {
156: throw new InvalidArgumentException(
157: 'Default engine must be a string'
158: );
159: }
160: $this->defaultEngine = $engineIdent;
161: return $this;
162: }
163:
164: /**
165: * @return string
166: */
167: public function defaultEngine()
168: {
169: return $this->defaultEngine;
170: }
171: }
172: