1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sketch\Tpl; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Engine |
9
|
|
|
* @package Sketch\Tpl |
10
|
|
|
*/ |
11
|
|
|
class Engine |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var array |
15
|
|
|
*/ |
16
|
|
|
private $data = []; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param array $config |
20
|
|
|
* @throws Exception |
21
|
|
|
*/ |
22
|
2 |
|
public function config($config): void |
23
|
|
|
{ |
24
|
2 |
|
$expected = ['environment', 'template_dir', 'cache_dir']; |
25
|
|
|
|
26
|
2 |
|
foreach ($expected as $exp) { |
27
|
2 |
|
if (count($config) == 3) { |
28
|
2 |
|
if (!array_key_exists($exp, $config)) { |
29
|
2 |
|
throw new Exception("The $exp configuration is expected"); |
30
|
|
|
} |
31
|
|
|
} else { |
32
|
2 |
|
throw new Exception("The configuration expected only tree arguments"); |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
2 |
|
Tag::setConfig($config); |
37
|
2 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param string $view |
41
|
|
|
* @param array $data |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
2 |
|
public function render(string $view, array $data = []): string |
45
|
|
|
{ |
46
|
|
|
try { |
47
|
2 |
|
$content = $this->handle(Content::getContent($view, Tag::getConfig())); |
48
|
|
|
} catch (Exception $e) { |
49
|
|
|
return $e->getMessage(); // @codeCoverageIgnore |
50
|
|
|
} |
51
|
|
|
|
52
|
2 |
|
$this->data = array_merge($this->data, $data); |
53
|
|
|
|
54
|
2 |
|
$fname = getcwd() . '/' . Tag::getConfig()['cache_dir'] . '/' . $view . '.phtml'; |
55
|
|
|
|
56
|
2 |
|
$file = new File($fname); |
57
|
|
|
|
58
|
2 |
|
if (Tag::getConfig()['environment'] == 'production') { |
59
|
|
|
$file->open(); |
60
|
2 |
|
} elseif (Tag::getConfig()['environment'] == 'development') { |
61
|
2 |
|
$this->setCache($file, $content); |
62
|
|
|
} |
63
|
|
|
|
64
|
2 |
|
$content = $file->read($this->data); |
65
|
|
|
|
66
|
2 |
|
$file->close(); |
67
|
|
|
|
68
|
2 |
|
return $content; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param $content |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
2 |
|
private function handle($content) |
76
|
|
|
{ |
77
|
2 |
|
Tag::setContent($content); |
78
|
|
|
|
79
|
2 |
|
$this->registerTag([ |
80
|
2 |
|
'inheritance', |
81
|
|
|
'include', |
82
|
|
|
'loop', |
83
|
|
|
'if', |
84
|
|
|
'func', |
85
|
|
|
'variable' |
86
|
|
|
]); |
87
|
|
|
|
88
|
2 |
|
return Tag::getContent(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param File $file |
93
|
|
|
* @param $content |
94
|
|
|
*/ |
95
|
2 |
|
private function setCache(File $file, $content): void |
96
|
|
|
{ |
97
|
2 |
|
$file->create(); |
98
|
2 |
|
$file->write($content); |
99
|
2 |
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param array $tags |
103
|
|
|
*/ |
104
|
2 |
|
private function registerTag(array $tags): void |
105
|
|
|
{ |
106
|
2 |
|
foreach ($tags as $tag) { |
107
|
2 |
|
$tag = "\\Sketch\Tpl\\" . ucfirst($tag) . "Tag"; |
108
|
2 |
|
new $tag; |
109
|
|
|
} |
110
|
2 |
|
} |
111
|
|
|
} |
112
|
|
|
|