annotate src/Helpers/FileModifier.php @ 12:4bb4daa9e3f1 development

Working with the new FileModifier
author luka
date Wed, 24 Apr 2024 19:52:35 -0400
parents
children c969ed13c570
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
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
3 namespace Wizzard\MagicForger\Helpers;
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 /**
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
23 * @var mixed
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
24 */
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
25 public $file_path;
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
26
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
27
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
28 /**
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
29 * @param mixed $file_path
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
30 */
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
31 public function __construct($file_path)
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
32 {
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
33 $this->get_file_contents($file_path);
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
34 $this->file_path = $file_path;
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
35 }
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 /**
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
39 * @param mixed $file_path
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
40 * @return void
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
41 */
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
42 public function get_file_contents($file_path): void
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
43 {
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
44 //TODO: there needs to be more/any error checking
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
45 $f = fopen($file_path, "r");
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
46 $this->contents = fread($f, filesize($file_path));
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
47 fclose($f);
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
48 }
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
49
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
50
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
51 /**
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
52 * @param mixed $file_path
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
53 */
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
54 public function write_to_path($file_path = null): void
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
55 {
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
56 $file_path = $file_path ?? $this->file_path;
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
57
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
58 $f = fopen($file_path, "w");
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
59 fwrite($f, $this->contents);
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
60 fclose($f);
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
61 }
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
62
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
63
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
64 /**
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
65 * Replaces the replacement point with the value in the current contents
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
66 *
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
67 * @param mixed $value
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
68 * @param mixed $replacement_point
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
69 */
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
70 public function replace($value, $replacement_point): void
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
71 {
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
72 $this->contents = str_replace($replacement_point, $value, $this->contents);
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
73 }
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
74
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
75
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
76 /**
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
77 * Inserts the value above the insert point in the current contents
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
78 *
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
79 * @param mixed $value
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
80 * @param mixed $insert_point
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
81 * @return void
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
82 */
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
83 public function insert($value, $insert_point): void
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
84 {
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
85 //seperate on new lines into an array
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
86 $file_arr = explode("\n", $this->contents);
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
87 $temp_arr = [];
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
88
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
89 foreach($file_arr as $line) {
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
90 if(str_contains($line, $insert_point)) {
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
91 $temp_arr[] = $value;
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
92 }
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
93 $temp_arr[] = $line;
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
94 }
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
95
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
96 $this->contents = implode("\n",$temp_arr);
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
97 }
4bb4daa9e3f1 Working with the new FileModifier
luka
parents:
diff changeset
98 }