annotate src/Helpers/FileModifier.php @ 40:2cf26b593f4a ls_dev_2025_09 tip

better support for different column types
author Luka Sitas <sitas.luka.97@gmail.com>
date Thu, 16 Oct 2025 10:54:04 -0400
parents 19b7a8de0019
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
12
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
1 <?php
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
2
19
19b7a8de0019 updating namespace from typo
Luka Sitas <sitas.luka.97@gmail.com>
parents: 14
diff changeset
3 namespace Wizard\MagicForger\Helpers;
12
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
4
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
5 /*
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
6 * FileModifier
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
7 *
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
8 * A class that handles all file modifications
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
9 *
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
10 * General flow:
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
11 * Provide a file, a point to insert, and a value to insert.
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
12 * Insert the data.
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
13 *
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
14 * Replacements will consume the insert point.
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
15 * Inserts will maintain the insert point.
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
16 *
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
17 * */
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
18 class FileModifier
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
19 {
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
20 private $contents;
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
21
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
22 public $file_path;
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
23
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
24 public function __construct($file_path)
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
25 {
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
26 $this->get_file_contents($file_path);
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
27 $this->file_path = $file_path;
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
28 }
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
29
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
30 public function get_file_contents($file_path): void
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
31 {
14
c969ed13c570 Changes for various files
luka
parents: 12
diff changeset
32 // TODO: there needs to be more/any error checking
c969ed13c570 Changes for various files
luka
parents: 12
diff changeset
33 $f = fopen($file_path, 'r');
12
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
34 $this->contents = fread($f, filesize($file_path));
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
35 fclose($f);
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
36 }
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
37
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
38 public function write_to_path($file_path = null): void
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
39 {
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
40 $file_path = $file_path ?? $this->file_path;
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
41
14
c969ed13c570 Changes for various files
luka
parents: 12
diff changeset
42 $f = fopen($file_path, 'w');
12
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
43 fwrite($f, $this->contents);
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
44 fclose($f);
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
45 }
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
46
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
47 /**
14
c969ed13c570 Changes for various files
luka
parents: 12
diff changeset
48 * Replaces the replacement point with the value in the current contents.
12
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
49 */
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
50 public function replace($value, $replacement_point): void
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
51 {
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
52 $this->contents = str_replace($replacement_point, $value, $this->contents);
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
53 }
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
54
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
55 /**
14
c969ed13c570 Changes for various files
luka
parents: 12
diff changeset
56 * Inserts the value above the insert point in the current contents.
12
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
57 */
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
58 public function insert($value, $insert_point): void
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
59 {
14
c969ed13c570 Changes for various files
luka
parents: 12
diff changeset
60 // seperate on new lines into an array
12
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
61 $file_arr = explode("\n", $this->contents);
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
62 $temp_arr = [];
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
63
14
c969ed13c570 Changes for various files
luka
parents: 12
diff changeset
64 foreach ($file_arr as $line) {
c969ed13c570 Changes for various files
luka
parents: 12
diff changeset
65 if (str_contains($line, $insert_point)) {
12
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
66 $temp_arr[] = $value;
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
67 }
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
68 $temp_arr[] = $line;
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
69 }
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
70
14
c969ed13c570 Changes for various files
luka
parents: 12
diff changeset
71 $this->contents = implode("\n", $temp_arr);
12
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
72 }
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
73 }