annotate src/Generator/Replacer.php @ 12:4bb4daa9e3f1 development

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