|
1
|
|
|
<?php |
|
2
|
|
|
namespace gossi\codegen\generator\builder; |
|
3
|
|
|
|
|
4
|
|
|
use gossi\codegen\generator\ModelGenerator; |
|
5
|
|
|
use gossi\codegen\generator\utils\Writer; |
|
6
|
|
|
use gossi\codegen\model\AbstractModel; |
|
7
|
|
|
use gossi\docblock\Docblock; |
|
8
|
|
|
use gossi\codegen\config\CodeGeneratorConfig; |
|
9
|
|
|
use gossi\codegen\model\DocblockInterface; |
|
10
|
|
|
|
|
11
|
|
|
abstract class AbstractBuilder { |
|
12
|
|
|
|
|
13
|
|
|
/** @var ModelGenerator */ |
|
14
|
|
|
protected $generator; |
|
15
|
|
|
|
|
16
|
|
|
/** @var Writer */ |
|
17
|
|
|
protected $writer; |
|
18
|
|
|
|
|
19
|
|
|
/** @var CodeGeneratorConfig */ |
|
20
|
|
|
protected $config; |
|
21
|
|
|
|
|
22
|
39 |
|
public function __construct(ModelGenerator $generator) { |
|
23
|
39 |
|
$this->generator = $generator; |
|
24
|
39 |
|
$this->writer = $generator->getWriter(); |
|
25
|
39 |
|
$this->config = $generator->getConfig(); |
|
26
|
39 |
|
} |
|
27
|
|
|
|
|
28
|
|
|
public abstract function build(AbstractModel $model); |
|
|
|
|
|
|
29
|
|
|
|
|
30
|
6 |
|
protected function generate(AbstractModel $model) { |
|
31
|
6 |
|
$builder = $this->generator->getFactory()->getBuilder($model); |
|
32
|
6 |
|
$builder->build($model); |
|
33
|
6 |
|
} |
|
34
|
|
|
|
|
35
|
32 |
|
protected function ensureBlankLine() { |
|
36
|
32 |
|
if (!$this->writer->endsWith("\n\n") && strlen($this->writer->rtrim()->getContent()) > 0) { |
|
37
|
5 |
|
$this->writer->writeln(); |
|
38
|
|
|
} |
|
39
|
32 |
|
} |
|
40
|
|
|
|
|
41
|
32 |
|
protected function buildDocblock(DocblockInterface $model) { |
|
42
|
32 |
|
$this->ensureBlankLine(); |
|
43
|
32 |
|
if ($this->config->getGenerateDocblock()) { |
|
44
|
3 |
|
$model->generateDocblock(); |
|
45
|
|
|
} |
|
46
|
32 |
|
$docblock = $model->getDocblock(); |
|
47
|
32 |
|
if (!$docblock->isEmpty() || $this->config->getGenerateEmptyDocblock()) { |
|
48
|
3 |
|
$this->writer->writeln($docblock->toString()); |
|
49
|
|
|
} |
|
50
|
32 |
|
} |
|
51
|
|
|
|
|
52
|
|
|
} |
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
@returndoc comment to communicate to implementors of these methods what they are expected to return.