1: <?php
2:
3: namespace Deimos\Flow;
4:
5: use Deimos\DI\DI;
6:
7: class Configure
8: {
9:
10: use Traits\FileSystem;
11:
12: /**
13: * @var string
14: */
15: protected $compile;
16:
17: /**
18: * @var string
19: */
20: protected $template;
21:
22: /**
23: * @var Tokenizer
24: */
25: protected $tokenizer;
26:
27: /**
28: * @var DI
29: */
30: protected $block;
31:
32: /**
33: * @var DefaultContainer
34: */
35: protected $di;
36:
37: /**
38: * @var string
39: */
40: protected $ext = 'tpl';
41:
42: /**
43: * @var array
44: */
45: protected $extends = [];
46:
47: /**
48: * @var array
49: */
50: protected $extendsAll = [];
51:
52: /**
53: * @var bool
54: */
55: protected $phpEnable = false;
56:
57: /**
58: * @var bool
59: */
60: protected $isDebug = false;
61:
62: /**
63: * @var array
64: */
65: protected $variable = [];
66:
67: /**
68: * Configure constructor.
69: */
70: public function __construct()
71: {
72: $this->tokenizer = new Tokenizer();
73: }
74:
75: /**
76: * @param DefaultContainer $di
77: *
78: * @return DI
79: */
80: public function di(DefaultContainer $di = null)
81: {
82: if (!$this->di)
83: {
84: if (!$di)
85: {
86: $di = new DefaultContainer();
87: }
88:
89: $this->di = $di;
90: }
91:
92: return $this->di;
93: }
94:
95: /**
96: * @return Block
97: */
98: public function block()
99: {
100: if (!$this->block)
101: {
102: $this->block = new Block($this);
103: }
104:
105: return $this->block;
106: }
107:
108: /**
109: * @return Tokenizer
110: */
111: public function tokenizer()
112: {
113: return $this->tokenizer;
114: }
115:
116: /**
117: * @return bool
118: */
119: public function isPhpEnable()
120: {
121: return $this->phpEnable;
122: }
123:
124: /**
125: * @return bool
126: */
127: public function isDebug()
128: {
129: return $this->isDebug;
130: }
131:
132: /**
133: * set php enable
134: */
135: public function phpEnable()
136: {
137: $this->phpEnable = true;
138: }
139:
140: /**
141: * set php enable
142: */
143: public function debugEnable()
144: {
145: $this->isDebug = true;
146: }
147:
148: /**
149: * @param string $path
150: *
151: * @return string
152: *
153: * @throws \InvalidArgumentException
154: */
155: public function compile($path = null)
156: {
157: if ($path)
158: {
159: $this->compile = $this->createDirectory($path);
160: }
161:
162: if (!$this->compile)
163: {
164: throw new \InvalidArgumentException('Set compile directory!');
165: }
166:
167: return $this->compile;
168: }
169:
170: /**
171: * @param $path
172: *
173: * @return mixed
174: *
175: * @throws \InvalidArgumentException
176: */
177: public function getFile($path)
178: {
179: $fullPath = $this->template() . $path;
180:
181: if (file_exists($fullPath))
182: {
183: return file_get_contents($fullPath);
184: }
185:
186: return file_get_contents($path);
187: }
188:
189: /**
190: * @param $path
191: *
192: * @return mixed
193: *
194: * @throws \InvalidArgumentException
195: */
196: public function requireFile($path)
197: {
198: $flow = new Flow($this);
199:
200: return $flow->render($path);
201: }
202:
203:
204: /**
205: * @param string $view
206: * @param string $path
207: *
208: * @return array
209: *
210: * @throws \InvalidArgumentException
211: */
212: public function extendsFile($view, $path)
213: {
214: if (!isset($this->extends[$view]))
215: {
216: $this->extends[$view] = [];
217: }
218:
219: $this->extends[$view][] = $path;
220: $this->extendsAll[$view] = false;
221: $this->extendsAll[$path] = true;
222:
223: return $this->extends[$view];
224: }
225:
226: /**
227: * @param $view
228: *
229: * @return bool
230: */
231: public function getExtendsAll($view)
232: {
233: if (isset($this->extendsAll[$view]))
234: {
235: return $this->extendsAll[$view];
236: }
237:
238: return true;
239: }
240:
241: /**
242: * @param string $view
243: * @param bool $remove
244: *
245: * @return array
246: */
247: public function getExtendsFile($view, $remove = false)
248: {
249: if (isset($this->extends[$view]))
250: {
251: $data = $this->extends[$view];
252:
253: if ($remove)
254: {
255: unset($this->extends[$view]);
256: }
257:
258: return $data;
259: }
260:
261: return [];
262: }
263:
264: /**
265: * @param string $path
266: *
267: * @return string
268: *
269: * @throws \InvalidArgumentException
270: */
271: public function template($path = null)
272: {
273: if ($path)
274: {
275: $this->template = $this->createDirectory($path);
276: }
277:
278: if (!$this->template)
279: {
280: throw new \InvalidArgumentException('Set template directory!');
281: }
282:
283: return $this->template;
284: }
285:
286: /**
287: * @param string $value
288: *
289: * @return string
290: */
291: public function ext($value = null)
292: {
293: if ($value)
294: {
295: $this->ext = $value;
296: }
297:
298: return '.' . $this->ext;
299: }
300:
301: public function getVariables()
302: {
303: return $this->variable;
304: }
305:
306: public function __get($name)
307: {
308: return $this->getVariable($name);
309: }
310:
311: public function __isset($name)
312: {
313: return $this->issetVariable($name);
314: }
315:
316: public function __set($name, $value)
317: {
318: $this->setVariable($name, $value);
319: }
320:
321: public function getVariable($name)
322: {
323: return $this->variable[$name];
324: }
325:
326: public function issetVariable($name)
327: {
328: return isset($this->variable[$name]);
329: }
330:
331: public function setVariable($name, $value)
332: {
333: $this->variable[$name] = $value;
334: }
335:
336: }