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