1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace crocodicstudio\crudbooster\Modules\ModuleGenerator; |
4
|
|
|
|
5
|
|
|
class ScaffoldingParser |
6
|
|
|
{ |
7
|
|
|
static function parse($code, $type = 'form') |
|
|
|
|
8
|
|
|
{ |
9
|
|
|
$colsItem = self::extractLines($code, $type); |
10
|
|
|
|
11
|
|
|
foreach ($colsItem as &$item) { |
12
|
|
|
$item = str_replace(' ','', $item); |
13
|
|
|
$item = str_replace('\',]',']', $item); |
14
|
|
|
$item = trim($item); |
15
|
|
|
$item = trim($item, '['); |
16
|
|
|
$item = trim($item, '];'); |
17
|
|
|
$item = trim($item); |
18
|
|
|
$item = trim(preg_replace("/[\n\r\t]/", "", $item)); |
19
|
|
|
$strSplit = str_split($item); |
20
|
|
|
$innerCount = 0; |
21
|
|
|
foreach ($strSplit as $index => $s) { |
22
|
|
|
if ($s == '[') { |
23
|
|
|
$innerCount++; |
24
|
|
|
} |
25
|
|
|
if ($s == ']') { |
26
|
|
|
$innerCount--; |
27
|
|
|
} |
28
|
|
|
if ($innerCount == 0 && $s == ',' && $strSplit[$index + 1] == "'") { |
29
|
|
|
$strSplit[$index] = "|SPLIT|"; |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
$item = implode("", $strSplit); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
foreach ($colsItem as &$col) { |
36
|
|
|
$colInnerItem = self::prepareFields(explode('|SPLIT|', $col)); |
37
|
|
|
$col = $colInnerItem; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
self::formOptions($type, $colsItem); |
41
|
|
|
|
42
|
|
|
return $colsItem; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param $code |
47
|
|
|
* @param $type |
48
|
|
|
* @return array |
49
|
|
|
*/ |
50
|
|
|
private static function extractLines($code, $type) |
51
|
|
|
{ |
52
|
|
|
if ($type == 'form') { |
53
|
|
|
$d = 'FORM'; |
54
|
|
|
} elseif ($type == 'col') { |
55
|
|
|
$d = 'COLUMNS'; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$cols = self::extract_unit($code, "# START $d DO NOT REMOVE THIS LINE", "# END $d DO NOT REMOVE THIS LINE"); |
|
|
|
|
59
|
|
|
$cols = str_replace('"', "'", $cols); |
60
|
|
|
$cols = trim(str_replace('$this->'.$type.' = [];', '', $cols)); |
61
|
|
|
$colsItem = explode('$this->'.$type.'[] = ', $cols); |
62
|
|
|
|
63
|
|
|
return array_filter($colsItem); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
static function extract_unit($string, $start, $end) |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
$pos = stripos($string, $start); |
69
|
|
|
$str = substr($string, $pos); |
70
|
|
|
$str_two = substr($str, strlen($start)); |
71
|
|
|
$second_pos = stripos($str_two, $end); |
72
|
|
|
$str_three = substr($str_two, 0, $second_pos); |
73
|
|
|
$unit = trim($str_three); // remove whitespaces |
74
|
|
|
|
75
|
|
|
return $unit; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param $type |
80
|
|
|
* @param $colsItem |
81
|
|
|
* @return mixed |
82
|
|
|
*/ |
83
|
|
|
private static function formOptions($type, $colsItem) |
84
|
|
|
{ |
85
|
|
|
foreach ($colsItem as &$form) { |
86
|
|
|
if ($type !== 'form') { |
87
|
|
|
continue; |
88
|
|
|
} |
89
|
|
|
if ($form['options']) { |
90
|
|
|
@eval("\$options = $form[options];"); |
|
|
|
|
91
|
|
|
@$form['options'] = $options; |
|
|
|
|
92
|
|
|
} else { |
93
|
|
|
$form['options'] = []; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param $s |
100
|
|
|
* @return array |
101
|
|
|
*/ |
102
|
|
|
private static function parseCallback($s) |
103
|
|
|
{ |
104
|
|
|
$s = str_replace("return", "return ", $s); |
105
|
|
|
$val = trim(str_replace(["'callback'=>function(\$row) {", "'callback'=>function(\$row){"], "", $s)); |
106
|
|
|
$val = substr($val, 0, -1); |
107
|
|
|
|
108
|
|
|
return $val; //to remove last } |
|
|
|
|
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param $split |
113
|
|
|
* @return array |
114
|
|
|
*/ |
115
|
|
|
private static function prepareFields($split) |
116
|
|
|
{ |
117
|
|
|
$colInnerItem = []; |
118
|
|
|
foreach ($split as $s) { |
119
|
|
|
if (strpos($s, 'options') !== false) { |
120
|
|
|
$colInnerItem['options'] = trim(str_replace("'options'=>", "", $s)); |
121
|
|
|
} elseif (strpos($s, 'callback') !== false) { |
122
|
|
|
$colInnerItem['callback'] = self::parseCallback($s); |
123
|
|
|
} else { |
124
|
|
|
$s = trim($s, "'"); |
125
|
|
|
$sSplit = explode('=>', $s); |
126
|
|
|
$colInnerItem[$sSplit[0]] = $sSplit[1]; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $colInnerItem; |
131
|
|
|
} |
132
|
|
|
} |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.