Mercurial > packages > magicforger
comparison src/Replacer/Replacer.php @ 34:f65ab84ee47f default
merge with codex
| author | luka |
|---|---|
| date | Wed, 10 Sep 2025 21:00:47 -0400 |
| parents | 010ace248d14 |
| children | 55d2e5c5dad9 |
comparison
equal
deleted
inserted
replaced
| 10:a9ff874afdbd | 34:f65ab84ee47f |
|---|---|
| 1 <?php | 1 <?php |
| 2 | 2 |
| 3 namespace Wizzard\MagicForger\Replacer; | 3 namespace Wizard\MagicForger\Replacer; |
| 4 | 4 |
| 5 use Illuminate\Support\Str; | 5 use Illuminate\Support\Str; |
| 6 | 6 |
| 7 trait Replacer | 7 trait Replacer |
| 8 { | 8 { |
| 9 /** | 9 /** |
| 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 protected string $controller_prefix = ''; |
| 14 */ | |
| 15 protected $controller_prefix = ''; | |
| 16 | 14 |
| 17 /** | 15 /** |
| 18 * Prefix and Suffix for controller. | 16 * Prefix and Suffix for controller. |
| 19 * Usage is up to the user. | 17 * Usage is up to the user. |
| 20 * | 18 */ |
| 21 * @var string | 19 protected string $controller_suffix = 'Controller'; |
| 22 */ | |
| 23 protected $controller_suffix = 'Controller'; | |
| 24 | 20 |
| 25 /** | 21 /** |
| 26 * Finds all places in a string that could be replaced. | 22 * Finds all places in a string that could be replaced. |
| 27 * Returns an array of all potential replacements as they | 23 * Returns an array of all potential replacements as they appear in the target. |
| 28 * appear in the target. | |
| 29 */ | 24 */ |
| 30 public function get_all_keywords(string $target): array | 25 public function get_all_keywords(string $target): array |
| 31 { | 26 { |
| 32 // find all the matches to our expected syntax | 27 // find all matches to our expected syntax |
| 33 $matches = []; | 28 $matches = []; |
| 34 preg_match_all('/{{[\sa-zA-Z\-_]+}}/', $target, $matches); | 29 preg_match_all('/{{[\sa-zA-Z\-_]+}}/', $target, $matches); |
| 35 // sort the array and return unique values | 30 // sort the array and return unique values |
| 36 sort($matches[0]); | 31 sort($matches[0]); |
| 37 | 32 |
| 38 return array_values(array_unique($matches[0])); | 33 return array_values(array_unique($matches[0])); |
| 39 } | 34 } |
| 40 | 35 |
| 36 /** | |
| 37 * Apply replacements to the target string. | |
| 38 */ | |
| 41 public function apply_replacements(string $target): string | 39 public function apply_replacements(string $target): string |
| 42 { | 40 { |
| 43 $inserts = $this->get_all_keywords($target); | 41 $inserts = $this->get_all_keywords($target); |
| 44 $available_replacements = $this->get_available_replacements(); | 42 $available_replacements = $this->get_available_replacements(); |
| 45 | 43 |
| 46 $target = str_replace( | 44 return str_replace( |
| 47 array_keys($available_replacements), | 45 array_keys($available_replacements), |
| 48 $available_replacements, | 46 $available_replacements, |
| 49 $target | 47 $target |
| 50 ); | 48 ); |
| 51 | 49 } |
| 52 return $target; | 50 |
| 53 } | 51 /** |
| 54 | 52 * Get available replacements for string replacements. |
| 55 public function get_available_replacements() | 53 */ |
| 54 public function get_available_replacements(): array | |
| 56 { | 55 { |
| 57 $table_name = $this->getTableInput(); | 56 $table_name = $this->getTableInput(); |
| 58 $replacements = [ | 57 |
| 59 '{{ class }}' => $this->getClassName($table_name), | 58 return [ |
| 60 '{{ controllerName }}' => $this->controller_name($table_name), | 59 '{{ class }}' => $this->getClassName($table_name), |
| 61 '{{ model }}' => $this->model_name($table_name), | 60 '{{ controllerName }}' => $this->controller_name($table_name), |
| 62 '{{ modelVariable }}' => $this->model_variable($table_name), | 61 '{{ model }}' => $this->model_name($table_name), |
| 63 '{{ namespace }}' => $this->{'get'.$this->type.'Namespace'}($table_name), | 62 '{{ modelVariable }}' => $this->model_variable($table_name), |
| 64 '{{ namespacedModel }}' => $this->getNamespacedModel($table_name), | 63 '{{ namespace }}' => $this->{'get'.$this->type.'Namespace'}($table_name), |
| 65 '{{ requestUses }}' => $this->getRequestUses($table_name), | 64 '{{ namespacedModel }}' => $this->getNamespacedModel($table_name), |
| 66 '{{ rootNamespace }}' => $this->getRootNamespace(), | 65 '{{ requestUses }}' => $this->getRequestUses($table_name), |
| 67 '{{ storeRequest }}' => $this->store_request_name($table_name), | 66 '{{ rootNamespace }}' => $this->getRootNamespace(), |
| 68 '{{ tableName }}' => $table_name, | 67 '{{ storeRequest }}' => $this->store_request_name($table_name), |
| 69 '{{ updateRequest }}' => $this->update_request_name($table_name), | 68 '{{ filterRequest }}' => $this->filter_request_name($table_name), |
| 69 '{{ updateRequest }}' => $this->update_request_name($table_name), | |
| 70 '{{ tableName }}' => $table_name, | |
| 70 ]; | 71 ]; |
| 71 | 72 } |
| 72 return $replacements; | 73 |
| 73 } | 74 // Model and Controller Naming |
| 74 | 75 |
| 75 // ////////////////////////////////////////// | 76 /** |
| 76 // Internals and Classes // | 77 * Generate model name in Studly case. |
| 77 // ////////////////////////////////////////// | |
| 78 | |
| 79 /** | |
| 80 * Model names are generated in uppercase first Camel case. | |
| 81 */ | 78 */ |
| 82 public function model_name(string $name): string | 79 public function model_name(string $name): string |
| 83 { | 80 { |
| 84 return Str::singular(Str::studly($name)); | 81 return Str::singular(Str::studly($name)); |
| 85 } | 82 } |
| 86 | 83 |
| 87 /** | 84 /** |
| 88 * Model variable is standardly just a singular version of the table name. | 85 * Generate singular model variable name. |
| 89 */ | 86 */ |
| 90 public function model_variable(string $name): string | 87 public function model_variable(string $name): string |
| 91 { | 88 { |
| 92 /* return Str::singular($name); */ | 89 return Str::singular($name); |
| 93 return 'item'; | 90 } |
| 94 } | 91 |
| 95 | 92 /** |
| 96 /** | 93 * Generate controller name using prefix/suffix and studly case. |
| 97 * Controller names are generated in uppercase first Camel case | |
| 98 * and wrapped in the prefix and suffix. | |
| 99 */ | 94 */ |
| 100 public function controller_name(string $name): string | 95 public function controller_name(string $name): string |
| 101 { | 96 { |
| 102 return $this->controller_prefix. | 97 return $this->controller_prefix. |
| 103 $this->model_name($name). | 98 $this->model_name($name). |
| 104 $this->controller_suffix; | 99 $this->controller_suffix; |
| 105 } | 100 } |
| 106 | 101 |
| 102 /** | |
| 103 * Generate the store request name. | |
| 104 */ | |
| 107 public function store_request_name(string $name): string | 105 public function store_request_name(string $name): string |
| 108 { | 106 { |
| 109 return 'Store'.$this->model_name($name).'Request'; | 107 return 'Store'.$this->model_name($name).'Request'; |
| 110 } | 108 } |
| 111 | 109 |
| 110 /** | |
| 111 * Generate the filter request name. | |
| 112 */ | |
| 113 public function filter_request_name(string $name): string | |
| 114 { | |
| 115 return 'Filter'.$this->model_name($name).'Request'; | |
| 116 } | |
| 117 | |
| 118 /** | |
| 119 * Generate the update request name. | |
| 120 */ | |
| 112 public function update_request_name(string $name): string | 121 public function update_request_name(string $name): string |
| 113 { | 122 { |
| 114 return 'Update'.$this->model_name($name).'Request'; | 123 return 'Update'.$this->model_name($name).'Request'; |
| 115 } | 124 } |
| 116 | 125 |
| 117 // ////////////////////////////////////////// | 126 |
| 118 // Namespaces // | 127 /** |
| 119 // ////////////////////////////////////////// | 128 * Generate the index view name. |
| 120 | 129 */ |
| 121 public function getRootNamespace() | 130 public function index_view_name(string $name): string |
| 131 { | |
| 132 return ''; | |
| 133 } | |
| 134 | |
| 135 /** | |
| 136 * Generate the create_edit view name. | |
| 137 */ | |
| 138 public function create_edit_view_name(string $name): string | |
| 139 { | |
| 140 return ''; | |
| 141 } | |
| 142 | |
| 143 /** | |
| 144 * Generate the show view name. | |
| 145 */ | |
| 146 public function show_view_name(string $name): string | |
| 147 { | |
| 148 return ''; | |
| 149 } | |
| 150 | |
| 151 | |
| 152 /** | |
| 153 * Generate route name in Studly case. | |
| 154 */ | |
| 155 public function routes_name(string $name): string | |
| 156 { | |
| 157 return Str::singular(Str::studly($name)); | |
| 158 } | |
| 159 | |
| 160 // Namespace Methods | |
| 161 // These methods handle the formation of various namespaces used within the replacements. | |
| 162 | |
| 163 /** | |
| 164 * Get the root namespace for the application. | |
| 165 */ | |
| 166 public function getRootNamespace(): string | |
| 122 { | 167 { |
| 123 return $this->laravel->getNamespace(); | 168 return $this->laravel->getNamespace(); |
| 124 } | 169 } |
| 125 | 170 |
| 126 public function getModelNamespace(string $name = '') | 171 /** |
| 172 * Get the model namespace. | |
| 173 */ | |
| 174 public function getRouteNamespace(string $name = ''): string | |
| 175 { | |
| 176 return base_path() | |
| 177 . DIRECTORY_SEPARATOR . 'routes' | |
| 178 . DIRECTORY_SEPARATOR . 'resources' | |
| 179 ; | |
| 180 } | |
| 181 | |
| 182 /** | |
| 183 * Get the model namespace. | |
| 184 */ | |
| 185 public function getModelNamespace(string $name = ''): string | |
| 127 { | 186 { |
| 128 return $this->getRootNamespace().'Models'; | 187 return $this->getRootNamespace().'Models'; |
| 129 } | 188 } |
| 130 | 189 |
| 131 public function getNamespacedModel(string $name = '') | 190 /** |
| 191 * Get the fully-qualified namespaced model class. | |
| 192 */ | |
| 193 public function getNamespacedModel(string $name = ''): string | |
| 132 { | 194 { |
| 133 return $this->getModelNamespace().'\\'.$this->model_name($name); | 195 return $this->getModelNamespace().'\\'.$this->model_name($name); |
| 134 } | 196 } |
| 135 | 197 |
| 136 public function getControllerNamespace(string $name = '') | 198 /** |
| 199 * Get the controller namespace. | |
| 200 */ | |
| 201 public function getControllerNamespace(string $name = ''): string | |
| 137 { | 202 { |
| 138 return $this->getRootNamespace().'Http\\Controllers'; | 203 return $this->getRootNamespace().'Http\\Controllers'; |
| 139 } | 204 } |
| 140 | 205 |
| 141 public function getRequestNamespace(string $name) | 206 /** |
| 207 * Get the request namespace. | |
| 208 */ | |
| 209 public function getRequestNamespace(string $name): string | |
| 142 { | 210 { |
| 143 return $this->getRootNamespace().'Http\\Requests\\'.$this->model_name($name); | 211 return $this->getRootNamespace().'Http\\Requests\\'.$this->model_name($name); |
| 144 } | 212 } |
| 145 | 213 |
| 146 public function getStoreRequestNamespace(string $name) | 214 /** |
| 215 * Get the store request namespace. | |
| 216 */ | |
| 217 public function getStoreRequestNamespace(string $name): string | |
| 147 { | 218 { |
| 148 return $this->getRequestNamespace($name); | 219 return $this->getRequestNamespace($name); |
| 149 } | 220 } |
| 150 | 221 |
| 151 public function getUpdateRequestNamespace(string $name) | 222 /** |
| 223 * Get the filter request namespace. | |
| 224 */ | |
| 225 public function getFilterRequestNamespace(string $name): string | |
| 152 { | 226 { |
| 153 return $this->getRequestNamespace($name); | 227 return $this->getRequestNamespace($name); |
| 154 } | 228 } |
| 155 | 229 |
| 156 public function getRequestUses(string $name) | 230 /** |
| 231 * Get the update request namespace. | |
| 232 */ | |
| 233 public function getUpdateRequestNamespace(string $name): string | |
| 234 { | |
| 235 return $this->getRequestNamespace($name); | |
| 236 } | |
| 237 | |
| 238 /** | |
| 239 * Get the view namespace. | |
| 240 */ | |
| 241 public function getViewNamespace(string $name): string | |
| 242 { | |
| 243 return $this->viewPath($name) . '\\'; | |
| 244 } | |
| 245 | |
| 246 /** | |
| 247 * Get the index view namespace. | |
| 248 */ | |
| 249 public function getIndexViewNamespace(string $name): string | |
| 250 { | |
| 251 return $this->getViewNamespace($name) . '\\'; | |
| 252 } | |
| 253 | |
| 254 /** | |
| 255 * Get the create_edit view namespace. | |
| 256 */ | |
| 257 public function getCreateEditViewNamespace(string $name): string | |
| 258 { | |
| 259 return $this->getViewNamespace($name) . '\\'; | |
| 260 } | |
| 261 | |
| 262 /** | |
| 263 * Get the show view namespace. | |
| 264 */ | |
| 265 public function getShowViewNamespace(string $name): string | |
| 266 { | |
| 267 return $this->getViewNamespace($name) . '\\'; | |
| 268 } | |
| 269 | |
| 270 | |
| 271 /** | |
| 272 * Get the request uses string for replacement. | |
| 273 */ | |
| 274 public function getRequestUses(string $name): string | |
| 157 { | 275 { |
| 158 return implode("\n", [ | 276 return implode("\n", [ |
| 159 'use '.$this->getRequestNamespace($name).'\\'.$this->store_request_name($name).';', | 277 'use '.$this->getRequestNamespace($name).'\\'.$this->store_request_name($name).';', |
| 278 'use '.$this->getRequestNamespace($name).'\\'.$this->filter_request_name($name).';', | |
| 160 'use '.$this->getRequestNamespace($name).'\\'.$this->update_request_name($name).';', | 279 'use '.$this->getRequestNamespace($name).'\\'.$this->update_request_name($name).';', |
| 161 ]); | 280 ]); |
| 162 } | 281 } |
| 163 | 282 |
| 164 public function getRouteNamespace(string $name = '') | 283 // Text Manipulation |
| 165 { | 284 |
| 166 return $this->getRootNamespace().'Http\\Controllers'; | 285 /** |
| 167 } | 286 * Convert a string to a human-readable format. |
| 168 | 287 * Assumes camel case input. |
| 169 // ////////////////////////////////////////// | |
| 170 // Language and Presentables // | |
| 171 // ////////////////////////////////////////// | |
| 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 */ | 288 */ |
| 178 public function human_readable(string $name): string | 289 public function human_readable(string $name): string |
| 179 { | 290 { |
| 180 return Str::title(Str::replace('_', ' ', $name)); | 291 return Str::title(Str::replace('_', ' ', $name)); |
| 181 } | 292 } |
| 182 | 293 |
| 183 /** | 294 /** |
| 184 * Breaks up a string and makes it human readable and lowecase. | 295 * Convert a string to a lowercase human-readable format. |
| 185 * | 296 * Assumes camel case input. |
| 186 * This function assumes that the inputted name is camel case | |
| 187 */ | 297 */ |
| 188 public function human_readable_lc(string $name): string | 298 public function human_readable_lc(string $name): string |
| 189 { | 299 { |
| 190 return Str::lower($this->human_readable($name)); | 300 return Str::lower($this->human_readable($name)); |
| 191 } | 301 } |
