1
|
|
|
<?php |
2
|
|
|
namespace gossi\codegen\generator; |
3
|
|
|
|
4
|
|
|
use gossi\codegen\config\CodeFileGeneratorConfig; |
5
|
|
|
use gossi\codegen\model\GenerateableInterface; |
6
|
|
|
use gossi\docblock\Docblock; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Code file generator. |
10
|
|
|
* |
11
|
|
|
* Generates code for a model and puts it into a file with `<?php` statements. Can also |
12
|
|
|
* generate header comments. |
13
|
|
|
* |
14
|
|
|
* @author Thomas Gossmann |
15
|
|
|
*/ |
16
|
|
|
class CodeFileGenerator extends CodeGenerator { |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Creates a new CodeFileGenerator |
20
|
|
|
* |
21
|
|
|
* @see https://php-code-generator.readthedocs.org/en/latest/generator.html |
22
|
|
|
* @param CodeFileGeneratorConfig|array $config |
23
|
|
|
*/ |
24
|
6 |
|
public function __construct($config = null) { |
25
|
6 |
|
if (is_array($config)) { |
26
|
4 |
|
$this->config = new CodeFileGeneratorConfig($config); |
27
|
6 |
|
} else if ($config instanceof CodeFileGeneratorConfig) { |
28
|
|
|
$this->config = $config; |
29
|
|
|
} else { |
30
|
2 |
|
$this->config = new CodeFileGeneratorConfig(); |
31
|
|
|
} |
32
|
|
|
|
33
|
6 |
|
$this->init(); |
34
|
6 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritDoc} |
38
|
|
|
* |
39
|
|
|
* @return CodeFileGeneratorConfig |
40
|
|
|
*/ |
41
|
|
|
public function getConfig() { |
42
|
|
|
return $this->config; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritDoc} |
47
|
|
|
*/ |
48
|
6 |
|
public function generate(GenerateableInterface $model) { |
49
|
6 |
|
$content = "<?php\n"; |
50
|
|
|
|
51
|
6 |
|
$comment = $this->config->getHeaderComment(); |
52
|
6 |
|
if (!empty($comment)) { |
53
|
|
|
$docblock = new Docblock(); |
54
|
|
|
$docblock->setLongDescription($comment); |
55
|
|
|
$content .= str_replace('/**', '/*', $docblock->toString()) . "\n"; |
56
|
|
|
} |
57
|
|
|
|
58
|
6 |
|
if ($this->config->getHeaderDocblock() instanceof Docblock) { |
59
|
|
|
$content .= $this->config->getHeaderDocblock()->toString() . "\n"; |
60
|
|
|
} |
61
|
|
|
|
62
|
6 |
|
if ($this->config->getDeclareStrictTypes()) { |
63
|
1 |
|
$content .= "declare(strict_types=1);\n\n"; |
64
|
1 |
|
} |
65
|
|
|
|
66
|
6 |
|
$content .= parent::generate($model); |
67
|
|
|
|
68
|
6 |
|
if ($this->config->getBlankLineAtEnd()) { |
69
|
6 |
|
$content .= "\n"; |
70
|
6 |
|
} |
71
|
|
|
|
72
|
6 |
|
return $content; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|