Mercurial > packages > magicforger
comparison src/Replacer/Replacer.php @ 21:f0b0d014e448 main-dev
Cleaning up code based on AI overlord review
| author | Luka Sitas <sitas.luka.97@gmail.com> |
|---|---|
| date | Wed, 26 Feb 2025 19:45:08 -0500 |
| parents | 19b7a8de0019 |
| children | 827efbf4d73c |
comparison
equal
deleted
inserted
replaced
| 20:81a76472ba21 | 21:f0b0d014e448 |
|---|---|
| 10 * Prefix and Suffix for controller. | 10 * Prefix and Suffix for controller. |
| 11 * Usage is up to the user. | 11 * Usage is up to the user. |
| 12 * | 12 * |
| 13 * @var string | 13 * @var string |
| 14 */ | 14 */ |
| 15 protected $controller_prefix = ''; | 15 protected string $controller_prefix = ''; |
| 16 | 16 |
| 17 /** | 17 /** |
| 18 * Prefix and Suffix for controller. | 18 * Prefix and Suffix for controller. |
| 19 * Usage is up to the user. | 19 * Usage is up to the user. |
| 20 * | 20 * |
| 21 * @var string | 21 * @var string |
| 22 */ | 22 */ |
| 23 protected $controller_suffix = 'Controller'; | 23 protected string $controller_suffix = 'Controller'; |
| 24 | 24 |
| 25 /** | 25 /** |
| 26 * Finds all places in a string that could be replaced. | 26 * Finds all places in a string that could be replaced. |
| 27 * Returns an array of all potential replacements as they | 27 * Returns an array of all potential replacements as they appear in the target. |
| 28 * appear in the target. | 28 * |
| 29 * @param string $target | |
| 30 * @return array | |
| 29 */ | 31 */ |
| 30 public function get_all_keywords(string $target): array | 32 public function get_all_keywords(string $target): array |
| 31 { | 33 { |
| 32 // find all the matches to our expected syntax | 34 // find all matches to our expected syntax |
| 33 $matches = []; | 35 $matches = []; |
| 34 preg_match_all('/{{[\sa-zA-Z\-_]+}}/', $target, $matches); | 36 preg_match_all('/{{[\sa-zA-Z\-_]+}}/', $target, $matches); |
| 35 // sort the array and return unique values | 37 // sort the array and return unique values |
| 36 sort($matches[0]); | 38 sort($matches[0]); |
| 37 | 39 |
| 38 return array_values(array_unique($matches[0])); | 40 return array_values(array_unique($matches[0])); |
| 39 } | 41 } |
| 40 | 42 |
| 43 /** | |
| 44 * Apply replacements to the target string. | |
| 45 * | |
| 46 * @param string $target | |
| 47 * @return string | |
| 48 */ | |
| 41 public function apply_replacements(string $target): string | 49 public function apply_replacements(string $target): string |
| 42 { | 50 { |
| 43 $inserts = $this->get_all_keywords($target); | 51 $inserts = $this->get_all_keywords($target); |
| 44 $available_replacements = $this->get_available_replacements(); | 52 $available_replacements = $this->get_available_replacements(); |
| 45 | 53 |
| 46 $target = str_replace( | 54 return str_replace( |
| 47 array_keys($available_replacements), | 55 array_keys($available_replacements), |
| 48 $available_replacements, | 56 $available_replacements, |
| 49 $target | 57 $target |
| 50 ); | 58 ); |
| 51 | 59 } |
| 52 return $target; | 60 |
| 53 } | 61 /** |
| 54 | 62 * Get available replacements for string replacements. |
| 63 * | |
| 64 * @return array | |
| 65 */ | |
| 55 public function get_available_replacements(): array | 66 public function get_available_replacements(): array |
| 56 { | 67 { |
| 57 $table_name = $this->getTableInput(); | 68 $table_name = $this->getTableInput(); |
| 58 $replacements = [ | 69 |
| 59 '{{ class }}' => $this->getClassName($table_name), | 70 return [ |
| 60 '{{ controllerName }}' => $this->controller_name($table_name), | 71 '{{ class }}' => $this->getClassName($table_name), |
| 61 '{{ model }}' => $this->model_name($table_name), | 72 '{{ controllerName }}' => $this->controller_name($table_name), |
| 62 '{{ modelVariable }}' => $this->model_variable($table_name), | 73 '{{ model }}' => $this->model_name($table_name), |
| 63 '{{ namespace }}' => $this->{'get'.$this->type.'Namespace'}($table_name), | 74 '{{ modelVariable }}' => $this->model_variable($table_name), |
| 64 '{{ namespacedModel }}' => $this->getNamespacedModel($table_name), | 75 '{{ namespace }}' => $this->{'get' . $this->type . 'Namespace'}($table_name), |
| 65 '{{ requestUses }}' => $this->getRequestUses($table_name), | 76 '{{ namespacedModel }}' => $this->getNamespacedModel($table_name), |
| 66 '{{ rootNamespace }}' => $this->getRootNamespace(), | 77 '{{ requestUses }}' => $this->getRequestUses($table_name), |
| 67 '{{ storeRequest }}' => $this->store_request_name($table_name), | 78 '{{ rootNamespace }}' => $this->getRootNamespace(), |
| 68 '{{ tableName }}' => $table_name, | 79 '{{ storeRequest }}' => $this->store_request_name($table_name), |
| 69 '{{ updateRequest }}' => $this->update_request_name($table_name), | 80 '{{ tableName }}' => $table_name, |
| 81 '{{ updateRequest }}' => $this->update_request_name($table_name), | |
| 70 ]; | 82 ]; |
| 71 | 83 } |
| 72 return $replacements; | 84 |
| 73 } | 85 // Model and Controller Naming |
| 74 | 86 |
| 75 // ////////////////////////////////////////// | 87 /** |
| 76 // Internals and Classes // | 88 * Generate model name in Studly case. |
| 77 // ////////////////////////////////////////// | 89 * |
| 78 | 90 * @param string $name |
| 79 /** | 91 * @return string |
| 80 * Model names are generated in uppercase first Camel case. | |
| 81 */ | 92 */ |
| 82 public function model_name(string $name): string | 93 public function model_name(string $name): string |
| 83 { | 94 { |
| 84 return Str::singular(Str::studly($name)); | 95 return Str::singular(Str::studly($name)); |
| 85 } | 96 } |
| 86 | 97 |
| 87 /** | 98 /** |
| 88 * Model variable is standardly just a singular version of the table name. | 99 * Generate singular model variable name. |
| 100 * | |
| 101 * @param string $name | |
| 102 * @return string | |
| 89 */ | 103 */ |
| 90 public function model_variable(string $name): string | 104 public function model_variable(string $name): string |
| 91 { | 105 { |
| 92 return Str::singular($name); | 106 return Str::singular($name); |
| 93 /* return 'item'; */ | 107 } |
| 94 } | 108 |
| 95 | 109 /** |
| 96 /** | 110 * Generate controller name using prefix/suffix and studly case. |
| 97 * Controller names are generated in uppercase first Camel case | 111 * |
| 98 * and wrapped in the prefix and suffix. | 112 * @param string $name |
| 113 * @return string | |
| 99 */ | 114 */ |
| 100 public function controller_name(string $name): string | 115 public function controller_name(string $name): string |
| 101 { | 116 { |
| 102 return $this->controller_prefix. | 117 return $this->controller_prefix . |
| 103 $this->model_name($name). | 118 $this->model_name($name) . |
| 104 $this->controller_suffix; | 119 $this->controller_suffix; |
| 105 } | 120 } |
| 106 | 121 |
| 122 /** | |
| 123 * Generate the store request name. | |
| 124 * | |
| 125 * @param string $name | |
| 126 * @return string | |
| 127 */ | |
| 107 public function store_request_name(string $name): string | 128 public function store_request_name(string $name): string |
| 108 { | 129 { |
| 109 return 'Store'.$this->model_name($name).'Request'; | 130 return 'Store' . $this->model_name($name) . 'Request'; |
| 110 } | 131 } |
| 111 | 132 |
| 133 /** | |
| 134 * Generate the update request name. | |
| 135 * | |
| 136 * @param string $name | |
| 137 * @return string | |
| 138 */ | |
| 112 public function update_request_name(string $name): string | 139 public function update_request_name(string $name): string |
| 113 { | 140 { |
| 114 return 'Update'.$this->model_name($name).'Request'; | 141 return 'Update' . $this->model_name($name) . 'Request'; |
| 115 } | 142 } |
| 116 | 143 |
| 117 // ////////////////////////////////////////// | 144 // Namespace Methods |
| 118 // Namespaces // | 145 // These methods handle the formation of various namespaces used within the replacements. |
| 119 // ////////////////////////////////////////// | 146 |
| 120 | 147 /** |
| 148 * Get the root namespace for the application. | |
| 149 * | |
| 150 * @return string | |
| 151 */ | |
| 121 public function getRootNamespace(): string | 152 public function getRootNamespace(): string |
| 122 { | 153 { |
| 123 return $this->laravel->getNamespace(); | 154 return $this->laravel->getNamespace(); |
| 124 } | 155 } |
| 125 | 156 |
| 157 /** | |
| 158 * Get the model namespace. | |
| 159 * | |
| 160 * @param string $name | |
| 161 * @return string | |
| 162 */ | |
| 126 public function getModelNamespace(string $name = ''): string | 163 public function getModelNamespace(string $name = ''): string |
| 127 { | 164 { |
| 128 return $this->getRootNamespace().'Models'; | 165 return $this->getRootNamespace() . 'Models'; |
| 129 } | 166 } |
| 130 | 167 |
| 168 /** | |
| 169 * Get the fully-qualified namespaced model class. | |
| 170 * | |
| 171 * @param string $name | |
| 172 * @return string | |
| 173 */ | |
| 131 public function getNamespacedModel(string $name = ''): string | 174 public function getNamespacedModel(string $name = ''): string |
| 132 { | 175 { |
| 133 return $this->getModelNamespace().'\\'.$this->model_name($name); | 176 return $this->getModelNamespace() . '\\' . $this->model_name($name); |
| 134 } | 177 } |
| 135 | 178 |
| 179 /** | |
| 180 * Get the controller namespace. | |
| 181 * | |
| 182 * @param string $name | |
| 183 * @return string | |
| 184 */ | |
| 136 public function getControllerNamespace(string $name = ''): string | 185 public function getControllerNamespace(string $name = ''): string |
| 137 { | 186 { |
| 138 return $this->getRootNamespace().'Http\\Controllers'; | 187 return $this->getRootNamespace() . 'Http\\Controllers'; |
| 139 } | 188 } |
| 140 | 189 |
| 190 /** | |
| 191 * Get the request namespace. | |
| 192 * | |
| 193 * @param string $name | |
| 194 * @return string | |
| 195 */ | |
| 141 public function getRequestNamespace(string $name): string | 196 public function getRequestNamespace(string $name): string |
| 142 { | 197 { |
| 143 return $this->getRootNamespace().'Http\\Requests\\'.$this->model_name($name); | 198 return $this->getRootNamespace() . 'Http\\Requests\\' . $this->model_name($name); |
| 144 } | 199 } |
| 145 | 200 |
| 201 /** | |
| 202 * Get the store request namespace. | |
| 203 * | |
| 204 * @param string $name | |
| 205 * @return string | |
| 206 */ | |
| 146 public function getStoreRequestNamespace(string $name): string | 207 public function getStoreRequestNamespace(string $name): string |
| 147 { | 208 { |
| 148 return $this->getRequestNamespace($name); | 209 return $this->getRequestNamespace($name); |
| 149 } | 210 } |
| 150 | 211 |
| 212 /** | |
| 213 * Get the update request namespace. | |
| 214 * | |
| 215 * @param string $name | |
| 216 * @return string | |
| 217 */ | |
| 151 public function getUpdateRequestNamespace(string $name): string | 218 public function getUpdateRequestNamespace(string $name): string |
| 152 { | 219 { |
| 153 return $this->getRequestNamespace($name); | 220 return $this->getRequestNamespace($name); |
| 154 } | 221 } |
| 155 | 222 |
| 223 /** | |
| 224 * Get the request uses string for replacement. | |
| 225 * | |
| 226 * @param string $name | |
| 227 * @return string | |
| 228 */ | |
| 156 public function getRequestUses(string $name): string | 229 public function getRequestUses(string $name): string |
| 157 { | 230 { |
| 158 return implode("\n", [ | 231 return implode("\n", [ |
| 159 'use '.$this->getRequestNamespace($name).'\\'.$this->store_request_name($name).';', | 232 'use ' . $this->getRequestNamespace($name) . '\\' . $this->store_request_name($name) . ';', |
| 160 'use '.$this->getRequestNamespace($name).'\\'.$this->update_request_name($name).';', | 233 'use ' . $this->getRequestNamespace($name) . '\\' . $this->update_request_name($name) . ';', |
| 161 ]); | 234 ]); |
| 162 } | 235 } |
| 163 | 236 |
| 164 public function getRouteNamespace(string $name = ''): string | 237 // Text Manipulation |
| 165 { | 238 |
| 166 return $this->getRootNamespace().'Http\\Controllers'; | 239 /** |
| 167 } | 240 * Convert a string to a human-readable format. |
| 168 | 241 * Assumes camel case input. |
| 169 // ////////////////////////////////////////// | 242 * |
| 170 // Language and Presentables // | 243 * @param string $name |
| 171 // ////////////////////////////////////////// | 244 * @return string |
| 172 | |
| 173 /** | |
| 174 * Breaks up a string and makes it human readable. | |
| 175 * | |
| 176 * This function assumes that the inputted name is camel case | |
| 177 */ | 245 */ |
| 178 public function human_readable(string $name): string | 246 public function human_readable(string $name): string |
| 179 { | 247 { |
| 180 return Str::title(Str::replace('_', ' ', $name)); | 248 return Str::title(Str::replace('_', ' ', $name)); |
| 181 } | 249 } |
| 182 | 250 |
| 183 /** | 251 /** |
| 184 * Breaks up a string and makes it human readable and lowecase. | 252 * Convert a string to a lowercase human-readable format. |
| 185 * | 253 * Assumes camel case input. |
| 186 * This function assumes that the inputted name is camel case | 254 * |
| 255 * @param string $name | |
| 256 * @return string | |
| 187 */ | 257 */ |
| 188 public function human_readable_lc(string $name): string | 258 public function human_readable_lc(string $name): string |
| 189 { | 259 { |
| 190 return Str::lower($this->human_readable($name)); | 260 return Str::lower($this->human_readable($name)); |
| 191 } | 261 } |
