-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPerfORMModelCacheBuilder.php
More file actions
301 lines (252 loc) · 7.59 KB
/
PerfORMModelCacheBuilder.php
File metadata and controls
301 lines (252 loc) · 7.59 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
<?php
/**
* PerfORM - Object-relational mapping based on David Grudl's dibi
*
* @copyright Copyright (c) 2004, 2010 David Grudl
* @copyright Copyright (c) 2010 Eduard 'edke' Kracmar
* @license http://nettephp.com/license Nette license
* @link http://nettephp.com
* @category PerfORM
* @package PerfORM
*/
/**
* PerfORMModelCacheBuilder
*
* Builder heavilly extended from great David Grudl's RobotLoadet of Nette
* Searches for Models in APP directory and builds model's cache
*
* @copyright Copyright (c) 2004, 2010 David Grudl
* @copyright Copyright (c) 2010 Eduard 'edke' Kracmar
* @package PerfORM
*/
class PerfORMModelCacheBuilder
{
/** @var array */
public $scanDirs;
/** @var string comma separated wildcards */
public $ignoreDirs = '.*, *.old, *.bak, *.tmp, log*, templates, temp, presenters';
/** @var string comma separated wildcards */
public $acceptFiles = '*.php, *.php5';
/** @var string */
private $acceptMask;
/** @var string */
private $ignoreMask;
/** @var array all models of application */
private $models= array();
/**
* @var array
*/
private $modelInfo= array();
/**
* @var string
*/
private $modelCacheDir;
/**
* Constructor
*/
public function __construct()
{
/* check if modelCacheDir valid */
$this->modelCacheDir= realpath(Environment::getConfig('perform')->modelCache);
if ( !file_exists($this->modelCacheDir) and !mkdir($this->modelCacheDir, 0777, true))
{
throw new Exception("Unable to create model cache directory '$this->modelCacheDir'.");
}
if ( !is_writable($this->modelCacheDir) )
{
throw new Exception("Model cache directory '$this->modelCacheDir' is not writable.");
}
}
/**
* Add class and file name to the list.
* @param string
* @param string
* @return void
*/
public function addModelInfo($modelInfo)
{
$this->modelInfo[]= $modelInfo;
}
/**
* Add directory (or directories) to list.
* @param string|array
* @return void
* @throws DirectoryNotFoundException if path is not found
*/
public function addDirectory($path)
{
foreach ((array) $path as $val)
{
$real = realpath($val);
if ($real === FALSE)
{
throw new DirectoryNotFoundException("Directory '$val' not found.");
}
$this->scanDirs[] = $real;
}
}
/**
* Getter for models found in application
* @return array
*/
public function getModels()
{
return $this->models;
}
/**
* Rebuilds class list cache.
* @param bool
* @return void
*/
public function rebuild()
{
/* scan for model bases */
$this->acceptMask = self::wildcards2re($this->acceptFiles);
$this->ignoreMask = self::wildcards2re($this->ignoreDirs);
foreach (array_unique($this->scanDirs) as $dir)
{
$this->scanDirectory($dir);
}
/* cleans previous models */
$iterator = dir($this->modelCacheDir);
while (FALSE !== ($entry = $iterator->read()))
{
if ($entry == '.' || $entry == '..') continue;
$path = $this->modelCacheDir . DIRECTORY_SEPARATOR . $entry;
if (is_file($path) && preg_match($this->acceptMask, $entry))
{
unlink($path);
}
}
$iterator->close();
/* build new models */
$_template= file_get_contents(dirname(__FILE__).'/ModelCacheTemplate/ModelCache.phptmpl');
foreach($this->modelInfo as $modelInfo)
{
$template= $_template;
$template= str_replace('%lastModification%', time(), $template);
$template= str_replace('%modelName%', $modelInfo->model, $template);
$template= str_replace('%modelBase%', $modelInfo->extends, $template);
/* $template= str_replace('%setup_fields%', $modelInfo->setup_fields, $template);
$template= str_replace('%setup_indexes%', $modelInfo->setup_indexes, $template);*/
$template= str_replace('%setup_extends%', $modelInfo->setup_extends, $template);
$template= str_replace('%source%', $modelInfo->path, $template);
$properties= null;
foreach( $modelInfo->fields as $field)
{
$propertyType= $field->reference ? $field->reference : call_user_func($field->type . '::getPhpDocProperty');
$properties .= sprintf("\n * @property %s \$%s", $propertyType , $field->name);
}
$template= str_replace('%properties%', $properties, $template);
file_put_contents($this->modelCacheDir . DIRECTORY_SEPARATOR . $modelInfo->model.'.php', $template);
}
/* first run, make models callable */
foreach($this->modelInfo as $modelInfo)
{
if ( !class_exists($modelInfo->model, false))
{
require_once $this->modelCacheDir . DIRECTORY_SEPARATOR . $modelInfo->model .'.php';
}
}
/* second run, create new model instances and collection of models */
foreach($this->modelInfo as $modelInfo)
{
$model= new $modelInfo->model;
$this->models[$model->getTableName()]= $model;
}
}
protected function removeSpaces(&$string)
{
$string= preg_replace('#\s+#', '', $string);
}
/**
* Scan a directory for PHP files and subdirectories
* @param string
* @return void
*/
protected function scanDirectory($dir)
{
$iterator = dir($dir);
if (!$iterator) return;
while (FALSE !== ($entry = $iterator->read()))
{
if ($entry == '.' || $entry == '..') continue;
$path = $dir . DIRECTORY_SEPARATOR . $entry;
// process subdirectories
if (is_dir($path))
{
// check ignore mask
if (!preg_match($this->ignoreMask, $entry))
{
$this->scanDirectory($path);
}
continue;
}
if (is_file($path) && preg_match($this->acceptMask, $entry))
{
$this->scanScript($path);
}
}
$iterator->close();
}
/**$class[3][$key]
* Analyse PHP file.
* @param string
* @return void
*/
protected function scanScript($file)
{
$buffer= file_get_contents($file);
// clean comments
$buffer= preg_replace('/(\/\/|#).*/', '', $buffer);
$buffer= preg_replace('#/\*.*\*/#msU', '', $buffer);
if ( preg_match_all('#abstract\s*class\s*(PerfORM([^ ]+))\s*extends\s*(PerfORM([^ ]*))\s*(?:implements\s*[a-zA-Z0-9, ]+\s*)*{.*protected\s*function\s*setup\s*\(\s*\)\s*{(.*)}#imsU', $buffer, $class))
{
foreach($class[0] as $key => $value)
{
$_fields= array();
if ( preg_match_all('#\$this\-\>add(.+Field)\s*\(\s*[\'\"]([^\'\"]+)[\'\"](?:\s*,\s*new\s*([^ \)]+)\s*\)){0,1}#i', $class[5][$key], $field))
{
foreach($field[0] as $field_key => $field_value)
{
$reference= null;
if ( $field[1][$field_key] == 'ForeignKeyField' && !empty ($field[3][$field_key]) )
{
$reference= $field[3][$field_key];
}
$_fields[$field[2][$field_key]]= (object) array(
'name' => $field[2][$field_key],
'type' => $field[1][$field_key],
'reference' => $reference,
);
}
}
$this->addModelInfo( (object) array(
'path' => $file,
'extends' => $class[1][$key],
'model' => $class[2][$key],
'table' => strtolower($class[2][$key]),
'setup_extends' => empty($class[4][$key]) ? '' : "\t\$this->setInheritance( new ". $class[4][$key].");",
'fields' => $_fields,
));
}
}
}
/**
* Converts comma separated wildcards to regular expression.
* @param string
* @return string
*/
private static function wildcards2re($wildcards)
{
$mask = array();
foreach (explode(',', $wildcards) as $wildcard)
{
$wildcard = trim($wildcard);
$wildcard = addcslashes($wildcard, '.\\+[^]$(){}=!><|:#');
$wildcard = strtr($wildcard, array('*' => '.*', '?' => '.'));
$mask[] = $wildcard;
}
return '#^(' . implode('|', $mask) . ')$#i';
}
}