-
Notifications
You must be signed in to change notification settings - Fork 0
/
doT.php
182 lines (143 loc) · 4.32 KB
/
doT.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
/**
* @license BSD-3-Clause
* @author Alex Yatsenko
* @link https://github.com/webfay
* @version 0.0.1
*/
class doT
{
/**
* @var string $templateContents
*/
protected $templateContents = null;
/**
* @var array $assings
*/
protected $assigns = [];
/**
* @var array $settings
*/
public $settings =
[
'jsPropertyName' => 'it',
'jsPropertyCall' => '.',
'getdoTValueTemplate' => '{{=%s}}',
'isCacheContents' => 1,
'findIdRegex' => '/id\=("|\')%ID%("|\')(.*)\>(.*)\<\/script\>/Uims',
'regexResultId' => 4,
'templatesIsHandleByPHP' => 1
];
/**
* @var array $cacheContents
*/
public static $cacheContents = [];
/**
* @param string $template path or content of template
* @param boolean $isFile is $template of path
* @param array $settings
* @return object of doT
*/
public static
function instance($template = null, $isFile = 0,array $settings = [])
{
return new self($template, $isFile, $settings);
}
/**
* @param string $template path to template or contents
* @param boolean $isFile $template is path to template file
* @param array $setinngs
* @throws Exception if file on following is not found
*/
public
function __construct($template = null, $isFile = 0,array $settings = [])
{
$this->settings = $settings + $this->settings;
if($template && !$isFile)
return $this->templateContents = $template;
if($isFile && file_exists($template))
return $this->loadTemplate($template, $this->settings['templatesIsHandleByPHP']);
if($isFile)
throw new Exception('File '.$template.' is not exists');
}
public
function __set($key, $value)
{
$this->assigns[$key] = $value;
return null;
}
/**
* @param string $templatePath path of template
* @param boolean $isHandlePHP if set to true code in template will be execute
* @return object $this
*/
public
function loadTemplate($templatePath = null, $isHandlePHP = 0)
{
if($this->settings['isCacheContents'] && isset(self::$cacheContents[$templatePath]))
return $this->templateContents = self::$cacheContents[$templatePath];
if(!file_exists($templatePath) || !is_readable($templatePath))
throw new Exception('Path to template '. $templatePath.' is not exist or not readable');
if($isHandlePHP)
{
ob_start();
require $templatePath;
$this->templateContents = ob_get_contents();
ob_end_clean();
return $this;
}
$this->templateContents = file_get_contents($templatePath);
return $this;
}
/**
* @param array $assigns
* @return object $this
*/
public
function assign(array $assigns = [])
{
$this->assigns = $assigns;
return $this;
}
/**
* @return array prepared assigns for replace in template contents
*/
protected
function getPrepareAssigns()
{
$assigns = [];
foreach($this->assigns as $assignKey => $assignValue)
{
if(is_array($assignValue) || is_object($assignValue))
throw new Exception('php doT render: value not can be array or object. Array key :' . $assignKey);
$key = $this->settings['jsPropertyName'].$this->settings['jsPropertyCall']. $assignKey;
$assigns [str_replace('%s', $key, $this->settings['getdoTValueTemplate'])] = $assignValue;
}
return $assigns;
}
/**
* @param string $ID script template id to find in contents
* @return string part with ID
*/
protected
function getTemplatePartByScriptId($ID = null)
{
// @TODO: make cache for next
preg_match(str_replace('%ID%', $ID, $this->settings['findIdRegex']), $this->templateContents, $match);
return isset($match[$this->settings['regexResultId']]) ? $match[$this->settings['regexResultId']] : FALSE;
}
/**
* @param string $FindID script template id to find in contents
* @param array $assigns assigns to set with $this->assign
* @return string ready template
*/
public
function render($FindID = null, array $assigns = [])
{
if(sizeof($assigns))
$this->assign($assigns);
if($FindID && !$this->getTemplatePartByScriptId($FindID))
throw new Exception('Failed to parse template: id '. $FindID.' is not find');
return strtr(($FindID ? $this->getTemplatePartByScriptId($FindID) : $this->templateContents), $this->getPrepareAssigns());
}
}