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
|
|
|
if ($tagBuka == end($methodIndex)) { |
104
|
|
|
return $tagPentutups[count($tagPentutups) - 2]; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
foreach ($tagPentutups as $i => $tp) { |
108
|
|
|
if ($tp > $methodIndex[$methodNextIndex]) { |
109
|
|
|
$finalTagPenutup = $tagPentutups[$i - 1]; |
110
|
|
|
break; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
|
115
|
|
|
return $finalTagPenutup; |
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param $line |
120
|
|
|
* @param $e |
121
|
|
|
* @param $tagPentutups |
122
|
|
|
* @return mixed |
123
|
|
|
*/ |
124
|
|
|
private static function tagPentutups($line, $e, $tagPentutups) |
125
|
|
|
{ |
126
|
|
|
if (stripos($line, '}') !== false) { |
127
|
|
|
$tagPentutups[$e] = $e; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
if (stripos($line, '{') !== false) { |
131
|
|
|
$tagPembukas[$e] = $e; |
|
|
|
|
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $tagPentutups; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param $line |
139
|
|
|
* @param $i |
140
|
|
|
* @param $methodIndex |
141
|
|
|
* @return array |
142
|
|
|
*/ |
143
|
|
|
private static function methodIndex($line, $i, $methodIndex) |
144
|
|
|
{ |
145
|
|
|
foreach (['public', 'private'] as $m) { |
146
|
|
|
if (strpos($line, $m) !== false) { |
147
|
|
|
$methodIndex[] = $i; |
148
|
|
|
break; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
if (strpos($line, ' function ') !== false) { |
153
|
|
|
$methodIndex[] = $i; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return $methodIndex; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param $methodIndex |
161
|
|
|
* @param $tagBuka |
162
|
|
|
* @return array |
163
|
|
|
*/ |
164
|
|
|
private static function tagBuka($methodIndex, $tagBuka) |
165
|
|
|
{ |
166
|
|
|
$methodIndex = array_values(array_unique($methodIndex)); |
167
|
|
|
|
168
|
|
|
$keyTagBukaInMethodIndex = array_search($tagBuka, $methodIndex); |
169
|
|
|
$totalMethodIndex = count($methodIndex) - 1; |
170
|
|
|
$methodNextIndex = ($totalMethodIndex == $keyTagBukaInMethodIndex) ? $keyTagBukaInMethodIndex : $keyTagBukaInMethodIndex + 1; |
171
|
|
|
|
172
|
|
|
return [$methodIndex, $methodNextIndex]; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @param $codeArray array |
177
|
|
|
* @param $tagBuka int |
178
|
|
|
* @param $finalTagPenutup int |
179
|
|
|
* @return array |
180
|
|
|
*/ |
181
|
|
|
private static function removeMethodContent($codeArray, $tagBuka, $finalTagPenutup) |
182
|
|
|
{ |
183
|
|
|
foreach ($codeArray as $i => $c) { |
184
|
|
|
if ($i > $tagBuka && $i < $finalTagPenutup) { |
185
|
|
|
unset($codeArray[$i]); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
return $codeArray; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
static function putCtrlContent($ctrl, $fileContent) |
|
|
|
|
192
|
|
|
{ |
193
|
|
|
return file_put_contents(controller_path($ctrl), $fileContent); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
static function readCtrlContent($ctrl) |
|
|
|
|
197
|
|
|
{ |
198
|
|
|
return file_get_contents(controller_path($ctrl)); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
public static function extractBetween($raw, $mark) |
202
|
|
|
{ |
203
|
|
|
list($before, $_rest) = explode("# START $mark DO NOT REMOVE THIS LINE", $raw); |
204
|
|
|
list($_middle, $after) = explode("# END $mark DO NOT REMOVE THIS LINE", $_rest); |
205
|
|
|
|
206
|
|
|
return [trim($before), trim($_middle), trim($after)]; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @param $phpCode |
211
|
|
|
* @param $mark |
212
|
|
|
* @param $newCode |
213
|
|
|
* @return string |
214
|
|
|
*/ |
215
|
|
|
public static function replaceBetweenMark($phpCode, $mark, $newCode) |
216
|
|
|
{ |
217
|
|
|
list($top, $_middle, $bottom) = self::extractBetween($phpCode, $mark); |
218
|
|
|
|
219
|
|
|
$_code = $top."\n\n"; |
220
|
|
|
$_code .= " # START $mark DO NOT REMOVE THIS LINE\n"; |
221
|
|
|
$_code .= $newCode."\n"; |
222
|
|
|
$_code .= " # END $mark DO NOT REMOVE THIS LINE\n\n"; |
223
|
|
|
$_code .= ' '.$bottom; |
224
|
|
|
|
225
|
|
|
return $_code; |
226
|
|
|
} |
227
|
|
|
} |
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.