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