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