|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Crocodicstudio\Crudbooster\Modules\ModuleGenerator; |
|
4
|
|
|
|
|
5
|
|
|
class ScaffoldingParser |
|
6
|
|
|
{ |
|
7
|
|
|
public static function parse($code, $type = 'form') |
|
8
|
|
|
{ |
|
9
|
|
|
$colsItem = self::extractLines($code, $type); |
|
10
|
|
|
|
|
11
|
|
|
foreach ($colsItem as &$item) { |
|
12
|
|
|
$item = self::removeExtraCharacters($item); |
|
13
|
|
|
$strSplit = str_split($item); |
|
14
|
|
|
$strSplit = self::putDelimiter($strSplit); |
|
15
|
|
|
$item = implode("", $strSplit); |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
foreach ($colsItem as &$col) { |
|
19
|
|
|
$col = self::prepareFields(explode('|SPLIT|', $col)); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
$colsItem = self::formOptions($type, $colsItem); |
|
23
|
|
|
|
|
24
|
|
|
return $colsItem; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param $code |
|
29
|
|
|
* @param $type |
|
30
|
|
|
* @return array |
|
31
|
|
|
*/ |
|
32
|
|
|
private static function extractLines($code, $type) |
|
33
|
|
|
{ |
|
34
|
|
|
$mark = 'COLUMNS'; |
|
35
|
|
|
if ($type == 'form') { |
|
36
|
|
|
$mark = 'FORM'; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
$cols = self::extract_unit($code, cbStartMarker($mark), cbEndMarker($mark)); |
|
40
|
|
|
$cols = str_replace('"', "'", $cols); |
|
41
|
|
|
$cols = trim(str_replace('$this->'.$type.' = [];', '', $cols)); |
|
42
|
|
|
$colsItem = explode('$this->'.$type.'[] = ', $cols); |
|
43
|
|
|
|
|
44
|
|
|
return array_filter($colsItem); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public static function extract_unit($string, $start, $end) |
|
48
|
|
|
{ |
|
49
|
|
|
$pos = stripos($string, $start); |
|
50
|
|
|
$str = substr($string, $pos); |
|
51
|
|
|
$str_two = substr($str, strlen($start)); |
|
52
|
|
|
$second_pos = stripos($str_two, $end); |
|
53
|
|
|
$str_three = substr($str_two, 0, $second_pos); |
|
54
|
|
|
$unit = trim($str_three); // remove whitespaces |
|
55
|
|
|
|
|
56
|
|
|
return $unit; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param $type |
|
61
|
|
|
* @param $colsItem |
|
62
|
|
|
* @return mixed |
|
63
|
|
|
*/ |
|
64
|
|
|
private static function formOptions($type, $colsItem) |
|
65
|
|
|
{ |
|
66
|
|
|
if ($type !== 'form') { |
|
67
|
|
|
return $colsItem; |
|
68
|
|
|
} |
|
69
|
|
|
foreach ($colsItem as $i => $form) { |
|
70
|
|
|
if ( empty($form['options']) !== false && $form['options'] !== '[]') { |
|
71
|
|
|
@eval("\$options = $form[options];"); |
|
|
|
|
|
|
72
|
|
|
@$colsItem[$i]['options'] = $options; |
|
|
|
|
|
|
73
|
|
|
} else { |
|
74
|
|
|
$colsItem[$i]['options'] = []; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return $colsItem; |
|
79
|
|
|
|
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param $s |
|
84
|
|
|
* @return array |
|
85
|
|
|
*/ |
|
86
|
|
|
private static function parseCallback($s) |
|
87
|
|
|
{ |
|
88
|
|
|
$s = str_replace("return", "return ", $s); |
|
89
|
|
|
$val = trim(str_replace(["'callback'=>function(\$row) {", "'callback'=>function(\$row){"], "", $s)); |
|
90
|
|
|
$val = substr($val, 0, -1); |
|
91
|
|
|
|
|
92
|
|
|
return $val; //to remove last } |
|
|
|
|
|
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @param $split |
|
97
|
|
|
* @return array |
|
98
|
|
|
*/ |
|
99
|
|
|
private static function prepareFields($split) |
|
100
|
|
|
{ |
|
101
|
|
|
$colInnerItem = []; |
|
102
|
|
|
foreach ($split as $s) { |
|
103
|
|
|
if (strpos($s, 'options') !== false) { |
|
104
|
|
|
$colInnerItem['options'] = trim(str_replace("'options'=>", "", $s)); |
|
105
|
|
|
continue; |
|
106
|
|
|
} |
|
107
|
|
|
if (strpos($s, 'callback') !== false) { |
|
108
|
|
|
$colInnerItem['callback'] = self::parseCallback($s); |
|
109
|
|
|
continue; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
$s = str_replace("'", '',$s); |
|
113
|
|
|
list($key, $val) = explode('=>', $s); |
|
114
|
|
|
$colInnerItem[$key] = $val; |
|
115
|
|
|
|
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
return $colInnerItem; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @param $item |
|
123
|
|
|
* @return mixed|string |
|
124
|
|
|
*/ |
|
125
|
|
|
private static function removeExtraCharacters($item) |
|
126
|
|
|
{ |
|
127
|
|
|
$item = str_replace(' ', '', $item); |
|
128
|
|
|
// "['label'=>'KanapeType','name'=>'kanape_type',];\r\n" |
|
129
|
|
|
|
|
130
|
|
|
$item = str_replace('\',]', ']', $item); // replaces: ',] with ] |
|
131
|
|
|
// "['label'=>'KanapeType','name'=>'kanape_type];\r\n" |
|
132
|
|
|
|
|
133
|
|
|
$item = trim($item); |
|
134
|
|
|
// "['label'=>'KanapeType','name'=>'kanape_type];" |
|
135
|
|
|
|
|
136
|
|
|
$item = trim($item, '['); |
|
137
|
|
|
// "'label'=>'KanapeType','name'=>'kanape_type];" |
|
138
|
|
|
|
|
139
|
|
|
$item = trim($item, '];'); |
|
140
|
|
|
// "'label'=>'KanapeType','name'=>'kanape_type" |
|
141
|
|
|
|
|
142
|
|
|
$item = trim($item); |
|
143
|
|
|
$item = trim(preg_replace("/[\n\r\t]/", "", $item)); |
|
144
|
|
|
|
|
145
|
|
|
return $item; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* @param $strSplit |
|
150
|
|
|
* @return mixed |
|
151
|
|
|
*/ |
|
152
|
|
|
private static function putDelimiter($strSplit) |
|
153
|
|
|
{ |
|
154
|
|
|
$innerCount = 0; |
|
155
|
|
|
foreach ($strSplit as $index => $s) { |
|
156
|
|
|
if ($s == '[') { |
|
157
|
|
|
$innerCount++; |
|
158
|
|
|
} |
|
159
|
|
|
if ($s == ']') { |
|
160
|
|
|
$innerCount--; |
|
161
|
|
|
} |
|
162
|
|
|
if ($innerCount == 0 && $s == ',' && $strSplit[$index + 1] == "'") { |
|
163
|
|
|
$strSplit[$index] = "|SPLIT|"; |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
return $strSplit; |
|
168
|
|
|
} |
|
169
|
|
|
} |