1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Process; |
4
|
|
|
|
5
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Filesystem\File; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* This process handles updating the namespace for deeply nested Entities, |
9
|
|
|
* eg ones that are not in the root Entities namespace |
10
|
|
|
*/ |
11
|
|
|
class ReplaceEntitiesSubNamespaceProcess implements ProcessInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var string|null |
15
|
|
|
*/ |
16
|
|
|
private $entitySubNamespace; |
17
|
|
|
|
18
|
3 |
|
public function setEntityFqn(string $entityFqn) |
19
|
|
|
{ |
20
|
3 |
|
if (false === \ts\stringContains($entityFqn, '\\Entities\\')) { |
21
|
1 |
|
throw new \RuntimeException( |
22
|
1 |
|
'This does not look like an Entity FQN: ' . $entityFqn |
23
|
|
|
); |
24
|
|
|
} |
25
|
2 |
|
$this->setEntitySubNamespaceFromEntityFqn($entityFqn); |
26
|
|
|
|
27
|
2 |
|
return $this; |
28
|
|
|
} |
29
|
|
|
|
30
|
2 |
|
private function setEntitySubNamespaceFromEntityFqn(string $entityFqn): void |
31
|
|
|
{ |
32
|
2 |
|
$fromEntities = substr($entityFqn, \ts\strpos($entityFqn, '\\Entities\\')); |
33
|
2 |
|
$exploded = explode('\\', $fromEntities); |
34
|
2 |
|
array_pop($exploded); |
35
|
2 |
|
array_shift($exploded); |
36
|
2 |
|
array_shift($exploded); |
37
|
2 |
|
$exploded = array_filter($exploded); |
38
|
2 |
|
if ([] === $exploded) { |
39
|
|
|
return; |
40
|
|
|
} |
41
|
2 |
|
$this->entitySubNamespace = implode('\\', $exploded); |
42
|
2 |
|
} |
43
|
|
|
|
44
|
2 |
|
public function run(File\FindReplace $findReplace): void |
45
|
|
|
{ |
46
|
2 |
|
if (null === $this->entitySubNamespace) { |
47
|
|
|
return; |
48
|
|
|
} |
49
|
2 |
|
$this->replaceEntities($findReplace); |
50
|
2 |
|
$this->replaceEntity($findReplace); |
51
|
2 |
|
} |
52
|
|
|
|
53
|
2 |
|
private function replaceEntities(File\FindReplace $findReplace): void |
54
|
|
|
{ |
55
|
2 |
|
$pattern = $findReplace->convertForwardSlashesToBackSlashes('%/Entities(/|;)(?!Abstract)%'); |
56
|
2 |
|
$replacement = '\\Entities\\' . $this->entitySubNamespace . '$1'; |
57
|
2 |
|
$findReplace->findReplaceRegex($pattern, $replacement); |
58
|
2 |
|
} |
59
|
|
|
|
60
|
2 |
|
private function replaceEntity(File\FindReplace $findReplace) |
61
|
|
|
{ |
62
|
2 |
|
$pattern = $findReplace->convertForwardSlashesToBackSlashes( |
63
|
2 |
|
'%(.+?)/Entity/([^/]+?)(/|;)(?!Fixtures)(?!Abstract)%' |
64
|
|
|
); |
65
|
2 |
|
$replacement = '$1\\Entity\\\$2\\' . $this->entitySubNamespace . '$3'; |
66
|
2 |
|
$findReplace->findReplaceRegex($pattern, $replacement); |
67
|
2 |
|
} |
68
|
|
|
} |
69
|
|
|
|