Mercurial > packages > magicforger
annotate src/Replacer/Replacer.php @ 7:769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
| author | luka |
|---|---|
| date | Wed, 18 Oct 2023 21:04:11 -0400 |
| parents | b0b2e79ad8e6 |
| children | d4730c14806d |
| 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 | |
| 55 public function get_available_replacements() | |
| 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 { | |
| 92 return Str::singular($name); | |
| 93 } | |
| 94 | |
| 95 /** | |
| 96 * 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
|
97 * and wrapped in the prefix and suffix. |
| 5 | 98 */ |
| 99 public function controller_name(string $name): string | |
| 100 { | |
|
7
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
5
diff
changeset
|
101 return $this->controller_prefix. |
|
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
5
diff
changeset
|
102 $this->model_name($name). |
| 5 | 103 $this->controller_suffix; |
| 104 } | |
| 105 | |
| 106 public function store_request_name(string $name): string | |
| 107 { | |
|
7
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
5
diff
changeset
|
108 return 'Store'.$this->model_name($name).'Request'; |
| 5 | 109 } |
| 110 | |
| 111 public function update_request_name(string $name): string | |
| 112 { | |
|
7
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
5
diff
changeset
|
113 return 'Update'.$this->model_name($name).'Request'; |
| 5 | 114 } |
| 115 | |
|
7
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
5
diff
changeset
|
116 // ////////////////////////////////////////// |
| 5 | 117 // Namespaces // |
|
7
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
5
diff
changeset
|
118 // ////////////////////////////////////////// |
| 5 | 119 |
| 120 public function getRootNamespace() | |
| 121 { | |
| 122 return $this->laravel->getNamespace(); | |
| 123 } | |
| 124 | |
| 125 public function getModelNamespace(string $name = '') | |
| 126 { | |
|
7
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
5
diff
changeset
|
127 return $this->getRootNamespace().'Models'; |
| 5 | 128 } |
| 129 | |
| 130 public function getNamespacedModel(string $name = '') | |
| 131 { | |
|
7
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
5
diff
changeset
|
132 return $this->getModelNamespace().'\\'.$this->model_name($name); |
| 5 | 133 } |
| 134 | |
| 135 public function getControllerNamespace(string $name = '') | |
| 136 { | |
|
7
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
5
diff
changeset
|
137 return $this->getRootNamespace().'Http\\Controllers'; |
| 5 | 138 } |
| 139 | |
| 140 public function getRequestNamespace(string $name) | |
| 141 { | |
|
7
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
5
diff
changeset
|
142 return $this->getRootNamespace().'Http\\Requests\\'.$this->model_name($name); |
| 5 | 143 } |
| 144 | |
| 145 public function getStoreRequestNamespace(string $name) | |
| 146 { | |
| 147 return $this->getRequestNamespace($name); | |
| 148 } | |
| 149 | |
| 150 public function getUpdateRequestNamespace(string $name) | |
| 151 { | |
| 152 return $this->getRequestNamespace($name); | |
| 153 } | |
| 154 | |
| 155 public function getRequestUses(string $name) | |
| 156 { | |
| 157 return implode("\n", [ | |
|
7
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
5
diff
changeset
|
158 '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
|
159 'use '.$this->getRequestNamespace($name).'\\'.$this->update_request_name($name).';', |
| 5 | 160 ]); |
| 161 } | |
| 162 | |
| 163 public function getRouteNamespace(string $name = '') | |
| 164 { | |
|
7
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
5
diff
changeset
|
165 return $this->getRootNamespace().'Http\\Controllers'; |
| 5 | 166 } |
| 167 | |
|
7
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
5
diff
changeset
|
168 // ////////////////////////////////////////// |
| 5 | 169 // Language and Presentables // |
|
7
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
5
diff
changeset
|
170 // ////////////////////////////////////////// |
| 5 | 171 |
| 172 /** | |
|
7
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
5
diff
changeset
|
173 * Breaks up a string and makes it human readable. |
| 5 | 174 * |
| 175 * This function assumes that the inputted name is camel case | |
| 176 */ | |
| 177 public function human_readable(string $name): string | |
| 178 { | |
| 179 return Str::title(Str::replace('_', ' ', $name)); | |
| 180 } | |
| 181 | |
| 182 /** | |
|
7
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
5
diff
changeset
|
183 * Breaks up a string and makes it human readable and lowecase. |
| 5 | 184 * |
| 185 * This function assumes that the inputted name is camel case | |
| 186 */ | |
| 187 public function human_readable_lc(string $name): string | |
| 188 { | |
| 189 return Str::lower($this->human_readable($name)); | |
| 190 } | |
| 191 } |
