|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace gossi\codegen\generator\builder\parts; |
|
5
|
|
|
|
|
6
|
|
|
use gossi\codegen\generator\ComparatorFactory; |
|
7
|
|
|
use gossi\codegen\model\AbstractModel; |
|
8
|
|
|
use gossi\codegen\model\AbstractPhpStruct; |
|
9
|
|
|
use gossi\codegen\model\ConstantsInterface; |
|
10
|
|
|
use gossi\codegen\model\DocblockInterface; |
|
11
|
|
|
use gossi\codegen\model\NamespaceInterface; |
|
12
|
|
|
use gossi\codegen\model\PropertiesInterface; |
|
13
|
|
|
use gossi\codegen\model\TraitsInterface; |
|
14
|
|
|
|
|
15
|
|
|
trait StructBuilderPart { |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @return void |
|
19
|
|
|
*/ |
|
20
|
|
|
abstract protected function ensureBlankLine(): void; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param AbstractModel $model |
|
24
|
|
|
* @return void |
|
25
|
|
|
*/ |
|
26
|
|
|
abstract protected function generate(AbstractModel $model): void; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param DocblockInterface $model |
|
30
|
|
|
* @return void |
|
31
|
|
|
*/ |
|
32
|
|
|
abstract protected function buildDocblock(DocblockInterface $model): void; |
|
33
|
|
|
|
|
34
|
16 |
|
protected function buildHeader(AbstractPhpStruct $model): void { |
|
35
|
16 |
|
$this->buildNamespace($model); |
|
36
|
16 |
|
$this->buildRequiredFiles($model); |
|
37
|
16 |
|
$this->buildUseStatements($model); |
|
38
|
16 |
|
$this->buildDocblock($model); |
|
39
|
16 |
|
} |
|
40
|
|
|
|
|
41
|
16 |
|
protected function buildNamespace(NamespaceInterface $model): void { |
|
42
|
16 |
|
if ($namespace = $model->getNamespace()) { |
|
43
|
2 |
|
$this->writer->writeln('namespace ' . $namespace . ';'); |
|
44
|
|
|
} |
|
45
|
16 |
|
} |
|
46
|
|
|
|
|
47
|
16 |
|
protected function buildRequiredFiles(AbstractPhpStruct $model): void { |
|
48
|
16 |
|
if ($files = $model->getRequiredFiles()) { |
|
49
|
16 |
|
$this->ensureBlankLine(); |
|
50
|
16 |
|
foreach ($files as $file) { |
|
51
|
1 |
|
$this->writer->writeln('require_once ' . var_export($file, true) . ';'); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
16 |
|
} |
|
55
|
|
|
|
|
56
|
16 |
|
protected function buildUseStatements(AbstractPhpStruct $model): void { |
|
57
|
16 |
|
if ($useStatements = $model->getUseStatements()) { |
|
58
|
16 |
|
$this->ensureBlankLine(); |
|
59
|
16 |
|
foreach ($useStatements as $alias => $namespace) { |
|
60
|
1 |
|
if (false === strpos($namespace, '\\')) { |
|
61
|
1 |
|
$commonName = $namespace; |
|
62
|
|
|
} else { |
|
63
|
1 |
|
$commonName = substr($namespace, strrpos($namespace, '\\') + 1); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
1 |
|
if (false === strpos($namespace, '\\') && !$model->getNamespace()) { |
|
67
|
|
|
//avoid fatal 'The use statement with non-compound name '$commonName' has no effect' |
|
68
|
1 |
|
continue; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
1 |
|
$this->writer->write('use ' . $namespace); |
|
72
|
|
|
|
|
73
|
1 |
|
if ($commonName !== $alias) { |
|
74
|
1 |
|
$this->writer->write(' as ' . $alias); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
1 |
|
$this->writer->writeln(';'); |
|
78
|
|
|
} |
|
79
|
16 |
|
$this->ensureBlankLine(); |
|
80
|
|
|
} |
|
81
|
16 |
|
} |
|
82
|
|
|
|
|
83
|
14 |
|
protected function buildTraits(TraitsInterface $model): void { |
|
84
|
14 |
|
foreach ($model->getTraits() as $trait) { |
|
85
|
1 |
|
$this->writer->write('use '); |
|
86
|
1 |
|
$this->writer->writeln($trait . ';'); |
|
87
|
|
|
} |
|
88
|
14 |
|
} |
|
89
|
|
|
|
|
90
|
15 |
|
protected function buildConstants(ConstantsInterface $model): void { |
|
91
|
15 |
|
foreach ($model->getConstants() as $constant) { |
|
92
|
2 |
|
$this->generate($constant); |
|
93
|
|
|
} |
|
94
|
15 |
|
} |
|
95
|
|
|
|
|
96
|
14 |
|
protected function buildProperties(PropertiesInterface $model): void { |
|
97
|
14 |
|
foreach ($model->getProperties() as $property) { |
|
98
|
3 |
|
$this->generate($property); |
|
99
|
|
|
} |
|
100
|
14 |
|
} |
|
101
|
|
|
|
|
102
|
16 |
|
protected function buildMethods(AbstractPhpStruct $model): void { |
|
103
|
16 |
|
foreach ($model->getMethods() as $method) { |
|
104
|
3 |
|
$this->generate($method); |
|
105
|
|
|
} |
|
106
|
16 |
|
} |
|
107
|
|
|
|
|
108
|
16 |
|
private function sortUseStatements(AbstractPhpStruct $model): void { |
|
109
|
16 |
|
if ($this->config->isSortingEnabled() |
|
110
|
16 |
|
&& ($useStatementSorting = $this->config->getUseStatementSorting()) !== false) { |
|
111
|
16 |
|
if (is_string($useStatementSorting)) { |
|
112
|
16 |
|
$useStatementSorting = ComparatorFactory::createUseStatementComparator($useStatementSorting); |
|
113
|
|
|
} |
|
114
|
16 |
|
$model->getUseStatements()->sort($useStatementSorting); |
|
115
|
|
|
} |
|
116
|
16 |
|
} |
|
117
|
|
|
|
|
118
|
15 |
|
private function sortConstants(ConstantsInterface $model): void { |
|
119
|
15 |
|
if ($this->config->isSortingEnabled() |
|
120
|
15 |
|
&& ($constantSorting = $this->config->getConstantSorting()) !== false) { |
|
121
|
15 |
|
if (is_string($constantSorting)) { |
|
122
|
15 |
|
$constantSorting = ComparatorFactory::createConstantComparator($constantSorting); |
|
123
|
|
|
} |
|
124
|
15 |
|
$model->getConstants()->sort($constantSorting); |
|
125
|
|
|
} |
|
126
|
15 |
|
} |
|
127
|
|
|
|
|
128
|
14 |
|
private function sortProperties(PropertiesInterface $model): void { |
|
129
|
14 |
|
if ($this->config->isSortingEnabled() |
|
130
|
14 |
|
&& ($propertySorting = $this->config->getPropertySorting()) !== false) { |
|
131
|
14 |
|
if (is_string($propertySorting)) { |
|
132
|
14 |
|
$propertySorting = ComparatorFactory::createPropertyComparator($propertySorting); |
|
133
|
|
|
} |
|
134
|
14 |
|
$model->getProperties()->sort($propertySorting); |
|
135
|
|
|
} |
|
136
|
14 |
|
} |
|
137
|
|
|
|
|
138
|
16 |
|
private function sortMethods(AbstractPhpStruct $model): void { |
|
139
|
16 |
|
if ($this->config->isSortingEnabled() |
|
140
|
16 |
|
&& ($methodSorting = $this->config->getMethodSorting()) !== false) { |
|
141
|
16 |
|
if (is_string($methodSorting)) { |
|
142
|
16 |
|
$methodSorting = ComparatorFactory::createMethodComparator($methodSorting); |
|
143
|
|
|
} |
|
144
|
16 |
|
$model->getMethods()->sort($methodSorting); |
|
145
|
|
|
} |
|
146
|
16 |
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|