Mercurial > packages > magicforger
comparison src/Helpers/FileModifier.php @ 14:c969ed13c570 main-dev
Changes for various files
| author | luka |
|---|---|
| date | Mon, 23 Sep 2024 20:35:14 -0400 |
| parents | 4bb4daa9e3f1 |
| children | 19b7a8de0019 |
comparison
equal
deleted
inserted
replaced
| 13:7ee152c22478 | 14:c969ed13c570 |
|---|---|
| 17 * */ | 17 * */ |
| 18 class FileModifier | 18 class FileModifier |
| 19 { | 19 { |
| 20 private $contents; | 20 private $contents; |
| 21 | 21 |
| 22 /** | |
| 23 * @var mixed | |
| 24 */ | |
| 25 public $file_path; | 22 public $file_path; |
| 26 | 23 |
| 27 | |
| 28 /** | |
| 29 * @param mixed $file_path | |
| 30 */ | |
| 31 public function __construct($file_path) | 24 public function __construct($file_path) |
| 32 { | 25 { |
| 33 $this->get_file_contents($file_path); | 26 $this->get_file_contents($file_path); |
| 34 $this->file_path = $file_path; | 27 $this->file_path = $file_path; |
| 35 } | 28 } |
| 36 | 29 |
| 37 | |
| 38 /** | |
| 39 * @param mixed $file_path | |
| 40 * @return void | |
| 41 */ | |
| 42 public function get_file_contents($file_path): void | 30 public function get_file_contents($file_path): void |
| 43 { | 31 { |
| 44 //TODO: there needs to be more/any error checking | 32 // TODO: there needs to be more/any error checking |
| 45 $f = fopen($file_path, "r"); | 33 $f = fopen($file_path, 'r'); |
| 46 $this->contents = fread($f, filesize($file_path)); | 34 $this->contents = fread($f, filesize($file_path)); |
| 47 fclose($f); | 35 fclose($f); |
| 48 } | 36 } |
| 49 | 37 |
| 50 | |
| 51 /** | |
| 52 * @param mixed $file_path | |
| 53 */ | |
| 54 public function write_to_path($file_path = null): void | 38 public function write_to_path($file_path = null): void |
| 55 { | 39 { |
| 56 $file_path = $file_path ?? $this->file_path; | 40 $file_path = $file_path ?? $this->file_path; |
| 57 | 41 |
| 58 $f = fopen($file_path, "w"); | 42 $f = fopen($file_path, 'w'); |
| 59 fwrite($f, $this->contents); | 43 fwrite($f, $this->contents); |
| 60 fclose($f); | 44 fclose($f); |
| 61 } | 45 } |
| 62 | 46 |
| 63 | |
| 64 /** | 47 /** |
| 65 * Replaces the replacement point with the value in the current contents | 48 * Replaces the replacement point with the value in the current contents. |
| 66 * | |
| 67 * @param mixed $value | |
| 68 * @param mixed $replacement_point | |
| 69 */ | 49 */ |
| 70 public function replace($value, $replacement_point): void | 50 public function replace($value, $replacement_point): void |
| 71 { | 51 { |
| 72 $this->contents = str_replace($replacement_point, $value, $this->contents); | 52 $this->contents = str_replace($replacement_point, $value, $this->contents); |
| 73 } | 53 } |
| 74 | 54 |
| 75 | |
| 76 /** | 55 /** |
| 77 * Inserts the value above the insert point in the current contents | 56 * Inserts the value above the insert point in the current contents. |
| 78 * | |
| 79 * @param mixed $value | |
| 80 * @param mixed $insert_point | |
| 81 * @return void | |
| 82 */ | 57 */ |
| 83 public function insert($value, $insert_point): void | 58 public function insert($value, $insert_point): void |
| 84 { | 59 { |
| 85 //seperate on new lines into an array | 60 // seperate on new lines into an array |
| 86 $file_arr = explode("\n", $this->contents); | 61 $file_arr = explode("\n", $this->contents); |
| 87 $temp_arr = []; | 62 $temp_arr = []; |
| 88 | 63 |
| 89 foreach($file_arr as $line) { | 64 foreach ($file_arr as $line) { |
| 90 if(str_contains($line, $insert_point)) { | 65 if (str_contains($line, $insert_point)) { |
| 91 $temp_arr[] = $value; | 66 $temp_arr[] = $value; |
| 92 } | 67 } |
| 93 $temp_arr[] = $line; | 68 $temp_arr[] = $line; |
| 94 } | 69 } |
| 95 | 70 |
| 96 $this->contents = implode("\n",$temp_arr); | 71 $this->contents = implode("\n", $temp_arr); |
| 97 } | 72 } |
| 98 } | 73 } |
