|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\PostProcessor; |
|
6
|
|
|
|
|
7
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Filesystem\Factory\FileFactory; |
|
8
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Filesystem\File; |
|
9
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException; |
|
10
|
|
|
use Generator; |
|
11
|
|
|
use RuntimeException; |
|
12
|
|
|
use Symfony\Component\Finder\Finder; |
|
13
|
|
|
|
|
14
|
|
|
class EntityFormatter |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var FileFactory |
|
18
|
|
|
*/ |
|
19
|
|
|
private $fileFactory; |
|
20
|
|
|
/** |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
private $pathToProjectRoot; |
|
24
|
|
|
|
|
25
|
1 |
|
public function __construct(FileFactory $fileFactory) |
|
26
|
|
|
{ |
|
27
|
1 |
|
$this->fileFactory = $fileFactory; |
|
28
|
1 |
|
} |
|
29
|
|
|
|
|
30
|
1 |
|
public function run(): void |
|
31
|
|
|
{ |
|
32
|
1 |
|
foreach ($this->entityFileGenerator() as $entityFile) { |
|
33
|
1 |
|
$this->organiseTraits($entityFile); |
|
34
|
1 |
|
$entityFile->putContents(); |
|
35
|
|
|
} |
|
36
|
1 |
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @return Generator|File[] |
|
40
|
|
|
* @throws DoctrineStaticMetaException |
|
41
|
|
|
*/ |
|
42
|
1 |
|
private function entityFileGenerator(): Generator |
|
43
|
|
|
{ |
|
44
|
1 |
|
$finder = new Finder(); |
|
45
|
1 |
|
$finder->in($this->pathToProjectRoot . '/src/Entities'); |
|
46
|
1 |
|
$finder->ignoreDotFiles(true); |
|
47
|
1 |
|
$finder->files(); |
|
48
|
1 |
|
foreach ($finder as $file) { |
|
49
|
1 |
|
$entityFile = $this->fileFactory->createFromExistingPath($file->getPathname()); |
|
50
|
1 |
|
$entityFile->loadContents(); |
|
51
|
1 |
|
yield $entityFile; |
|
52
|
|
|
} |
|
53
|
1 |
|
} |
|
54
|
|
|
|
|
55
|
1 |
|
private function organiseTraits(File $entityFile): void |
|
56
|
|
|
{ |
|
57
|
1 |
|
$body = $this->getEntityBody($entityFile); |
|
58
|
1 |
|
$extraCode = $body; |
|
59
|
1 |
|
preg_match_all('%\s+?use [^;]+;%', $body, $traitMatches); |
|
60
|
1 |
|
$traits = []; |
|
61
|
1 |
|
foreach ($traitMatches[0] as $traitLine) { |
|
62
|
1 |
|
$extraCode = str_replace($traitLine, '', $extraCode); |
|
63
|
1 |
|
$traits[$this->getTraitType($traitLine)][] = trim($traitLine); |
|
64
|
|
|
} |
|
65
|
1 |
|
ksort($traits); |
|
66
|
1 |
|
$traitsBody = ''; |
|
67
|
1 |
|
foreach ($traits as $title => $lines) { |
|
68
|
1 |
|
$comment = "\n" . substr($title, 2); |
|
69
|
1 |
|
$extraCode = str_replace($comment, '', $extraCode); |
|
70
|
1 |
|
$traitsBody .= $comment; |
|
71
|
1 |
|
$traitsBody .= "\n " . implode("\n ", $lines) . "\n"; |
|
72
|
|
|
} |
|
73
|
1 |
|
$extraCode = \ts\preg_replace("%\n{3,}%", "\n\n", $extraCode); |
|
74
|
1 |
|
$organisedBody = "$traitsBody\n$extraCode"; |
|
75
|
1 |
|
$organisedBody = str_replace($body, $organisedBody, $entityFile->getContents()); |
|
76
|
1 |
|
$organisedBody = \ts\preg_replace("%\n+?}%", "\n}", $organisedBody); |
|
77
|
1 |
|
$entityFile->setContents($organisedBody); |
|
78
|
1 |
|
} |
|
79
|
|
|
|
|
80
|
1 |
|
private function getEntityBody(File $entityFile): string |
|
81
|
|
|
{ |
|
82
|
1 |
|
preg_match('%class[^{].+?{(.+)}%s', $entityFile->getContents(), $matches); |
|
83
|
|
|
|
|
84
|
1 |
|
return $matches[1]; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
1 |
|
private function getTraitType(string $traitLine): string |
|
88
|
|
|
{ |
|
89
|
1 |
|
$commentStart = " /**\n *"; |
|
90
|
1 |
|
$commentEnd = "\n */"; |
|
91
|
1 |
|
if (false !== \ts\stringContains($traitLine, 'Embeddable')) { |
|
|
|
|
|
|
92
|
1 |
|
return "5 $commentStart Embeddables $commentEnd"; |
|
93
|
|
|
} |
|
94
|
1 |
|
if (false !== \ts\stringContains($traitLine, 'use HasRequired')) { |
|
95
|
1 |
|
return "1 $commentStart Required Relations $commentEnd"; |
|
96
|
|
|
} |
|
97
|
1 |
|
if (false !== \ts\stringContains($traitLine, 'use Has')) { |
|
98
|
1 |
|
return "2 $commentStart Relations $commentEnd"; |
|
99
|
|
|
} |
|
100
|
1 |
|
if (false !== \ts\stringContains($traitLine, 'use DSM\\Traits')) { |
|
101
|
1 |
|
return "0 $commentStart DSM Traits $commentEnd"; |
|
102
|
|
|
} |
|
103
|
1 |
|
if (false !== \ts\stringContains($traitLine, 'use DSM\\Fields')) { |
|
104
|
1 |
|
return "3 $commentStart DSM Fields $commentEnd"; |
|
105
|
|
|
} |
|
106
|
1 |
|
if (false !== \ts\stringContains($traitLine, 'FieldTrait')) { |
|
107
|
1 |
|
return "4 $commentStart Fields $commentEnd"; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
throw new RuntimeException('Failed finding trait type for line ' . $traitLine); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @param string $pathToProjectRoot |
|
115
|
|
|
* |
|
116
|
|
|
* @return EntityFormatter |
|
117
|
|
|
*/ |
|
118
|
1 |
|
public function setPathToProjectRoot(string $pathToProjectRoot): EntityFormatter |
|
119
|
|
|
{ |
|
120
|
1 |
|
$this->pathToProjectRoot = $pathToProjectRoot; |
|
121
|
|
|
|
|
122
|
1 |
|
return $this; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|