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