1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace crocodicstudio\crudbooster\Modules\ModuleGenerator; |
4
|
|
|
|
5
|
|
|
class FileManipulator |
6
|
|
|
{ |
7
|
|
|
static function readMethodContent($code, $functionToFind) |
|
|
|
|
8
|
|
|
{ |
9
|
|
|
$codeArray = explode("\n", $code); |
10
|
|
|
$tagBuka = 0; |
11
|
|
|
$tagPentutups = []; |
12
|
|
|
$methodIndex = []; |
13
|
|
|
foreach ($codeArray as $i => &$line) { |
14
|
|
|
if (strpos($line, 'function '.$functionToFind.'(') !== false) { |
15
|
|
|
$tagBuka = $i; |
16
|
|
|
} |
17
|
|
|
$tagPentutups = self::tagPentutups($line, $i, $tagPentutups); |
18
|
|
|
$methodIndex = self::methodIndex($line, $i, $methodIndex); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
list($methodIndex, $methodNextIndex) = self::tagBuka($methodIndex, $tagBuka); |
22
|
|
|
|
23
|
|
|
$tagPentutups = array_values($tagPentutups); |
24
|
|
|
|
25
|
|
|
$finalTagPenutup = self::finalTagPenutup($tagBuka, $tagPentutups, $methodIndex, $methodNextIndex); |
26
|
|
|
|
27
|
|
|
$content = self::getContents($codeArray, $tagBuka, $finalTagPenutup); |
28
|
|
|
|
29
|
|
|
return implode("\n", $content); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
static function writeMethodContent($code, $functionToFind, $stringToInsert) |
|
|
|
|
34
|
|
|
{ |
35
|
|
|
$codeArray = explode("\n", $code); |
36
|
|
|
$tagBuka = 0; |
37
|
|
|
$tagPentutups = []; |
38
|
|
|
$methodIndex = []; |
39
|
|
|
$indentCount = 0; |
40
|
|
|
|
41
|
|
|
foreach ($codeArray as $i => &$line) { |
42
|
|
|
if (strpos($line, 'function '.$functionToFind.'(') !== false) { |
43
|
|
|
$tagBuka = $i; |
44
|
|
|
$indentCount = substr_count($line, " "); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$tagPentutups = self::tagPentutups($line, $i, $tagPentutups); |
48
|
|
|
$methodIndex = self::methodIndex($line, $i, $methodIndex); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
list($methodIndex, $methodNextIndex) = self::tagBuka($methodIndex, $tagBuka); |
52
|
|
|
|
53
|
|
|
$tagPentutups = array_values($tagPentutups); |
54
|
|
|
|
55
|
|
|
$finalTagPenutup = self::finalTagPenutup($tagBuka, $tagPentutups, $methodIndex, $methodNextIndex); |
56
|
|
|
|
57
|
|
|
//Removing Content Of Method |
58
|
|
|
$codeArray = self::removeMethodContent($codeArray, $tagBuka, $finalTagPenutup); |
59
|
|
|
|
60
|
|
|
//Insert Content To Method |
61
|
|
|
$stringToInsertArray = explode("\n", trim($stringToInsert)); |
62
|
|
|
foreach ($stringToInsertArray as $i => &$s) { |
63
|
|
|
$s = str_repeat(' ', $indentCount + 2).trim($s); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$stringToInsert = implode("\n", $stringToInsertArray); |
67
|
|
|
foreach ($codeArray as $i => $c) { |
68
|
|
|
if ($i == $tagBuka) { |
69
|
|
|
array_splice($codeArray, $i + 1, 0, [$stringToInsert]); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return implode("\n", $codeArray); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param $codeArray |
78
|
|
|
* @param $tagBuka |
79
|
|
|
* @param $finalTagPenutup |
80
|
|
|
* @return array |
81
|
|
|
*/ |
82
|
|
|
private static function getContents($codeArray, $tagBuka, $finalTagPenutup) |
83
|
|
|
{ |
84
|
|
|
$content = []; |
85
|
|
|
foreach ($codeArray as $i => $line) { |
86
|
|
|
if ($i > $tagBuka && $i < $finalTagPenutup) { |
87
|
|
|
$content[] = $line; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $content; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param $tagBuka |
96
|
|
|
* @param $tagPentutups |
97
|
|
|
* @param $methodIndex |
98
|
|
|
* @param $methodNextIndex |
99
|
|
|
* @return mixed |
100
|
|
|
*/ |
101
|
|
|
private static function finalTagPenutup($tagBuka, $tagPentutups, $methodIndex, $methodNextIndex) |
102
|
|
|
{ |
103
|
|
|
$finalTagPenutup = 0; |
104
|
|
|
|
105
|
|
|
if ($tagBuka == end($methodIndex)) { |
106
|
|
|
return $tagPentutups[count($tagPentutups) - 2]; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
foreach ($tagPentutups as $i => $tp) { |
110
|
|
|
if ($tp > $methodIndex[$methodNextIndex]) { |
111
|
|
|
$finalTagPenutup = $tagPentutups[$i - 1]; |
112
|
|
|
break; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
|
117
|
|
|
return $finalTagPenutup; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param $line |
122
|
|
|
* @param $e |
123
|
|
|
* @param $tagPentutups |
124
|
|
|
* @return mixed |
125
|
|
|
*/ |
126
|
|
|
private static function tagPentutups($line, $e, $tagPentutups) |
127
|
|
|
{ |
128
|
|
|
if (stripos($line, '}') !== false) { |
129
|
|
|
$tagPentutups[$e] = $e; |
130
|
|
|
} |
131
|
|
|
$tagPembukas = []; |
132
|
|
|
if (stripos($line, '{') !== false) { |
133
|
|
|
$tagPembukas[$e] = $e; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return $tagPentutups; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param $line |
141
|
|
|
* @param $i |
142
|
|
|
* @param $methodIndex |
143
|
|
|
* @return array |
144
|
|
|
*/ |
145
|
|
|
private static function methodIndex($line, $i, $methodIndex) |
146
|
|
|
{ |
147
|
|
|
foreach (['public', 'private'] as $m) { |
148
|
|
|
if (strpos($line, $m) !== false) { |
149
|
|
|
$methodIndex[] = $i; |
150
|
|
|
break; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
if (strpos($line, ' function ') !== false) { |
155
|
|
|
$methodIndex[] = $i; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return $methodIndex; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @param $methodIndex |
163
|
|
|
* @param $tagBuka |
164
|
|
|
* @return array |
165
|
|
|
*/ |
166
|
|
|
private static function tagBuka($methodIndex, $tagBuka) |
167
|
|
|
{ |
168
|
|
|
$methodIndex = array_values(array_unique($methodIndex)); |
169
|
|
|
|
170
|
|
|
$keyTagBukaInMethodIndex = array_search($tagBuka, $methodIndex); |
171
|
|
|
$totalMethodIndex = count($methodIndex) - 1; |
172
|
|
|
$methodNextIndex = ($totalMethodIndex == $keyTagBukaInMethodIndex) ? $keyTagBukaInMethodIndex : $keyTagBukaInMethodIndex + 1; |
173
|
|
|
|
174
|
|
|
return [$methodIndex, $methodNextIndex]; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @param $codeArray array |
179
|
|
|
* @param $tagBuka int |
180
|
|
|
* @param $finalTagPenutup int |
181
|
|
|
* @return array |
182
|
|
|
*/ |
183
|
|
|
private static function removeMethodContent($codeArray, $tagBuka, $finalTagPenutup) |
184
|
|
|
{ |
185
|
|
|
foreach ($codeArray as $i => $c) { |
186
|
|
|
if ($i > $tagBuka && $i < $finalTagPenutup) { |
187
|
|
|
unset($codeArray[$i]); |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
return $codeArray; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
static function putCtrlContent($ctrl, $fileContent) |
|
|
|
|
194
|
|
|
{ |
195
|
|
|
return file_put_contents(controller_path($ctrl), $fileContent); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
static function readCtrlContent($ctrl) |
|
|
|
|
199
|
|
|
{ |
200
|
|
|
return file_get_contents(controller_path($ctrl)); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
public static function extractBetween($raw, $mark) |
204
|
|
|
{ |
205
|
|
|
list($before, $_rest) = explode("# START $mark DO NOT REMOVE THIS LINE", $raw); |
206
|
|
|
list($_middle, $after) = explode("# END $mark DO NOT REMOVE THIS LINE", $_rest); |
207
|
|
|
|
208
|
|
|
return [trim($before), trim($_middle), trim($after)]; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* @param $phpCode |
213
|
|
|
* @param $mark |
214
|
|
|
* @param $newCode |
215
|
|
|
* @return string |
216
|
|
|
*/ |
217
|
|
|
public static function replaceBetweenMark($phpCode, $mark, $newCode) |
218
|
|
|
{ |
219
|
|
|
list($top, $_middle, $bottom) = self::extractBetween($phpCode, $mark); |
220
|
|
|
|
221
|
|
|
$_code = $top."\n\n"; |
222
|
|
|
$_code .= " # START $mark DO NOT REMOVE THIS LINE\n"; |
223
|
|
|
$_code .= $newCode."\n"; |
224
|
|
|
$_code .= " # END $mark DO NOT REMOVE THIS LINE\n\n"; |
225
|
|
|
$_code .= ' '.$bottom; |
226
|
|
|
|
227
|
|
|
return $_code; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
public static function stringify($input, $indent = "") |
231
|
|
|
{ |
232
|
|
|
if (! is_array($input)) { |
233
|
|
|
return var_export($input, true); |
234
|
|
|
} |
235
|
|
|
$buffer = []; |
236
|
|
|
foreach ($input as $key => $value) { |
237
|
|
|
$buffer[] = $indent.var_export($key, true)."=>".min_var_export($value, ($indent." ")); |
|
|
|
|
238
|
|
|
} |
239
|
|
|
if (empty($buffer)) { |
240
|
|
|
return "[]"; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
return "[\n".implode(",\n", $buffer)."\n$indent]"; |
244
|
|
|
} |
245
|
|
|
} |
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.