Repository navigation
Expand file tree
/
Copy pathModelFileParser.php
More file actions
186 lines (150 loc) · 5.01 KB
/
Copy pathModelFileParser.php
File metadata and controls
186 lines (150 loc) · 5.01 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
<?php namespace RainLab\Builder\Classes;
/**
* Parses models source files.
*
* @package rainlab\builder
* @author Alexey Bobkov, Samuel Georges
*/
class ModelFileParser
{
/**
* Returns the model namespace, class name and table name.
* @param string $fileContents Specifies the file contents.
* @return array|null Returns an array with keys 'namespace', 'class' and 'table'
* Returns null if the parsing fails.
*/
public function extractModelInfoFromSource($fileContents)
{
$stream = new PhpSourceStream($fileContents);
$result = [];
while ($stream->forward()) {
$tokenCode = $stream->getCurrentCode();
if ($tokenCode == T_NAMESPACE) {
$namespace = $this->extractNamespace($stream);
if ($namespace === null) {
return null;
}
$result['namespace'] = $namespace;
}
if ($tokenCode == T_CLASS && !isset($result['class'])) {
$className = $this->extractClassName($stream);
if ($className === null) {
return null;
}
$result['class'] = $className;
}
if ($tokenCode == T_PUBLIC || $tokenCode == T_PROTECTED) {
$tableName = $this->extractTableName($stream);
if ($tableName === false) {
continue;
}
if ($tableName === null) {
return null;
}
$result['table'] = $tableName;
}
}
if (!$result) {
return null;
}
return $result;
}
/**
* Extracts names and types of model relations.
* @param string $fileContents Specifies the file contents.
* @return array|null Returns an array with keys matching the relation types and values containing relation names as array.
* Returns null if the parsing fails.
*/
public function extractModelRelationsFromSource($fileContents)
{
$result = [];
$stream = new PhpSourceStream($fileContents);
while ($stream->forward()) {
$tokenCode = $stream->getCurrentCode();
if ($tokenCode == T_PUBLIC) {
$relations = $this->extractRelations($stream);
if ($relations === false) {
continue;
}
}
}
if (!$result) {
return null;
}
return $result;
}
protected function extractNamespace($stream)
{
if ($stream->getNextExpected(T_WHITESPACE) === null) {
return null;
}
return $stream->getNextExpectedTerminated([T_STRING, T_NS_SEPARATOR], [T_WHITESPACE, ';']);
}
protected function extractClassName($stream)
{
if ($stream->getNextExpected(T_WHITESPACE) === null) {
return null;
}
return $stream->getNextExpectedTerminated([T_STRING], [T_WHITESPACE, ';']);
}
/**
* Returns the table name. This method would return null in case if the
* $table variable was found, but it value cannot be read. If the variable
* is not found, the method returns false, allowing the outer loop to go to
* the next token.
*/
protected function extractTableName($stream)
{
if ($stream->getNextExpected(T_WHITESPACE) === null) {
return false;
}
if ($stream->getNextExpected(T_VARIABLE) === null) {
return false;
}
if ($stream->getCurrentText() != '$table') {
return false;
}
if ($stream->getNextExpectedTerminated(['=', T_WHITESPACE], [T_CONSTANT_ENCAPSED_STRING]) === null) {
return null;
}
$tableName = $stream->getCurrentText();
$tableName = trim($tableName, '\'');
$tableName = trim($tableName, '"');
return $tableName;
}
protected function extractRelations($stream)
{
if ($stream->getNextExpected(T_WHITESPACE) === null) {
return false;
}
if ($stream->getNextExpected(T_VARIABLE) === null) {
return false;
}
$relationTypes = [
'belongsTo',
'belongsToMany',
'attachMany',
'hasMany',
'morphToMany',
'morphedByMany',
'morphMany',
'hasManyThrough'
];
$relationType = null;
$currentText = $stream->getCurrentText();
foreach ($relationTypes as $type) {
if ($currentText == '$'.$type) {
$relationType = $type;
break;
}
}
if (!$relationType) {
return false;
}
if ($stream->getNextExpectedTerminated(['=', T_WHITESPACE], ['[']) === null) {
return null;
}
// The implementation is not finished and postponed. Relation definition could
// be quite complex and contain nested arrays.
}
}