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