Skip to content

Commit 9c12e5d

Browse files
committedAug 4, 2018
profile and formatter config values
1 parent d589823 commit 9c12e5d

File tree

3 files changed

+102
-29
lines changed

3 files changed

+102
-29
lines changed
 

‎src/config/CodeGeneratorConfig.php

+67-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace gossi\codegen\config;
33

4+
use gossi\code\profiles\Profile;
45
use gossi\codegen\generator\CodeGenerator;
56
use phootwork\lang\Comparator;
67
use Symfony\Component\OptionsResolver\Options;
@@ -15,6 +16,9 @@ class CodeGeneratorConfig {
1516

1617
protected $options;
1718

19+
/** @var Profile */
20+
protected $profile;
21+
1822
/**
1923
* Creates a new configuration for code generator
2024
*
@@ -25,34 +29,62 @@ public function __construct(array $options = []) {
2529
$resolver = new OptionsResolver();
2630
$this->configureOptions($resolver);
2731
$this->options = $resolver->resolve($options);
32+
$this->profile = is_string($this->options['profile']) ? new Profile($this->options['profile']) : $this->options['profile'];
2833
}
29-
34+
3035
protected function configureOptions(OptionsResolver $resolver) {
3136
$resolver->setDefaults([
37+
'profile' => 'default',
3238
'generateDocblock' => true,
3339
'generateEmptyDocblock' => function (Options $options) {
3440
return $options['generateDocblock'];
3541
},
3642
'generateScalarTypeHints' => false,
3743
'generateReturnTypeHints' => false,
44+
'enableFormatting' => false,
3845
'enableSorting' => true,
3946
'useStatementSorting' => CodeGenerator::SORT_USESTATEMENTS_DEFAULT,
4047
'constantSorting' => CodeGenerator::SORT_CONSTANTS_DEFAULT,
4148
'propertySorting' => CodeGenerator::SORT_PROPERTIES_DEFAULT,
4249
'methodSorting' => CodeGenerator::SORT_METHODS_DEFAULT
4350
]);
44-
51+
52+
$resolver->setAllowedTypes('profile', ['string', 'gossi\code\profiles\Profile']);
4553
$resolver->setAllowedTypes('generateDocblock', 'bool');
4654
$resolver->setAllowedTypes('generateEmptyDocblock', 'bool');
4755
$resolver->setAllowedTypes('generateScalarTypeHints', 'bool');
4856
$resolver->setAllowedTypes('generateReturnTypeHints', 'bool');
57+
$resolver->setAllowedTypes('enableFormatting', 'bool');
4958
$resolver->setAllowedTypes('enableSorting', 'bool');
5059
$resolver->setAllowedTypes('useStatementSorting', ['bool', 'string', '\Closure', 'phootwork\lang\Comparator']);
5160
$resolver->setAllowedTypes('constantSorting', ['bool', 'string', '\Closure', 'phootwork\lang\Comparator']);
5261
$resolver->setAllowedTypes('propertySorting', ['bool', 'string', '\Closure', 'phootwork\lang\Comparator']);
5362
$resolver->setAllowedTypes('methodSorting', ['bool', 'string', '\Closure', 'phootwork\lang\Comparator']);
5463
}
5564

65+
/**
66+
* Returns the code style profile
67+
*
68+
* @return Profile
69+
*/
70+
public function getProfile() {
71+
return $this->profile;
72+
}
73+
74+
/**
75+
* Sets the code style profile
76+
*
77+
* @param Profile/string $profile
78+
* @return $this
79+
*/
80+
public function setProfile($profile) {
81+
if (is_string($profile)) {
82+
$profile = new Profile($profile);
83+
}
84+
$this->profile = $profile;
85+
return $this;
86+
}
87+
5688
/**
5789
* Returns whether docblocks should be generated
5890
*
@@ -107,25 +139,34 @@ public function setGenerateEmptyDocblock($generate) {
107139
public function getGenerateScalarTypeHints() {
108140
return $this->options['generateScalarTypeHints'];
109141
}
110-
142+
111143
/**
112144
* Returns whether sorting is enabled
113-
*
145+
*
114146
* @return bool `true` if it is enabled and `false` if not
115147
*/
116148
public function isSortingEnabled() {
117149
return $this->options['enableSorting'];
118150
}
119-
151+
152+
/**
153+
* Returns whether formatting is enalbed
154+
*
155+
* @return bool `true` if it is enabled and `false` if not
156+
*/
157+
public function isFormattingEnabled() {
158+
return $this->options['enableFormatting'];
159+
}
160+
120161
/**
121162
* Returns the use statement sorting
122-
*
163+
*
123164
* @return string|bool|Comparator|\Closure
124165
*/
125166
public function getUseStatementSorting() {
126167
return $this->options['useStatementSorting'];
127168
}
128-
169+
129170
/**
130171
* Returns the constant sorting
131172
*
@@ -134,7 +175,7 @@ public function getUseStatementSorting() {
134175
public function getConstantSorting() {
135176
return $this->options['constantSorting'];
136177
}
137-
178+
138179
/**
139180
* Returns the property sorting
140181
*
@@ -143,7 +184,7 @@ public function getConstantSorting() {
143184
public function getPropertySorting() {
144185
return $this->options['propertySorting'];
145186
}
146-
187+
147188
/**
148189
* Returns the method sorting
149190
*
@@ -183,9 +224,9 @@ public function setGenerateReturnTypeHints($generate) {
183224
$this->options['generateReturnTypeHints'] = $generate;
184225
return $this;
185226
}
186-
227+
187228
/**
188-
* Returns whether sorting is enabled
229+
* Sets whether sorting is enabled
189230
*
190231
* @param $enabled bool `true` if it is enabled and `false` if not
191232
* @return $this
@@ -194,7 +235,18 @@ public function setSortingEnabled($enabled) {
194235
$this->options['enableSorting'] = $enabled;
195236
return $this;
196237
}
197-
238+
239+
/**
240+
* Sets whether formatting is enabled
241+
*
242+
* @param $enabled bool `true` if it is enabled and `false` if not
243+
* @return $this
244+
*/
245+
public function setFormattingEnabled($enabled) {
246+
$this->options['enableFormatting'] = $enabled;
247+
return $this;
248+
}
249+
198250
/**
199251
* Returns the use statement sorting
200252
*
@@ -205,7 +257,7 @@ public function setUseStatementSorting($sorting) {
205257
$this->options['useStatementSorting'] = $sorting;
206258
return $this;
207259
}
208-
260+
209261
/**
210262
* Returns the constant sorting
211263
*
@@ -216,7 +268,7 @@ public function setConstantSorting($sorting) {
216268
$this->options['constantSorting'] = $sorting;
217269
return $this;
218270
}
219-
271+
220272
/**
221273
* Returns the property sorting
222274
*
@@ -227,7 +279,7 @@ public function setPropertySorting($sorting) {
227279
$this->options['propertySorting'] = $sorting;
228280
return $this;
229281
}
230-
282+
231283
/**
232284
* Returns the method sorting
233285
*

‎src/generator/ModelGenerator.php

+21-9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use gossi\codegen\config\CodeGeneratorConfig;
55
use gossi\codegen\generator\utils\Writer;
66
use gossi\codegen\model\AbstractModel;
7+
use gossi\formatter\Formatter;
78

89
/**
910
* Model Generator
@@ -14,10 +15,10 @@ class ModelGenerator {
1415

1516
/** @var Writer */
1617
private $writer;
17-
18+
1819
/** @var BuilderFactory */
1920
private $factory;
20-
21+
2122
/** @var CodeGeneratorConfig */
2223
private $config;
2324

@@ -33,25 +34,30 @@ public function __construct($config = null) {
3334
} else {
3435
$this->config = new CodeGeneratorConfig(['generateDocblock' => false]);
3536
}
36-
37-
$this->writer = new Writer();
37+
38+
$profile = $this->config->getProfile();
39+
$this->writer = new Writer([
40+
'indentation_character' => $profile->getIndentation('character') == 'tab' ? "\t" : ' ',
41+
'indentation_size' => $profile->getIndentation('size')
42+
]);
43+
$this->formatter = new Formatter($profile);
3844
$this->factory = new BuilderFactory($this);
3945
}
40-
46+
4147
/**
4248
* @return CodeGeneratorConfig
4349
*/
4450
public function getConfig() {
4551
return $this->config;
4652
}
47-
53+
4854
/**
4955
* @return Writer
5056
*/
5157
public function getWriter() {
5258
return $this->writer;
5359
}
54-
60+
5561
/**
5662
* @return BuilderFactory
5763
*/
@@ -65,10 +71,16 @@ public function getFactory() {
6571
*/
6672
public function generate(AbstractModel $model) {
6773
$this->writer->reset();
68-
74+
6975
$builder = $this->factory->getBuilder($model);
7076
$builder->build($model);
7177

72-
return $this->writer->getContent();
78+
$code = $this->writer->getContent();
79+
80+
if ($this->config->isFormattingEnabled()) {
81+
$code = $this->formatter->format($code);
82+
}
83+
84+
return $code;
7385
}
7486
}

‎tests/config/ConfigTest.php

+14-5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use gossi\codegen\generator\CodeGenerator;
88
use phootwork\lang\ComparableComparator;
99
use phootwork\lang\Comparator;
10+
use gossi\code\profiles\Profile;
1011

1112
/**
1213
* @group config
@@ -21,6 +22,8 @@ public function testCodeGeneratorConfigDefaults() {
2122
$this->assertFalse($config->getGenerateScalarTypeHints());
2223
$this->assertFalse($config->getGenerateReturnTypeHints());
2324
$this->assertTrue($config->isSortingEnabled());
25+
$this->assertFalse($config->isFormattingEnabled());
26+
$this->assertTrue($config->getProfile() instanceof Profile);
2427
$this->assertEquals(CodeGenerator::SORT_USESTATEMENTS_DEFAULT, $config->getUseStatementSorting());
2528
$this->assertEquals(CodeGenerator::SORT_CONSTANTS_DEFAULT, $config->getConstantSorting());
2629
$this->assertEquals(CodeGenerator::SORT_PROPERTIES_DEFAULT, $config->getPropertySorting());
@@ -39,6 +42,9 @@ public function testCodeGeneratorConfigDisableDocblock() {
3942
public function testCodeGeneratorConfigSetters() {
4043
$config = new CodeGeneratorConfig();
4144

45+
$config->setProfile('psr-2');
46+
$this->assertTrue($config->getProfile() instanceof Profile);
47+
4248
$config->setGenerateDocblock(false);
4349
$this->assertFalse($config->getGenerateDocblock());
4450
$this->assertFalse($config->getGenerateEmptyDocblock());
@@ -56,24 +62,27 @@ public function testCodeGeneratorConfigSetters() {
5662

5763
$config->setGenerateScalarTypeHints(true);
5864
$this->assertTrue($config->getGenerateScalarTypeHints());
59-
65+
6066
$config->setUseStatementSorting(false);
6167
$this->assertFalse($config->getUseStatementSorting());
62-
68+
6369
$config->setConstantSorting('abc');
6470
$this->assertEquals('abc', $config->getConstantSorting());
65-
71+
6672
$config->setPropertySorting(new ComparableComparator());
6773
$this->assertTrue($config->getPropertySorting() instanceof Comparator);
68-
74+
6975
$cmp = function($a, $b) {
7076
return strcmp($a, $b);
7177
};
7278
$config->setMethodSorting($cmp);
7379
$this->assertSame($cmp, $config->getMethodSorting());
74-
80+
7581
$config->setSortingEnabled(false);
7682
$this->assertFalse($config->isSortingEnabled());
83+
84+
$config->setFormattingEnabled(true);
85+
$this->assertTrue($config->isFormattingEnabled());
7786
}
7887

7988
public function testCodeFileGeneratorConfigDefaults() {

0 commit comments

Comments
 (0)
Please sign in to comment.