|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Inflector\Inflector; |
|
6
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\NamespaceHelper; |
|
7
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Provides functionality to find and replace text in generated code |
|
11
|
|
|
* |
|
12
|
|
|
* Class FindAndReplaceHelper |
|
13
|
|
|
* |
|
14
|
|
|
* @package EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator |
|
15
|
|
|
* @SuppressWarnings(PHPMD.TooManyPublicMethods) |
|
16
|
|
|
*/ |
|
17
|
|
|
class FindAndReplaceHelper |
|
18
|
|
|
{ |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var NamespaceHelper |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $namespaceHelper; |
|
24
|
|
|
|
|
25
|
115 |
|
public function __construct(NamespaceHelper $namespaceHelper) |
|
26
|
|
|
{ |
|
27
|
115 |
|
$this->namespaceHelper = $namespaceHelper; |
|
28
|
115 |
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param string $find |
|
32
|
|
|
* @param string $replace |
|
33
|
|
|
* @param string $filePath |
|
34
|
|
|
* |
|
35
|
|
|
* @return self |
|
36
|
|
|
*/ |
|
37
|
113 |
|
public function findReplace( |
|
38
|
|
|
string $find, |
|
39
|
|
|
string $replace, |
|
40
|
|
|
string $filePath |
|
41
|
|
|
): self { |
|
42
|
113 |
|
$contents = file_get_contents($filePath); |
|
43
|
113 |
|
$contents = str_replace($find, $replace, $contents); |
|
44
|
113 |
|
file_put_contents($filePath, $contents); |
|
45
|
|
|
|
|
46
|
113 |
|
return $this; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param string $find |
|
51
|
|
|
* @param string $replace |
|
52
|
|
|
* @param string $filePath |
|
53
|
|
|
* |
|
54
|
|
|
* @return self |
|
55
|
|
|
*/ |
|
56
|
25 |
|
public function findReplaceRegex( |
|
57
|
|
|
string $find, |
|
58
|
|
|
string $replace, |
|
59
|
|
|
string $filePath |
|
60
|
|
|
): self { |
|
61
|
25 |
|
$contents = file_get_contents($filePath); |
|
62
|
25 |
|
$contents = preg_replace($find, $replace, $contents); |
|
63
|
25 |
|
file_put_contents($filePath, $contents); |
|
64
|
|
|
|
|
65
|
25 |
|
return $this; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param string $replacement |
|
70
|
|
|
* @param string $filePath |
|
71
|
|
|
* @param string $findName |
|
72
|
|
|
* |
|
73
|
|
|
* @return self |
|
74
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
|
75
|
|
|
*/ |
|
76
|
113 |
|
public function replaceName( |
|
77
|
|
|
string $replacement, |
|
78
|
|
|
string $filePath, |
|
79
|
|
|
$findName = AbstractGenerator::FIND_ENTITY_NAME |
|
80
|
|
|
): self { |
|
81
|
113 |
|
$this->findReplace($findName, $replacement, $filePath); |
|
82
|
113 |
|
$this->findReplace(\lcfirst($findName), \lcfirst($replacement), $filePath); |
|
83
|
113 |
|
$this->findReplace(\strtoupper($findName), \strtoupper($replacement), $filePath); |
|
84
|
113 |
|
$this->findReplace( |
|
85
|
113 |
|
\strtoupper(Inflector::tableize($findName)), |
|
86
|
113 |
|
\strtoupper(Inflector::tableize($replacement)), |
|
87
|
113 |
|
$filePath |
|
88
|
|
|
); |
|
89
|
|
|
|
|
90
|
113 |
|
return $this; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param string $replacement |
|
95
|
|
|
* @param string $filePath |
|
96
|
|
|
* |
|
97
|
|
|
* @return self |
|
98
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
|
99
|
|
|
*/ |
|
100
|
25 |
|
public function replacePluralName(string $replacement, string $filePath): self |
|
101
|
|
|
{ |
|
102
|
25 |
|
$this->findReplace(AbstractGenerator::FIND_ENTITY_NAME_PLURAL, $replacement, $filePath); |
|
103
|
25 |
|
$this->findReplace(\lcfirst(AbstractGenerator::FIND_ENTITY_NAME_PLURAL), \lcfirst($replacement), $filePath); |
|
104
|
25 |
|
$this->findReplace( |
|
105
|
25 |
|
\strtoupper(AbstractGenerator::FIND_ENTITY_NAME_PLURAL), |
|
106
|
25 |
|
\strtoupper($replacement), |
|
107
|
25 |
|
$filePath |
|
108
|
|
|
); |
|
109
|
25 |
|
$this->findReplace( |
|
110
|
25 |
|
\strtoupper(Inflector::tableize(AbstractGenerator::FIND_ENTITY_NAME_PLURAL)), |
|
111
|
25 |
|
\strtoupper(Inflector::tableize($replacement)), |
|
112
|
25 |
|
$filePath |
|
113
|
|
|
); |
|
114
|
|
|
|
|
115
|
25 |
|
return $this; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @param string $replacement |
|
120
|
|
|
* @param string $filePath |
|
121
|
|
|
* |
|
122
|
|
|
* @return self |
|
123
|
|
|
* @throws \RuntimeException |
|
124
|
|
|
*/ |
|
125
|
112 |
|
public function replaceEntitiesNamespace(string $replacement, string $filePath): self |
|
126
|
|
|
{ |
|
127
|
112 |
|
if (false === strpos($replacement, '\\Entities')) { |
|
128
|
|
|
throw new \RuntimeException('$replacement '.$replacement.' does not contain \\Entities\\'); |
|
129
|
|
|
} |
|
130
|
112 |
|
$this->findReplace( |
|
131
|
112 |
|
AbstractGenerator::FIND_ENTITIES_NAMESPACE, |
|
132
|
112 |
|
$this->namespaceHelper->tidy($replacement), |
|
133
|
112 |
|
$filePath |
|
134
|
|
|
); |
|
135
|
|
|
|
|
136
|
112 |
|
return $this; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @param string $replacement |
|
141
|
|
|
* @param string $filePath |
|
142
|
|
|
* |
|
143
|
|
|
* @return self |
|
144
|
|
|
* @throws \RuntimeException |
|
145
|
|
|
*/ |
|
146
|
|
|
public function replaceEntityNamespace(string $replacement, string $filePath): self |
|
147
|
|
|
{ |
|
148
|
|
|
if (false === strpos($replacement, '\\Entity')) { |
|
149
|
|
|
throw new \RuntimeException('$replacement '.$replacement.' does not contain \\Entity\\'); |
|
150
|
|
|
} |
|
151
|
|
|
$this->findReplace( |
|
152
|
|
|
AbstractGenerator::FIND_ENTITY_NAMESPACE, |
|
153
|
|
|
$this->namespaceHelper->tidy($replacement), |
|
154
|
|
|
$filePath |
|
155
|
|
|
); |
|
156
|
|
|
|
|
157
|
|
|
return $this; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* @param string $replacement |
|
162
|
|
|
* @param string $filePath |
|
163
|
|
|
* |
|
164
|
|
|
* @return self |
|
165
|
|
|
* @throws \RuntimeException |
|
166
|
|
|
*/ |
|
167
|
28 |
|
public function replaceFieldTraitNamespace(string $replacement, string $filePath): self |
|
168
|
|
|
{ |
|
169
|
28 |
|
if (false === strpos($replacement, AbstractGenerator::ENTITY_FIELD_TRAIT_NAMESPACE)) { |
|
170
|
|
|
throw new \RuntimeException( |
|
171
|
|
|
'$replacement '.$replacement.' does not contain ' |
|
172
|
|
|
.AbstractGenerator::ENTITY_FIELD_TRAIT_NAMESPACE |
|
173
|
|
|
); |
|
174
|
|
|
} |
|
175
|
28 |
|
$this->findReplace( |
|
176
|
28 |
|
AbstractGenerator::FIND_FIELD_TRAIT_NAMESPACE, |
|
177
|
28 |
|
$this->namespaceHelper->tidy($replacement), |
|
178
|
28 |
|
$filePath |
|
179
|
|
|
); |
|
180
|
|
|
|
|
181
|
28 |
|
return $this; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* @param string $replacement |
|
186
|
|
|
* @param string $filePath |
|
187
|
|
|
* |
|
188
|
|
|
* @return self |
|
189
|
|
|
* @throws \RuntimeException |
|
190
|
|
|
*/ |
|
191
|
28 |
|
public function replaceFieldInterfaceNamespace(string $replacement, string $filePath): self |
|
192
|
|
|
{ |
|
193
|
28 |
|
if (false === strpos($replacement, AbstractGenerator::ENTITY_FIELD_INTERFACE_NAMESPACE)) { |
|
194
|
|
|
throw new \RuntimeException( |
|
195
|
|
|
'$replacement '.$replacement.' does not contain '.AbstractGenerator::ENTITY_FIELD_INTERFACE_NAMESPACE |
|
196
|
|
|
); |
|
197
|
|
|
} |
|
198
|
28 |
|
$this->findReplace( |
|
199
|
28 |
|
AbstractGenerator::FIND_FIELD_INTERFACE_NAMESPACE, |
|
200
|
28 |
|
$this->namespaceHelper->tidy($replacement), |
|
201
|
28 |
|
$filePath |
|
202
|
|
|
); |
|
203
|
|
|
|
|
204
|
28 |
|
return $this; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* @param string $replacement |
|
209
|
|
|
* @param string $filePath |
|
210
|
|
|
* |
|
211
|
|
|
* @return self |
|
212
|
|
|
*/ |
|
213
|
112 |
|
public function replaceProjectNamespace(string $replacement, string $filePath): self |
|
214
|
|
|
{ |
|
215
|
112 |
|
$this->findReplace( |
|
216
|
112 |
|
AbstractGenerator::FIND_PROJECT_NAMESPACE, |
|
217
|
112 |
|
$this->namespaceHelper->tidy($replacement), |
|
218
|
112 |
|
$filePath |
|
219
|
|
|
); |
|
220
|
|
|
|
|
221
|
112 |
|
return $this; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* Simply enough, pass in a string that contains slashes and this will escape it for use in Regex |
|
226
|
|
|
* |
|
227
|
|
|
* @param string $input |
|
228
|
|
|
* |
|
229
|
|
|
* @return mixed |
|
230
|
|
|
*/ |
|
231
|
63 |
|
public function escapeSlashesForRegex(string $input) |
|
232
|
|
|
{ |
|
233
|
63 |
|
return \str_replace('\\', '\\\\', $input); |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* @param string $replacement |
|
238
|
|
|
* @param string $filePath |
|
239
|
|
|
* |
|
240
|
|
|
* @return self |
|
241
|
|
|
*/ |
|
242
|
112 |
|
public function replaceEntityRepositoriesNamespace(string $replacement, string $filePath): self |
|
243
|
|
|
{ |
|
244
|
112 |
|
$this->findReplace( |
|
245
|
112 |
|
AbstractGenerator::FIND_ENTITY_REPOSITORIES_NAMESPACE, |
|
246
|
112 |
|
$this->namespaceHelper->tidy($replacement), |
|
247
|
112 |
|
$filePath |
|
248
|
|
|
); |
|
249
|
|
|
|
|
250
|
112 |
|
return $this; |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
/** |
|
254
|
|
|
* @param string $replacement |
|
255
|
|
|
* @param string $filePath |
|
256
|
|
|
* |
|
257
|
|
|
* @return self |
|
258
|
|
|
*/ |
|
259
|
112 |
|
public function replaceEntityInterfaceNamespace(string $replacement, string $filePath): self |
|
260
|
|
|
{ |
|
261
|
112 |
|
$this->findReplace( |
|
262
|
112 |
|
AbstractGenerator::FIND_ENTITY_INTERFACE_NAMESPACE, |
|
263
|
112 |
|
$this->namespaceHelper->tidy($replacement), |
|
264
|
112 |
|
$filePath |
|
265
|
|
|
); |
|
266
|
|
|
|
|
267
|
112 |
|
return $this; |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
/** |
|
271
|
|
|
* Totally replace the defined namespace in a class/trait |
|
272
|
|
|
* with a namespace calculated from the path of the file |
|
273
|
|
|
* |
|
274
|
|
|
* @param string $filePath |
|
275
|
|
|
* |
|
276
|
|
|
* @param string $srcSubFolderName |
|
277
|
|
|
* |
|
278
|
|
|
* @param string $projectRootNamespace |
|
279
|
|
|
* |
|
280
|
|
|
* @return self |
|
281
|
|
|
* @throws DoctrineStaticMetaException |
|
282
|
|
|
*/ |
|
283
|
25 |
|
public function setNamespaceFromPath(string $filePath, string $srcSubFolderName, string $projectRootNamespace): self |
|
284
|
|
|
{ |
|
285
|
25 |
|
$pathForNamespace = substr( |
|
286
|
25 |
|
$filePath, |
|
287
|
25 |
|
strpos( |
|
288
|
25 |
|
$filePath, |
|
289
|
25 |
|
$srcSubFolderName |
|
290
|
|
|
) |
|
291
|
25 |
|
+ \strlen($srcSubFolderName) |
|
292
|
25 |
|
+ 1 |
|
293
|
|
|
); |
|
294
|
25 |
|
$pathForNamespace = substr($pathForNamespace, 0, strrpos($pathForNamespace, '/')); |
|
295
|
|
|
$namespaceToSet = $projectRootNamespace |
|
296
|
25 |
|
.'\\'.implode( |
|
297
|
25 |
|
'\\', |
|
298
|
25 |
|
explode( |
|
299
|
25 |
|
'/', |
|
300
|
25 |
|
$pathForNamespace |
|
301
|
|
|
) |
|
302
|
|
|
); |
|
303
|
25 |
|
$contents = file_get_contents($filePath); |
|
304
|
25 |
|
$contents = preg_replace( |
|
305
|
25 |
|
'%namespace[^:]+?;%', |
|
306
|
25 |
|
"namespace $namespaceToSet;", |
|
307
|
25 |
|
$contents, |
|
308
|
25 |
|
1, |
|
309
|
25 |
|
$count |
|
310
|
|
|
); |
|
311
|
25 |
|
if ($count !== 1) { |
|
312
|
|
|
throw new DoctrineStaticMetaException( |
|
313
|
|
|
'Namespace replace count is '.$count.', should be 1 when updating file: '.$filePath |
|
314
|
|
|
); |
|
315
|
|
|
} |
|
316
|
25 |
|
file_put_contents($filePath, $contents); |
|
317
|
|
|
|
|
318
|
25 |
|
return $this; |
|
319
|
|
|
} |
|
320
|
|
|
} |
|
321
|
|
|
|