forked from bezhanSalleh/filament-shield
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringer.php
502 lines (408 loc) · 17 KB
/
Stringer.php
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
<?php
/** @noinspection PhpSuspiciousNameCombinationInspection */
declare(strict_types=1);
namespace BezhanSalleh\FilamentShield;
use Illuminate\Support\Traits\Conditionable;
class Stringer
{
use Conditionable;
protected string $content;
protected string $filePath;
protected int $baseIndentLevel = 0; // Track the base indentation level
protected int $currentIndentLevel = 0; // Track the current indentation level
protected bool $addNewLine = false; // Track whether to add a new line
public static function for(string $filePath): static
{
return app(static::class, ['filePath' => $filePath]);
}
public function __construct(string $filePath)
{
$this->filePath = $filePath;
$this->content = file_get_contents($filePath) ?: '';
}
protected function findLine(string $needle): ?array
{
// Search for the needle and return the line and its indentation
$startPos = strpos($this->content, $needle);
if ($startPos === false) {
return null; // Not found
}
// Get the start of the line and calculate indentation
$lineStartPos = strrpos(substr($this->content, 0, $startPos), PHP_EOL) ?: 0;
$lineEndPos = strpos($this->content, PHP_EOL, $startPos) ?: strlen($this->content);
$line = substr($this->content, $lineStartPos, $lineEndPos - $lineStartPos);
$indentation = preg_replace('/\S.*/', '', $line); // Capture indentation
return [
'start' => $lineStartPos,
'end' => $lineEndPos,
'indentation' => $indentation,
];
}
public function prepend(string $needle, string $contentToPrepend, bool $beforeBlock = false): static
{
if (! $this->contains($needle)) {
return $this; // Needle not found
}
if ($beforeBlock) {
// Find the starting position of the method
$startPos = strpos($this->content, $needle);
if ($startPos === false) {
return $this; // Needle not found
}
// Find the opening parenthesis position
$openingParenPos = strpos($this->content, '(', $startPos);
if ($openingParenPos === false) {
return $this; // No opening parenthesis found
}
// Find the closing parenthesis
$closingParenPos = $this->findClosingParen($openingParenPos);
if (is_null($closingParenPos)) {
return $this; // No closing parenthesis found
}
// Get the line indentation
$lineInfo = $this->findLine($needle);
$indentation = $lineInfo['indentation'] . $this->getIndentation();
// Format the new content based on the newLine flag
$formattedReplacement = $this->addNewLine
? PHP_EOL . $indentation . trim($contentToPrepend)
: $indentation . trim($contentToPrepend);
$this->addNewLine = false; // Reset flag
// Insert the formatted replacement before the opening parenthesis
$this->content = substr_replace($this->content, $formattedReplacement, $openingParenPos, 0);
} else {
// Normal prepend logic
if ($lineInfo = $this->findLine($needle)) {
// Prepend the content with proper indentation
$newContent = $lineInfo['indentation'] . $this->getIndentation() . trim($contentToPrepend);
if ($this->addNewLine) {
$newContent = PHP_EOL . $newContent;
$this->addNewLine = false; // Reset the flag
}
$this->content = substr_replace(
$this->content,
$newContent,
$lineInfo['start'],
0
);
}
}
return $this;
}
public function append(string $needle, string $contentToAppend, bool $afterBlock = false): static
{
if (! $this->contains($needle)) {
return $this; // Needle not found
}
if ($afterBlock) {
// Find the starting position of the method
$startPos = strpos($this->content, $needle);
if ($startPos === false) {
return $this; // Needle not found
}
// Find the opening parenthesis position
$openingParenPos = strpos($this->content, '(', $startPos);
if ($openingParenPos === false) {
return $this; // No opening parenthesis found
}
// Find the closing parenthesis
$closingParenPos = $this->findClosingParen($openingParenPos);
if (is_null($closingParenPos)) {
return $this; // No closing parenthesis found
}
// Get the line indentation
$lineInfo = $this->findLine($needle);
$indentation = $lineInfo['indentation'] . $this->getIndentation();
// Format the new content based on the newLine flag
$formattedReplacement = $this->addNewLine
? $indentation . trim($contentToAppend) . PHP_EOL
: $indentation . trim($contentToAppend);
$this->addNewLine = false; // Reset flag
// If the closing parenthesis has a semicolon, move it to a new line with indentation
if ($this->content[$closingParenPos + 1] === ';') {
$this->content = substr_replace(
$this->content,
PHP_EOL . $indentation . ';',
$closingParenPos + 1,
0
);
$closingParenPos += strlen(PHP_EOL . $indentation . ';'); // Adjust closing position
}
// Insert the formatted replacement after the closing parenthesis
$this->content = substr_replace($this->content, $formattedReplacement, $closingParenPos + 1, 0);
} else {
// Normal append logic
if ($lineInfo = $this->findLine($needle)) {
// Append the content with proper indentation
$newContent = $lineInfo['indentation'] . $this->getIndentation() . trim($contentToAppend);
if ($this->addNewLine) {
$newContent = $newContent . PHP_EOL;
$this->addNewLine = false; // Reset the flag
}
$this->content = substr_replace(
$this->content,
$newContent,
$lineInfo['end'],
0
);
}
}
return $this;
}
protected function findClosingParen(int $openingParenPos): ?int
{
$stack = 0;
$length = strlen($this->content);
for ($i = $openingParenPos; $i < $length; $i++) {
if ($this->content[$i] === '(') {
$stack++;
} elseif ($this->content[$i] === ')') {
$stack--;
if ($stack === 0) {
return $i; // Found the closing parenthesis
}
}
}
return null; // Closing parenthesis not found
}
public function replace(string $needle, string $replacement): static
{
if ($lineInfo = $this->findLine($needle)) {
// Replace the entire line containing the needle
$this->content = substr_replace(
$this->content,
$lineInfo['indentation'] . trim($replacement),
$lineInfo['start'],
strlen(substr($this->content, $lineInfo['start'], $lineInfo['end'] - $lineInfo['start']))
);
}
return $this;
}
public function newLine(): static
{
$this->addNewLine = true; // Set the flag to add a new line
return $this;
}
public function indent(int $level): static
{
$this->currentIndentLevel += $level;
return $this;
}
public function getIndentation(): string
{
return str_repeat(' ', $this->baseIndentLevel + $this->currentIndentLevel);
}
public function contains(string $needle): bool
{
// Check if the needle has a wildcard for partial matching
$isPartialMatch = str_starts_with($needle, '*') || str_ends_with($needle, '*');
if ($isPartialMatch) {
// Strip the `*` characters for partial matching
$needle = trim($needle, '*');
return (bool) preg_match('/' . preg_quote($needle, '/') . '/', $this->content);
} else {
// Perform an exact search
return str_contains($this->content, $needle);
}
}
public function save(): bool
{
return (bool) file_put_contents($this->filePath, $this->content);
}
public function getContent(): string
{
return $this->content;
}
public function prependBeforeLast(string $needle, string $replacement): static
{
$lastPos = strrpos($this->content, $needle);
if ($lastPos !== false) {
$lineStartPos = strrpos(substr($this->content, 0, $lastPos), PHP_EOL) ?: 0;
$line = substr($this->content, $lineStartPos, $lastPos - $lineStartPos);
preg_match('/^\s*/', $line, $matches);
$originalIndentation = $matches[0] ?? '';
$formattedReplacement = $this->getIndentation() . trim($replacement);
if ($this->addNewLine) {
$formattedReplacement = PHP_EOL . $formattedReplacement . PHP_EOL;
}
$this->addNewLine = false;
$this->content = substr_replace($this->content, $originalIndentation . $formattedReplacement, $lineStartPos, 0);
}
return $this;
}
protected function findMethodDeclaration(string $needle): ?array
{
$lines = explode(PHP_EOL, $this->content);
$normalizedNeedle = preg_replace('/\s+/', ' ', trim($needle));
for ($i = 0; $i < count($lines); $i++) {
$currentLine = trim($lines[$i]);
$nextLine = isset($lines[$i + 1]) ? trim($lines[$i + 1]) : '';
// Check if current line contains the method declaration
// and next line contains the opening brace
if (str_contains(preg_replace('/\s+/', ' ', $currentLine), $normalizedNeedle)
&& str_contains($nextLine, '{')) {
$startPos = 0;
for ($j = 0; $j < $i; $j++) {
$startPos += strlen($lines[$j]) + strlen(PHP_EOL);
}
$endPos = $startPos + strlen($lines[$i]) + strlen(PHP_EOL) + strlen($lines[$i + 1]);
$indentation = preg_replace('/\S.*/', '', $lines[$i]);
// Find the closing brace position
$braceLevel = 0;
$methodEndLine = $i + 1;
for ($j = $i + 1; $j < count($lines); $j++) {
if (str_contains($lines[$j], '{')) {
$braceLevel++;
}
if (str_contains($lines[$j], '}')) {
$braceLevel--;
if ($braceLevel === 0) {
$methodEndLine = $j;
break;
}
}
}
$methodEndPos = $startPos;
for ($j = $i; $j <= $methodEndLine; $j++) {
$methodEndPos += strlen($lines[$j]) + strlen(PHP_EOL);
}
return [
'start' => $startPos,
'end' => $endPos,
'method_end' => $methodEndPos,
'indentation' => $indentation,
'is_method' => true,
'opening_brace_line' => $i + 1,
'closing_brace_line' => $methodEndLine,
];
}
}
// Fallback to regular findLine if method declaration pattern isn't found
return $this->findLine($needle);
}
public function findChainedBlock(string $block): ?array
{
// Normalize the search block by removing extra whitespace
$normalizedBlock = preg_replace('/\s+/', ' ', trim($block));
// Split the block into individual method calls
$methodCalls = array_map('trim', explode('->', $normalizedBlock));
$lines = explode(PHP_EOL, $this->content);
$contentLength = count($lines);
for ($i = 0; $i < $contentLength; $i++) {
$matchFound = true;
$currentMethodIndex = 0;
$startLine = $i;
$endLine = $i;
// Try to match all method calls in sequence
while ($currentMethodIndex < count($methodCalls) && $endLine < $contentLength) {
$currentLine = trim($lines[$endLine]);
$normalizedLine = preg_replace('/\s+/', ' ', $currentLine);
// Check if current line contains the current method call
if (str_contains($normalizedLine, trim($methodCalls[$currentMethodIndex]))) {
$currentMethodIndex++;
$endLine++;
} elseif (! empty($currentLine)) {
// If we find a non-empty line that doesn't match, break
$matchFound = false;
break;
} else {
// Skip empty lines
$endLine++;
}
}
if ($matchFound && $currentMethodIndex === count($methodCalls)) {
// Calculate positions
$startPos = 0;
for ($j = 0; $j < $startLine; $j++) {
$startPos += strlen($lines[$j]) + strlen(PHP_EOL);
}
$endPos = $startPos;
for ($j = $startLine; $j < $endLine; $j++) {
$endPos += strlen($lines[$j]) + strlen(PHP_EOL);
}
$indentation = preg_replace('/\S.*/', '', $lines[$startLine]);
return [
'start' => $startPos,
'end' => $endPos,
'indentation' => $indentation,
'is_block' => true,
];
}
}
return null;
}
public function containsChainedBlock(string $block): bool
{
return $this->findChainedBlock($block) !== null;
}
public function appendBlock(string $needle, string $contentToAppend, bool $afterBlock = false): static
{
if (! $this->contains($needle)) {
return $this;
}
// Use findMethodDeclaration for better method handling
$lineInfo = $this->findMethodDeclaration($needle);
if (! $lineInfo) {
return $this;
}
if ($afterBlock && isset($lineInfo['is_method']) && $lineInfo['is_method']) {
// For method declarations, get the lines of content
$lines = explode(PHP_EOL, $this->content);
// Calculate proper indentation
$methodIndent = $lineInfo['indentation'];
$contentIndent = $methodIndent . str_repeat(' ', 4); // One level deeper than method
// Format the content to append
$contentLines = explode(PHP_EOL, trim($contentToAppend));
$formattedContent = '';
foreach ($contentLines as $index => $line) {
$trimmedLine = trim($line);
if (empty($trimmedLine)) {
continue;
}
$formattedContent .= ($index > 0 ? PHP_EOL . $methodIndent : '') . $contentIndent . $trimmedLine;
}
// Add new line if flag is set
if ($this->addNewLine) {
$formattedContent = $formattedContent . PHP_EOL;
$this->addNewLine = false;
}
// Find position after opening brace
$insertPos = 0;
for ($i = 0; $i <= $lineInfo['opening_brace_line']; $i++) {
$insertPos += strlen($lines[$i]) + strlen(PHP_EOL);
}
// Insert the formatted content
$this->content = substr_replace(
$this->content,
$formattedContent . PHP_EOL . "\n",
$insertPos,
0
);
} else {
// Original append logic
$newContent = $lineInfo['indentation'] . $this->getIndentation() . trim($contentToAppend);
if ($this->addNewLine) {
$newContent = PHP_EOL . $newContent;
$this->addNewLine = false;
}
$this->content = substr_replace(
$this->content,
$newContent,
$lineInfo['end'],
0
);
}
return $this;
}
public function deleteLine(string $needle): static
{
if ($lineInfo = $this->findLine($needle)) {
$this->content = substr_replace(
$this->content,
'',
$lineInfo['start'],
$lineInfo['end'] - $lineInfo['start']
);
}
return $this;
}
}