13
13
use Twig \Environment ;
14
14
use Twig \Loader \FilesystemLoader ;
15
15
use Twig \TwigFilter ;
16
+ use TwigGenerator \Extension \ExtraFilterExtension ;
17
+ use TwigGenerator \Extension \PHPPrintExtension ;
18
+ use TwigGenerator \Extension \TwigPrintExtension ;
16
19
17
20
/**
18
21
* @author Cédric Lombardot
@@ -22,225 +25,139 @@ abstract class BaseBuilder implements BuilderInterface
22
25
/**
23
26
* Default Twig file extension.
24
27
*/
25
- const TWIG_EXTENSION = '.php.twig ' ;
28
+ final protected const TWIG_EXTENSION = '.php.twig ' ;
26
29
27
- /**
28
- * @var \TwigGenerator\Builder\Generator The generator.
29
- */
30
- protected $ generator ;
30
+ protected Generator $ generator ;
31
31
32
32
/**
33
- * @var array A list of template directories.
33
+ * @var string[] A list of template directories.
34
34
*/
35
- protected $ templateDirectories = array () ;
35
+ protected array $ templateDirectories = [] ;
36
36
37
- /**
38
- * @var string
39
- */
40
- protected $ templateName ;
37
+ protected string $ templateName = '' ;
41
38
42
- /**
43
- * @var string
44
- */
45
- protected $ outputName ;
39
+ protected string $ outputName = '' ;
46
40
47
- /**
48
- * @var Boolean
49
- */
50
- protected $ mustOverwriteIfExists = false ;
41
+ protected bool $ mustOverwriteIfExists = false ;
51
42
52
- /**
53
- * @var array
54
- */
55
- protected $ twigFilters = array (
56
- );
43
+ protected array $ twigFilters = [];
57
44
58
- /**
59
- * @var array
60
- */
61
- protected $ variables = array ();
45
+ protected array $ variables = [];
62
46
63
- /**
64
- * @var array
65
- */
66
- protected $ twigExtensions = array (
67
- '\\TwigGenerator \\Extension \\PHPPrintExtension ' ,
68
- '\\TwigGenerator \\Extension \\TwigPrintExtension ' ,
69
- '\\TwigGenerator \\Extension \\ExtraFilterExtension ' ,
70
- );
47
+ protected array $ twigExtensions = [
48
+ PHPPrintExtension::class,
49
+ TwigPrintExtension::class,
50
+ ExtraFilterExtension::class,
51
+ ];
71
52
72
- /**
73
- * Constructor
74
- */
75
53
public function __construct ()
76
54
{
77
55
$ this ->templateDirectories = $ this ->getDefaultTemplateDirs ();
78
56
$ this ->templateName = $ this ->getDefaultTemplateName ();
79
57
}
80
58
81
- /**
82
- * {@inheritDoc}
83
- */
84
- public function setGenerator (Generator $ generator )
59
+ public function setGenerator (Generator $ generator ): void
85
60
{
86
61
$ this ->generator = $ generator ;
87
62
}
88
63
89
- /**
90
- * {@inheritDoc}
91
- */
92
- public function getGenerator ()
64
+ public function getGenerator (): Generator
93
65
{
94
66
return $ this ->generator ;
95
67
}
96
68
97
- /**
98
- * {@inheritDoc}
99
- */
100
- public function addTemplateDir ($ templateDir )
69
+ public function addTemplateDir ($ templateDir ): void
101
70
{
102
71
$ this ->templateDirectories [$ templateDir ] = $ templateDir ;
103
72
}
104
73
105
- /**
106
- * {@inheritDoc}
107
- */
108
- public function setTemplateDirs (array $ templateDirs )
74
+ public function setTemplateDirs (array $ templateDirs ): void
109
75
{
110
76
$ this ->templateDirectories = $ templateDirs ;
111
77
}
112
78
113
- /**
114
- * {@inheritDoc}
115
- */
116
- public function getTemplateDirs ()
79
+ /** @return string[] */
80
+ public function getTemplateDirs (): array
117
81
{
118
82
return $ this ->templateDirectories ;
119
83
}
120
84
121
- /**
122
- * {@inheritDoc}
123
- */
124
- public function getDefaultTemplateDirs ()
85
+ public function getDefaultTemplateDirs (): array
125
86
{
126
- return array () ;
87
+ return [] ;
127
88
}
128
89
129
- /**
130
- * {@inheritDoc}
131
- */
132
- public function setTemplateName ($ templateName )
90
+ public function setTemplateName (string $ templateName ): void
133
91
{
134
92
$ this ->templateName = $ templateName ;
135
93
}
136
94
137
- /**
138
- * {@inheritDoc}
139
- */
140
- public function getTemplateName ()
95
+ public function getTemplateName (): string
141
96
{
142
97
return $ this ->templateName ;
143
98
}
144
99
145
- /**
146
- * {@inheritDoc}
147
- */
148
- public function getDefaultTemplateName ()
100
+ public function getDefaultTemplateName (): string
149
101
{
150
102
return $ this ->getSimpleClassName () . self ::TWIG_EXTENSION ;
151
103
}
152
104
153
- /**
154
- * {@inheritDoc}
155
- */
156
- public function getSimpleClassName ($ class = null )
105
+ public function getSimpleClassName ($ class = null ): string
157
106
{
158
107
if (null === $ class ) {
159
- $ class = get_class ( $ this ) ;
108
+ $ class = self ::class ;
160
109
}
161
110
162
111
$ classParts = explode ('\\' , $ class );
163
- $ simpleClassName = array_pop ($ classParts );
164
-
165
- return $ simpleClassName ;
112
+ return array_pop ($ classParts );
166
113
}
167
114
168
- /**
169
- * {@inheritDoc}
170
- */
171
- public function setOutputName ($ outputName )
115
+ public function setOutputName (string $ outputName ): void
172
116
{
173
117
$ this ->outputName = $ outputName ;
174
118
}
175
119
176
- /**
177
- * {@inheritDoc}
178
- */
179
- public function getOutputName ()
120
+ public function getOutputName (): string
180
121
{
181
122
return $ this ->outputName ;
182
123
}
183
124
184
- /**
185
- * {@inheritDoc}
186
- */
187
- public function mustOverwriteIfExists ()
125
+ public function mustOverwriteIfExists (): bool
188
126
{
189
127
return $ this ->mustOverwriteIfExists ;
190
128
}
191
129
192
- /**
193
- * {@inheritDoc}
194
- */
195
- public function setMustOverwriteIfExists ($ status = true )
130
+ public function setMustOverwriteIfExists (bool $ status = true ): void
196
131
{
197
132
$ this ->mustOverwriteIfExists = $ status ;
198
133
}
199
134
200
- /**
201
- * {@inheritDoc}
202
- */
203
- public function setVariables (array $ variables )
135
+ public function setVariables (array $ variables ): void
204
136
{
205
137
$ this ->variables = $ variables ;
206
138
}
207
139
208
- /**
209
- * {@inheritDoc}
210
- */
211
- public function setVariable ($ key , $ value )
140
+ public function setVariable (string $ key , mixed $ value ): void
212
141
{
213
142
$ this ->variables [$ key ] = $ value ;
214
143
}
215
144
216
- /**
217
- * {@inheritDoc}
218
- */
219
- public function getVariables ()
145
+ public function getVariables (): array
220
146
{
221
147
return $ this ->variables ;
222
148
}
223
149
224
- /**
225
- * {@inheritDoc}
226
- */
227
- public function hasVariable ($ key )
150
+ public function hasVariable ($ key ): bool
228
151
{
229
152
return isset ($ this ->variables [$ key ]);
230
153
}
231
154
232
- /**
233
- * {@inheritDoc}
234
- */
235
- public function getVariable ($ key , $ default = null )
155
+ public function getVariable (string $ key , mixed $ default = null ): mixed
236
156
{
237
157
return $ this ->hasVariable ($ key ) ? $ this ->variables [$ key ] : $ default ;
238
158
}
239
159
240
- /**
241
- * {@inheritDoc}
242
- */
243
- public function writeOnDisk ($ outputDirectory )
160
+ public function writeOnDisk (string $ outputDirectory ): void
244
161
{
245
162
$ path = $ outputDirectory . DIRECTORY_SEPARATOR . $ this ->getOutputName ();
246
163
$ dir = dirname ($ path );
@@ -254,10 +171,7 @@ public function writeOnDisk($outputDirectory)
254
171
}
255
172
}
256
173
257
- /**
258
- * {@inheritDoc}
259
- */
260
- public function getCode ()
174
+ public function getCode (): string
261
175
{
262
176
$ twig = $ this ->getTwigEnvironment ();
263
177
$ template = $ twig ->load ($ this ->getTemplateName ());
@@ -268,10 +182,7 @@ public function getCode()
268
182
return $ template ->render ($ variables );
269
183
}
270
184
271
- /**
272
- * {@inheritDoc}
273
- */
274
- public function addTwigFilters (array $ filters )
185
+ public function addTwigFilters (array $ filters ): void
275
186
{
276
187
foreach ($ filters as $ filter ) {
277
188
if (is_string ($ filter )) {
@@ -283,10 +194,7 @@ public function addTwigFilters(array $filters)
283
194
}
284
195
}
285
196
286
- /**
287
- * {@inheritDoc}
288
- */
289
- public function addTwigExtensions (array $ extensions )
197
+ public function addTwigExtensions (array $ extensions ): void
290
198
{
291
199
foreach ($ extensions as $ extension ) {
292
200
if (is_string ($ extension )) {
@@ -301,10 +209,8 @@ public function addTwigExtensions(array $extensions)
301
209
/**
302
210
* Initialize the Twig Environment which automatically loads
303
211
* extensions and filters.
304
- *
305
- * @return Environment
306
212
*/
307
- protected function getTwigEnvironment ()
213
+ protected function getTwigEnvironment (): Environment
308
214
{
309
215
$ loader = new FilesystemLoader ($ this ->getTemplateDirs ());
310
216
$ twig = new Environment ($ loader , array (
@@ -320,7 +226,7 @@ protected function getTwigEnvironment()
320
226
return $ twig ;
321
227
}
322
228
323
- protected function loadTwigFilters (Environment $ twig )
229
+ protected function loadTwigFilters (Environment $ twig ): void
324
230
{
325
231
foreach ($ this ->twigFilters as $ twigFilter ) {
326
232
if (is_object ($ twigFilter )) {
@@ -335,7 +241,7 @@ protected function loadTwigFilters(Environment $twig)
335
241
}
336
242
}
337
243
338
- protected function loadTwigExtensions (Environment $ twig )
244
+ protected function loadTwigExtensions (Environment $ twig ): void
339
245
{
340
246
foreach ($ this ->twigExtensions as $ twigExtensionName ) {
341
247
if (is_object ($ twigExtensionName )) {
0 commit comments