Mercurial > packages > magicforger
comparison src/Replacer/Replacer.php @ 5:b0b2e79ad8e6
Not exatly sure what was changed but commiting to it :)
| author | luka |
|---|---|
| date | Thu, 12 Oct 2023 19:41:04 -0400 |
| parents | |
| children | 769a17898cc0 |
comparison
equal
deleted
inserted
replaced
| 4:a20439b1c9d3 | 5:b0b2e79ad8e6 |
|---|---|
| 1 <?php | |
| 2 | |
| 3 namespace Wizzard\MagicForger\Replacer; | |
| 4 | |
| 5 use Illuminate\Support\Str; | |
| 6 | |
| 7 trait Replacer | |
| 8 { | |
| 9 /** | |
| 10 * Prefix and Suffix for controller. | |
| 11 * Usage is up to the user. | |
| 12 * | |
| 13 * @var string | |
| 14 */ | |
| 15 protected $controller_prefix = ""; | |
| 16 | |
| 17 | |
| 18 /** | |
| 19 * Prefix and Suffix for controller. | |
| 20 * Usage is up to the user. | |
| 21 * | |
| 22 * @var string | |
| 23 */ | |
| 24 protected $controller_suffix = "Controller"; | |
| 25 | |
| 26 | |
| 27 /** | |
| 28 * Finds all places in a string that could be replaced. | |
| 29 * Returns an array of all potential replacements as they | |
| 30 * appear in the target. | |
| 31 */ | |
| 32 public function get_all_inserts(string $target): array | |
| 33 { | |
| 34 //find all the matches to our expected syntax | |
| 35 $matches = []; | |
| 36 preg_match_all('/{{[\sa-zA-Z\-_]+}}/', $target, $matches); | |
| 37 // sort the array and return unique values | |
| 38 sort($matches[0]); | |
| 39 return array_values(array_unique($matches[0])); | |
| 40 } | |
| 41 | |
| 42 | |
| 43 public function apply_replacements(string $target): string | |
| 44 { | |
| 45 $inserts = $this->get_all_inserts($target); | |
| 46 $available_replacements = $this->get_available_replacements(); | |
| 47 | |
| 48 $target = str_replace( | |
| 49 array_keys($available_replacements), | |
| 50 $available_replacements, | |
| 51 $target | |
| 52 ); | |
| 53 | |
| 54 return $target; | |
| 55 } | |
| 56 | |
| 57 public function get_available_replacements() | |
| 58 { | |
| 59 $table_name = $this->getTableInput(); | |
| 60 $replacements = [ | |
| 61 "{{ class }}" => $this->getClassName($table_name), | |
| 62 "{{ controllerName }}" => $this->controller_name($table_name), | |
| 63 "{{ model }}" => $this->model_name($table_name), | |
| 64 "{{ modelVariable }}" => $this->model_variable($table_name), | |
| 65 "{{ namespace }}" => $this->{'get' . $this->type . 'Namespace'}($table_name), | |
| 66 "{{ namespacedModel }}" => $this->getNamespacedModel($table_name), | |
| 67 "{{ requestUses }}" => $this->getRequestUses($table_name), | |
| 68 "{{ rootNamespace }}" => $this->getRootNamespace(), | |
| 69 "{{ storeRequest }}" => $this->store_request_name($table_name), | |
| 70 "{{ tableName }}" => $table_name, | |
| 71 "{{ updateRequest }}" => $this->update_request_name($table_name), | |
| 72 ]; | |
| 73 | |
| 74 return $replacements; | |
| 75 } | |
| 76 | |
| 77 //////////////////////////////////////////// | |
| 78 // Internals and Classes // | |
| 79 //////////////////////////////////////////// | |
| 80 | |
| 81 /** | |
| 82 * Model names are generated in uppercase first Camel case | |
| 83 */ | |
| 84 public function model_name(string $name): string | |
| 85 { | |
| 86 return Str::singular(Str::studly($name)); | |
| 87 } | |
| 88 | |
| 89 /** | |
| 90 * Model variable is standardly just a singular version of the table name | |
| 91 */ | |
| 92 public function model_variable(string $name): string | |
| 93 { | |
| 94 return Str::singular($name); | |
| 95 } | |
| 96 | |
| 97 /** | |
| 98 * Controller names are generated in uppercase first Camel case | |
| 99 * and wrapped in the prefix and suffix | |
| 100 */ | |
| 101 public function controller_name(string $name): string | |
| 102 { | |
| 103 return $this->controller_prefix . | |
| 104 $this->model_name($name) . | |
| 105 $this->controller_suffix; | |
| 106 } | |
| 107 | |
| 108 public function store_request_name(string $name): string | |
| 109 { | |
| 110 return 'Store' . $this->model_name($name) . 'Request'; | |
| 111 } | |
| 112 | |
| 113 public function update_request_name(string $name): string | |
| 114 { | |
| 115 return 'Update' . $this->model_name($name) . 'Request'; | |
| 116 } | |
| 117 | |
| 118 | |
| 119 //////////////////////////////////////////// | |
| 120 // Namespaces // | |
| 121 //////////////////////////////////////////// | |
| 122 | |
| 123 public function getRootNamespace() | |
| 124 { | |
| 125 return $this->laravel->getNamespace(); | |
| 126 } | |
| 127 | |
| 128 public function getModelNamespace(string $name = '') | |
| 129 { | |
| 130 return $this->getRootNamespace() . 'Models'; | |
| 131 } | |
| 132 | |
| 133 public function getNamespacedModel(string $name = '') | |
| 134 { | |
| 135 return $this->getModelNamespace() . '\\' . $this->model_name($name); | |
| 136 } | |
| 137 | |
| 138 public function getControllerNamespace(string $name = '') | |
| 139 { | |
| 140 return $this->getRootNamespace() . 'Http\\Controllers'; | |
| 141 } | |
| 142 | |
| 143 public function getRequestNamespace(string $name) | |
| 144 { | |
| 145 return $this->getRootNamespace() . 'Http\\Requests\\' . $this->model_name($name); | |
| 146 } | |
| 147 | |
| 148 public function getStoreRequestNamespace(string $name) | |
| 149 { | |
| 150 return $this->getRequestNamespace($name); | |
| 151 } | |
| 152 | |
| 153 public function getUpdateRequestNamespace(string $name) | |
| 154 { | |
| 155 return $this->getRequestNamespace($name); | |
| 156 } | |
| 157 | |
| 158 public function getRequestUses(string $name) | |
| 159 { | |
| 160 return implode("\n", [ | |
| 161 "use " . $this->getRequestNamespace($name) . '\\' . $this->store_request_name($name) . ';', | |
| 162 "use " . $this->getRequestNamespace($name) . '\\' . $this->update_request_name($name) . ';', | |
| 163 ]); | |
| 164 } | |
| 165 | |
| 166 | |
| 167 public function getRouteNamespace(string $name = '') | |
| 168 { | |
| 169 return $this->getRootNamespace() . 'Http\\Controllers'; | |
| 170 } | |
| 171 | |
| 172 | |
| 173 //////////////////////////////////////////// | |
| 174 // Language and Presentables // | |
| 175 //////////////////////////////////////////// | |
| 176 | |
| 177 /** | |
| 178 * Breaks up a string and makes it human readable | |
| 179 * | |
| 180 * This function assumes that the inputted name is camel case | |
| 181 */ | |
| 182 public function human_readable(string $name): string | |
| 183 { | |
| 184 return Str::title(Str::replace('_', ' ', $name)); | |
| 185 } | |
| 186 | |
| 187 /** | |
| 188 * Breaks up a string and makes it human readable and lowecase | |
| 189 * | |
| 190 * This function assumes that the inputted name is camel case | |
| 191 */ | |
| 192 public function human_readable_lc(string $name): string | |
| 193 { | |
| 194 return Str::lower($this->human_readable($name)); | |
| 195 } | |
| 196 } |
