1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by Vitaly Iegorov <[email protected]>. |
4
|
|
|
* on 18.02.16 at 14:17 |
5
|
|
|
*/ |
6
|
|
|
namespace samsonframework\view; |
7
|
|
|
|
8
|
|
|
use samsonframework\view\exception\GeneratedViewPathHasReservedWord; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Views generator, this class scans resource for view files and creates |
12
|
|
|
* appropriate View class ancestors with namespace as relative view location |
13
|
|
|
* and file name as View class name ending with "View". |
14
|
|
|
* |
15
|
|
|
* Generator also analyzes view files content and creates protected class field |
16
|
|
|
* members for every variable used inside with chainable setter for this field, |
17
|
|
|
* to help IDE and developer in creating awesome code. |
18
|
|
|
* |
19
|
|
|
* TODO: Check for reserved keywords(like list) in namespaces |
20
|
|
|
* TODO: Somehow know view variable type(typehint??) and add comments and type-hints to generated classes. |
21
|
|
|
* TODO: Clever analysis for foreach, if, and so on language structures, we do not need to create variables for loop iterator. |
22
|
|
|
* TODO: If a variable is used in foreach - this is an array or Iteratable ancestor - we can add typehint automatically |
23
|
|
|
* TODO: Analyze view file php doc comments to get variable types |
24
|
|
|
* TODO: If a token variable is not $this and has "->" - this is object, maybe type-hint needs to be added. |
25
|
|
|
* TODO: Add caching logic to avoid duplicate file reading |
26
|
|
|
* TODO: Do not generate class fields with empty values |
27
|
|
|
* TODO: Generate constants with field names |
28
|
|
|
* |
29
|
|
|
* @package samsonframework\view |
30
|
|
|
*/ |
31
|
|
|
class Generator |
32
|
|
|
{ |
33
|
|
|
/** string All generated view classes will end with this suffix */ |
34
|
|
|
const VIEW_CLASSNAME_SUFFIX = 'View'; |
35
|
|
|
|
36
|
|
|
/** @var array Collection of PHP reserved words */ |
37
|
|
|
protected static $reservedWords = array('list'); |
38
|
|
|
|
39
|
|
|
/** @var Metadata[] Collection of view metadata */ |
40
|
|
|
protected $metadata = array(); |
41
|
|
|
|
42
|
|
|
/** @var \samsonphp\generator\Generator */ |
43
|
|
|
protected $generator; |
44
|
|
|
|
45
|
|
|
/** @var string Generated classes namespace prefix */ |
46
|
|
|
protected $namespacePrefix; |
47
|
|
|
|
48
|
|
|
/** @var string Collection of namespace parts to be ignored in generated namespaces */ |
49
|
|
|
protected $ignoreNamespace = array(); |
50
|
|
|
|
51
|
|
|
/** @var array Collection of view files */ |
52
|
|
|
protected $files = array(); |
53
|
|
|
|
54
|
|
|
/** @var string Scanning entry path */ |
55
|
|
|
protected $entryPath; |
56
|
|
|
|
57
|
|
|
/** @var string Parent view class name */ |
58
|
|
|
protected $parentViewClass; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Generator constructor. |
62
|
|
|
* |
63
|
|
|
* @param \samsonphp\generator\Generator $generator PHP code generator instance |
64
|
|
|
* @param string $namespacePrefix Generated classes namespace will have it |
65
|
|
|
* @param array $ignoreNamespace Namespace parts that needs to ignored |
66
|
|
|
* @param string $parentViewClass Generated classes will extend it |
67
|
|
|
*/ |
68
|
3 |
|
public function __construct( |
69
|
|
|
\samsonphp\generator\Generator $generator, |
70
|
|
|
$namespacePrefix, |
71
|
|
|
array $ignoreNamespace = array(), |
72
|
|
|
$parentViewClass = \samsonframework\view\View::class |
73
|
|
|
) |
|
|
|
|
74
|
|
|
{ |
75
|
3 |
|
$this->generator = $generator; |
76
|
3 |
|
$this->parentViewClass = $parentViewClass; |
77
|
3 |
|
$this->ignoreNamespace = $ignoreNamespace; |
|
|
|
|
78
|
3 |
|
$this->namespacePrefix = rtrim(ltrim($namespacePrefix, '\\'), '\\').'\\'; |
79
|
3 |
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Change variable name to camel caps format. |
83
|
|
|
* |
84
|
|
|
* @param string $variable |
85
|
|
|
* |
86
|
|
|
* @return string Changed variable name |
87
|
|
|
*/ |
88
|
1 |
|
public function changeName($variable) |
89
|
|
|
{ |
90
|
1 |
|
return lcfirst( |
91
|
1 |
|
implode( |
92
|
1 |
|
'', |
93
|
1 |
|
array_map( |
94
|
|
|
function ($element) { return ucfirst($element);}, |
|
|
|
|
95
|
1 |
|
explode('_', $variable) |
96
|
1 |
|
) |
97
|
1 |
|
) |
98
|
1 |
|
); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Recursively scan path for files with specified extensions. |
103
|
|
|
* |
104
|
|
|
* @param string $source Entry point path |
105
|
|
|
* @param string $path Entry path for scanning |
106
|
|
|
* @param array $extensions Collection of file extensions without dot |
107
|
|
|
*/ |
108
|
3 |
|
public function scan($source, array $extensions = array(View::DEFAULT_EXT), $path = null) |
109
|
|
|
{ |
110
|
3 |
|
$this->entryPath = $source; |
111
|
|
|
|
112
|
3 |
|
$path = isset($path) ? $path : $source; |
113
|
|
|
|
114
|
|
|
// Recursively go deeper into inner folders for scanning |
115
|
3 |
|
$folders = glob($path.'/*', GLOB_ONLYDIR); |
116
|
3 |
|
foreach ($folders as $folder) { |
117
|
3 |
|
$this->scan($source, $extensions, $folder); |
118
|
3 |
|
} |
119
|
|
|
|
120
|
|
|
// Iterate file extensions |
121
|
3 |
|
foreach ($extensions as $extension) { |
122
|
3 |
|
foreach (glob(rtrim($path, '/') . '/*.'.$extension) as $file) { |
123
|
3 |
|
$this->files[str_replace($source, '', $file)] = $file; |
124
|
3 |
|
} |
125
|
3 |
|
} |
126
|
3 |
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Analyze view file and create its metadata. |
130
|
|
|
* |
131
|
|
|
* @param string $file Path to view file |
132
|
|
|
* |
133
|
|
|
* @return Metadata View file metadata |
134
|
|
|
*/ |
135
|
2 |
|
public function analyze($file) |
136
|
|
|
{ |
137
|
2 |
|
$metadata = new Metadata(); |
138
|
|
|
// Use PHP tokenizer to find variables |
139
|
2 |
|
foreach ($tokens = token_get_all(file_get_contents($file)) as $idx => $token) { |
140
|
2 |
|
if (!is_string($token) && $token[0] === T_VARIABLE) { |
141
|
|
|
// Store variable |
142
|
1 |
|
$variableText = $token[1]; |
143
|
|
|
// Store variable name |
144
|
1 |
|
$variableName = ltrim($token[1], '$'); |
145
|
|
|
|
146
|
|
|
// Ignore static variables |
147
|
1 |
|
if (isset($tokens[$idx-1]) && $tokens[$idx-1][0] === T_DOUBLE_COLON) { |
148
|
1 |
|
$metadata->static[$variableName] = $variableText; |
149
|
1 |
|
continue; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
// If next token is object operator |
153
|
1 |
|
if ($tokens[$idx + 1][0] === T_OBJECT_OPERATOR) { |
154
|
|
|
// Ignore $this |
155
|
1 |
|
if ($variableName === 'this') { |
156
|
1 |
|
continue; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
// And two more tokens |
160
|
1 |
|
$variableText .= $tokens[$idx + 1][1] . $tokens[$idx + 2][1]; |
161
|
|
|
|
162
|
|
|
// Store object variable |
163
|
1 |
|
$metadata->variables[$this->changeName($variableName)] = $variableText; |
164
|
|
|
// Store view variable key - actual object name => full variable usage |
165
|
1 |
|
$metadata->originalVariables[$this->changeName($variableName)] = $variableName; |
166
|
1 |
|
} else { |
167
|
|
|
// Store original variable name |
168
|
1 |
|
$metadata->originalVariables[$this->changeName($variableName)] = $variableName; |
169
|
|
|
// Store view variable key - actual object name => full variable usage |
170
|
1 |
|
$metadata->variables[$this->changeName($variableName)] = $variableText; |
171
|
|
|
} |
172
|
2 |
|
} elseif ($token[0] === T_DOC_COMMENT) { // Match doc block comments |
173
|
|
|
// Parse variable type and name |
174
|
2 |
|
if (preg_match('/@var\s+(?<type>[^ ]+)\s+(?<variable>[^*]+)/', $token[1], $matches)) { |
175
|
1 |
|
$metadata->types[substr(trim($matches['variable']), 1)] = $matches['type']; |
176
|
1 |
|
} |
177
|
2 |
|
} |
178
|
2 |
|
} |
179
|
|
|
|
180
|
2 |
|
return $metadata; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Generic class name and its name space generator. |
185
|
|
|
* |
186
|
|
|
* @param string $file Full path to view file |
187
|
|
|
* @param string $entryPath Entry path |
188
|
|
|
* |
189
|
|
|
* @return array Class name[0] and namespace[1] |
190
|
|
|
* @throws GeneratedViewPathHasReservedWord |
191
|
|
|
*/ |
192
|
2 |
|
protected function generateClassName($file, $entryPath) |
193
|
|
|
{ |
194
|
|
|
// Get only file name as a class name with suffix |
195
|
2 |
|
$className = ucfirst(pathinfo($file, PATHINFO_FILENAME) . self::VIEW_CLASSNAME_SUFFIX); |
196
|
|
|
|
197
|
|
|
// Get namespace as part of file path relatively to entry path |
198
|
2 |
|
$nameSpace = rtrim(ltrim( |
199
|
2 |
|
str_replace( |
200
|
2 |
|
'/', |
201
|
2 |
|
'\\', |
202
|
2 |
|
str_replace($entryPath, '', pathinfo($file, PATHINFO_DIRNAME)) |
203
|
2 |
|
), |
204
|
|
|
'\\' |
205
|
2 |
|
), '\\'); |
206
|
|
|
|
207
|
|
|
// Remove ignored parts from namespaces |
208
|
2 |
|
$nameSpace = str_replace($this->ignoreNamespace, '', $nameSpace); |
209
|
|
|
|
210
|
|
|
// Check generated namespaces |
211
|
2 |
|
foreach (static::$reservedWords as $reservedWord) { |
212
|
2 |
|
if (strpos($nameSpace, '\\' . $reservedWord) !== false) { |
213
|
1 |
|
throw new GeneratedViewPathHasReservedWord($file.'('.$reservedWord.')'); |
214
|
|
|
} |
215
|
1 |
|
} |
216
|
|
|
|
217
|
|
|
// Return collection for further usage |
218
|
1 |
|
return array($className, rtrim($this->namespacePrefix . $nameSpace, '\\')); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Generate view classes. |
223
|
|
|
* |
224
|
|
|
* @param string $path Entry path for generated classes and folders |
225
|
|
|
*/ |
226
|
2 |
|
public function generate($path = __DIR__) |
227
|
|
|
{ |
228
|
2 |
|
foreach ($this->files as $relativePath => $absolutePath) { |
229
|
2 |
|
$this->metadata[$absolutePath] = $this->analyze($absolutePath); |
230
|
2 |
|
$this->metadata[$absolutePath]->path = $absolutePath; |
231
|
1 |
|
list($this->metadata[$absolutePath]->className, |
232
|
2 |
|
$this->metadata[$absolutePath]->namespace) = $this->generateClassName($absolutePath, $this->entryPath); |
233
|
1 |
|
} |
234
|
|
|
|
235
|
1 |
|
foreach ($this->metadata as $metadata) { |
236
|
1 |
|
$this->generateViewClass($metadata, $path); |
237
|
1 |
|
} |
238
|
1 |
|
} |
239
|
|
|
|
240
|
|
|
/** @return string Hash representing generator state */ |
241
|
1 |
|
public function hash() |
242
|
|
|
{ |
243
|
1 |
|
$hash = ''; |
244
|
1 |
|
foreach ($this->files as $relativePath => $absolutePath) { |
245
|
1 |
|
$hash .= md5($relativePath.filemtime($absolutePath)); |
246
|
1 |
|
} |
247
|
|
|
|
248
|
1 |
|
return md5($hash); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Create View class ancestor. |
253
|
|
|
* |
254
|
|
|
* @param Metadata $metadata View file metadata |
255
|
|
|
* @param string $path Entry path for generated classes and folders |
256
|
|
|
*/ |
257
|
1 |
|
protected function generateViewClass(Metadata $metadata, $path) |
258
|
|
|
{ |
259
|
1 |
|
$this->generator |
|
|
|
|
260
|
1 |
|
->defNamespace($metadata->namespace) |
261
|
1 |
|
->multiComment(array('Class for view "'.$metadata->path.'" rendering')) |
262
|
1 |
|
->defClass($metadata->className, '\\' . $this->parentViewClass) |
263
|
1 |
|
->commentVar('string', 'Path to view file') |
264
|
1 |
|
->defClassVar('$file', 'protected', $metadata->path) |
265
|
1 |
|
->commentVar('string', 'View source code') |
266
|
1 |
|
->defClassVar('$source', 'protected', '<<<\'EOT\'' . "\n" . file_get_contents($metadata->path) . "\n" . 'EOT'); |
267
|
|
|
//->commentVar('array', 'Collection of view variables') |
268
|
|
|
//->defClassVar('$variables', 'public static', array_keys($metadata->variables)) |
269
|
|
|
//->commentVar('array', 'Collection of view variable types') |
270
|
|
|
//->defClassVar('$types', 'public static', $metadata->types) |
271
|
1 |
|
; |
272
|
|
|
|
273
|
|
|
// Iterate all view variables |
274
|
1 |
|
foreach (array_keys($metadata->variables) as $name) { |
275
|
1 |
|
$type = array_key_exists($name, $metadata->types) ? $metadata->types[$name] : 'mixed'; |
276
|
1 |
|
$static = array_key_exists($name, $metadata->static) ? ' static' : ''; |
277
|
1 |
|
$this->generator |
278
|
1 |
|
->commentVar($type, 'View variable') |
279
|
1 |
|
->defClassVar('$'.$name, 'public'.$static); |
280
|
|
|
|
281
|
|
|
// Do not generate setters for static variables |
282
|
1 |
|
if ($static !== ' static') { |
283
|
1 |
|
$this->generator->text($this->generateViewVariableSetter( |
284
|
1 |
|
$name, |
285
|
1 |
|
$metadata->originalVariables[$name], |
286
|
|
|
$type |
287
|
1 |
|
)); |
288
|
1 |
|
} |
289
|
1 |
|
} |
290
|
|
|
|
291
|
|
|
// Iterate namespace and create folder structure |
292
|
1 |
|
$path .= '/'.str_replace('\\', '/', $metadata->namespace); |
293
|
1 |
|
if (!is_dir($path)) { |
294
|
1 |
|
mkdir($path, 0775, true); |
295
|
1 |
|
} |
296
|
|
|
|
297
|
1 |
|
file_put_contents( |
298
|
1 |
|
$path.'/'.$metadata->className.'.php', |
299
|
1 |
|
'<?php'.$this->generator->endClass()->flush() |
300
|
1 |
|
); |
301
|
1 |
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* Generate constructor for application class. |
305
|
|
|
* |
306
|
|
|
* @param string $variable View variable name |
307
|
|
|
* @param string $original Original view variable name |
308
|
|
|
* @param string $type Variable type |
309
|
|
|
* |
310
|
|
|
* @return string View variable setter method |
311
|
|
|
*/ |
312
|
1 |
|
protected function generateViewVariableSetter($variable, $original, $type = 'mixed') |
313
|
|
|
{ |
314
|
|
|
// Define type hint |
315
|
1 |
|
$typeHint = strpos($type, '\\') !== false ? $type.' ' : ''; |
316
|
|
|
|
317
|
1 |
|
$class = "\n\t" . '/**'; |
318
|
1 |
|
$class .= "\n\t" . ' * Setter for ' . $variable . ' view variable'; |
319
|
1 |
|
$class .= "\n\t" . ' *'; |
320
|
1 |
|
$class .= "\n\t" . ' * @param '.$type.' $value View variable value'; |
321
|
1 |
|
$class .= "\n\t" . ' * @return $this Chaining'; |
322
|
1 |
|
$class .= "\n\t" . ' */'; |
323
|
1 |
|
$class .= "\n\t" . 'public function ' . $variable . '('.$typeHint.'$value)'; |
324
|
1 |
|
$class .= "\n\t" . '{'; |
325
|
1 |
|
$class .= "\n\t\t" . 'return parent::set($value, \'' . $original . '\');'; |
326
|
1 |
|
$class .= "\n\t" . '}' . "\n"; |
327
|
|
|
|
328
|
1 |
|
return $class; |
329
|
|
|
} |
330
|
|
|
} |
331
|
|
|
|