annotate src/Generator/Replacer.php @ 3:6468684362c2

It works! Created a controller, no update insert but it works
author luka
date Tue, 27 Jun 2023 15:32:47 -0400
parents
children a20439b1c9d3
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
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
74 return $replacements;
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 ////////////////////////////////////////////
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
78 // Internals and Classes //
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 /**
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
82 * 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
83 */
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
84 public function model_name(string $name): string
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
85 {
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
86 return Str::singular(Str::studly($name));
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 /**
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
90 * 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
91 */
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
92 public function model_variable(string $name): string
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
93 {
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
94 return Str::singular($name);
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 /**
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
98 * 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
99 * and wrapped in the prefix and suffix
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
100 */
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
101 public function controller_name(string $name): string
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
102 {
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
103 return $this->controller_prefix .
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
104 $this->model_name($name) .
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
105 $this->controller_suffix;
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
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
108 public function store_request_name(string $name): string
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
109 {
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
110 return 'Store' . $this->model_name($name);
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
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
113 public function update_request_name(string $name): string
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
114 {
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
115 return 'Update' . $this->model_name($name);
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 ////////////////////////////////////////////
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
120 // Namespaces //
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
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
123 public function getRootNamespace()
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
124 {
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
125 return $this->laravel->getNamespace();
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
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
128 public function getModelNamespace()
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
129 {
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
130 return $this->getRootNamespace() . 'Models';
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
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
133 public function getControllerNamespace()
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
134 {
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
135 return $this->getRootNamespace() . 'Http\\Controllers';
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
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
138 public function getRequestNamespace(string $name)
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
139 {
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
140 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
141 }
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
142
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
143 public function getRequestUses(string $name)
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
144 {
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
145 return implode("\n", [
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
146 "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
147 "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
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 ////////////////////////////////////////////
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
153 // Language and Presentables //
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 /**
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
157 * 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
158 *
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
159 * 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
160 */
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
161 public function human_readable(string $name): string
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
162 {
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
163 return Str::title(Str::replace('_', ' ', $name));
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 /**
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
167 * 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
168 *
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
169 * 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
170 */
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
171 public function human_readable_lc(string $name): string
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
172 {
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
173 return Str::lower($this->human_readable($name));
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
174 }
6468684362c2 It works! Created a controller, no update insert but it works
luka
parents:
diff changeset
175 }