annotate src/Generator/Replacer.php @ 4:a20439b1c9d3

Added Model generator and other updates.
author luka
date Tue, 27 Jun 2023 20:16:55 -0400
parents 6468684362c2
children 4bb4daa9e3f1
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 }
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
56
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
57 public function get_available_replacements()
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
58 {
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
59 $table_name = $this->getTableInput();
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
60 $replacements = [
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
61 "{{ class }}" => $this->getClassName($table_name),
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
62 "{{ model }}" => $this->model_name($table_name),
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
63 "{{ modelVariable }}" => $this->model_variable($table_name),
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
64 "{{ namespace }}" => $this->{'get' . $this->type . 'Namespace'}(),
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
65 "{{ namespacedModel }}" => $this->getModelNamespace(),
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
66 "{{ requestUses }}" => $this->getRequestUses($table_name),
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
67 "{{ rootNamespace }}" => $this->getRootNamespace(),
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
68 "{{ storeRequest }}" => $this->store_request_name($table_name),
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
69 "{{ tableName }}" => $table_name,
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
70 "{{ updateRequest }}" => $this->update_request_name($table_name),
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
71 ];
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
72
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
73 return $replacements;
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
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 // Internals and Classes //
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
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 * 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
82 */
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
83 public function model_name(string $name): string
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 return Str::singular(Str::studly($name));
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
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 * 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
90 */
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
91 public function model_variable(string $name): string
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 return Str::singular($name);
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
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 * 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
98 * and wrapped in the prefix and suffix
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
99 */
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
100 public function controller_name(string $name): string
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 return $this->controller_prefix .
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
103 $this->model_name($name) .
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
104 $this->controller_suffix;
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
105 }
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
106
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
107 public function store_request_name(string $name): string
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 return 'Store' . $this->model_name($name);
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
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
112 public function update_request_name(string $name): string
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 return 'Update' . $this->model_name($name);
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
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 // Namespaces //
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
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
122 public function getRootNamespace()
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 return $this->laravel->getNamespace();
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
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
127 public function getModelNamespace()
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 return $this->getRootNamespace() . 'Models';
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
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
132 public function getControllerNamespace()
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 return $this->getRootNamespace() . 'Http\\Controllers';
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
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
137 public function getRequestNamespace(string $name)
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 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
140 }
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
141
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
142 public function getRequestUses(string $name)
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
143 {
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
144 return implode("\n", [
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
145 "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
146 "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
147 ]);
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
148 }
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
149
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
150
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 // Language and Presentables //
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 * 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
157 *
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
158 * 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
159 */
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
160 public function human_readable(string $name): string
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 return Str::title(Str::replace('_', ' ', $name));
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
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 * 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
167 *
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
168 * 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
169 */
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
170 public function human_readable_lc(string $name): string
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 return Str::lower($this->human_readable($name));
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 }