|
1
|
|
|
<?php |
|
2
|
|
|
namespace gossi\codegen\config; |
|
3
|
|
|
|
|
4
|
|
|
use gossi\docblock\Docblock; |
|
5
|
|
|
use Symfony\Component\OptionsResolver\Options; |
|
6
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Configuration for code file generation |
|
10
|
|
|
* |
|
11
|
|
|
* @author Thomas Gossmann |
|
12
|
|
|
*/ |
|
13
|
|
|
class CodeFileGeneratorConfig extends CodeGeneratorConfig { |
|
14
|
|
|
|
|
15
|
9 |
|
protected function configureOptions(OptionsResolver $resolver) { |
|
16
|
9 |
|
parent::configureOptions($resolver); |
|
17
|
|
|
|
|
18
|
9 |
|
$resolver->setDefaults([ |
|
19
|
9 |
|
'headerComment' => null, |
|
20
|
9 |
|
'headerDocblock' => null, |
|
21
|
9 |
|
'blankLineAtEnd' => true, |
|
22
|
9 |
|
'declareStrictTypes' => false, |
|
23
|
|
|
'generateScalarTypeHints' => function (Options $options) { |
|
24
|
9 |
|
return $options['declareStrictTypes']; |
|
25
|
9 |
|
}, |
|
26
|
|
|
'generateReturnTypeHints' => function (Options $options) { |
|
27
|
9 |
|
return $options['declareStrictTypes']; |
|
28
|
9 |
|
}, |
|
29
|
9 |
|
]); |
|
30
|
|
|
|
|
31
|
9 |
|
$resolver->setAllowedTypes('headerComment', ['null', 'string', 'gossi\\docblock\\Docblock']); |
|
32
|
9 |
|
$resolver->setAllowedTypes('headerDocblock', ['null', 'string', 'gossi\\docblock\\Docblock']); |
|
33
|
9 |
|
$resolver->setAllowedTypes('blankLineAtEnd', 'bool'); |
|
34
|
9 |
|
$resolver->setAllowedTypes('declareStrictTypes', 'bool'); |
|
35
|
|
|
|
|
36
|
|
|
$resolver->setNormalizer('headerComment', function (Options $options, $value) { |
|
37
|
9 |
|
return $this->toDocblock($value); |
|
38
|
9 |
|
}); |
|
39
|
9 |
|
$resolver->setNormalizer('headerDocblock', function (Options $options, $value) { |
|
40
|
9 |
|
return $this->toDocblock($value); |
|
41
|
9 |
|
}); |
|
42
|
9 |
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* |
|
46
|
|
|
* @param mixed $value |
|
47
|
|
|
* @return Docblock|null |
|
48
|
|
|
*/ |
|
49
|
9 |
|
private function toDocblock($value) { |
|
50
|
9 |
|
if (is_string($value)) { |
|
51
|
1 |
|
$value = Docblock::create()->setLongDescription($value); |
|
52
|
1 |
|
} |
|
53
|
|
|
|
|
54
|
9 |
|
return $value; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Returns the file header comment |
|
59
|
|
|
* |
|
60
|
|
|
* @return string the header comment |
|
61
|
|
|
*/ |
|
62
|
7 |
|
public function getHeaderComment() { |
|
63
|
7 |
|
return $this->options['headerComment']; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Sets the file header comment |
|
68
|
|
|
* |
|
69
|
|
|
* @param string $comment the header comment |
|
70
|
|
|
* @return $this |
|
71
|
|
|
*/ |
|
72
|
1 |
|
public function setHeaderComment($comment) { |
|
73
|
1 |
|
$this->options['headerComment'] = $comment; |
|
74
|
1 |
|
return $this; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Returns the file header docblock |
|
79
|
|
|
* |
|
80
|
|
|
* @return Docblock the docblock |
|
81
|
|
|
*/ |
|
82
|
7 |
|
public function getHeaderDocblock() { |
|
83
|
7 |
|
return $this->options['headerDocblock']; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Sets the file header docblock |
|
88
|
|
|
* |
|
89
|
|
|
* @param Docblock $docblock the docblock |
|
90
|
|
|
* @return $this |
|
91
|
|
|
*/ |
|
92
|
1 |
|
public function setHeaderDocblock(Docblock $docblock) { |
|
93
|
1 |
|
$this->options['headerDocblock'] = $docblock; |
|
94
|
1 |
|
return $this; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Returns whether a blank line should be generated at the end of the file |
|
99
|
|
|
* |
|
100
|
|
|
* @return bool `true` if it will be generated and `false` if not |
|
101
|
|
|
*/ |
|
102
|
7 |
|
public function getBlankLineAtEnd() { |
|
103
|
7 |
|
return $this->options['blankLineAtEnd']; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Sets whether a blank line should be generated at the end of the file |
|
108
|
|
|
* |
|
109
|
|
|
* @param bool $show `true` if it will be generated and `false` if not |
|
110
|
|
|
* @return $this |
|
111
|
|
|
*/ |
|
112
|
1 |
|
public function setBlankLineAtEnd($show) { |
|
113
|
1 |
|
$this->options['blankLineAtEnd'] = $show; |
|
114
|
1 |
|
return $this; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Returns whether a `declare(strict_types=1);` statement should be printed |
|
119
|
|
|
* below the header comments (PHP 7) |
|
120
|
|
|
* |
|
121
|
|
|
* @return bool `true` if it will be printed and `false` if not |
|
122
|
|
|
*/ |
|
123
|
8 |
|
public function getDeclareStrictTypes() { |
|
124
|
8 |
|
return $this->options['declareStrictTypes']; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Sets whether a `declare(strict_types=1);` statement should be printed |
|
129
|
|
|
* below the header comments (PHP 7) |
|
130
|
|
|
* |
|
131
|
|
|
* @param bool $strict `true` if it will be printed and `false` if not |
|
132
|
|
|
* @return $this |
|
133
|
|
|
*/ |
|
134
|
1 |
|
public function setDeclareStrictTypes($strict) { |
|
135
|
1 |
|
$this->options['declareStrictTypes'] = $strict; |
|
136
|
1 |
|
return $this; |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|