Skip to content

Commit 0183697

Browse files
sabbelasichonsimonschaufi
authored andcommitted
[FEATURE] Make indentation configurable
1 parent ac07a5e commit 0183697

File tree

8 files changed

+163
-73
lines changed

8 files changed

+163
-73
lines changed

README.md

+19
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,25 @@ return FractorConfiguration::configure()
8080
]);
8181
```
8282

83+
### Configure code style
84+
85+
Fractor tries to format the code as good as possible.
86+
If you want to adjust the indentation of your xml files, you can configure it this way:
87+
88+
```php
89+
<?php
90+
91+
use a9f\Fractor\Configuration\FractorConfiguration;
92+
use a9f\Fractor\ValueObject\Indent;
93+
use a9f\FractorXml\Configuration\XmlProcessorOption;
94+
95+
return FractorConfiguration::configure()
96+
->withOptions([
97+
XmlProcessorOption::INDENT_CHARACTER => Indent::STYLE_TAB,
98+
XmlProcessorOption::INDENT_SIZE => 1,
99+
])
100+
```
101+
83102
## Processing
84103

85104
Execute Fractor from the command line, passing the path to your configuration file as an argument:

packages/fractor-xml/config/application.php

+7
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22

33
declare(strict_types=1);
44

5+
use a9f\Fractor\ValueObject\Indent;
56
use a9f\FractorXml\Contract\Formatter;
67
use a9f\FractorXml\Contract\XmlFractor;
8+
use a9f\FractorXml\IndentFactory;
79
use a9f\FractorXml\PrettyXmlFormatter;
810
use a9f\FractorXml\XmlFileProcessor;
911
use Symfony\Component\DependencyInjection\ContainerBuilder;
1012
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
13+
use function Symfony\Component\DependencyInjection\Loader\Configurator\service;
1114
use function Symfony\Component\DependencyInjection\Loader\Configurator\tagged_iterator;
1215

1316
return static function (ContainerConfigurator $containerConfigurator, ContainerBuilder $containerBuilder): void {
@@ -18,7 +21,11 @@
1821

1922
$services->load('a9f\\FractorXml\\', __DIR__ . '/../src/');
2023

24+
$services->set('fractor.xml_processor.indent', Indent::class)
25+
->factory([service(IndentFactory::class), 'create']);
26+
2127
$services->set(XmlFileProcessor::class)
28+
->arg('$indent', service('fractor.xml_processor.indent'))
2229
->arg('$rules', tagged_iterator('fractor.xml_rule'));
2330

2431
$services->set(\PrettyXml\Formatter::class);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace a9f\FractorXml\Configuration;
6+
7+
final class XmlProcessorOption
8+
{
9+
public const INDENT_SIZE = 'xml-processor-indent-size';
10+
11+
public const INDENT_CHARACTER = 'xml-processor-indent-character';
12+
}
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace a9f\FractorXml;
6+
7+
use a9f\Fractor\ValueObject\Indent;
8+
use a9f\FractorXml\Configuration\XmlProcessorOption;
9+
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
10+
11+
final readonly class IndentFactory
12+
{
13+
public function __construct(
14+
private ContainerBagInterface $parameterBag
15+
) {
16+
}
17+
18+
public function create(): Indent
19+
{
20+
$size = $this->parameterBag->has(XmlProcessorOption::INDENT_SIZE) ? $this->parameterBag->get(
21+
XmlProcessorOption::INDENT_SIZE
22+
) : 4;
23+
$style = $this->parameterBag->has(XmlProcessorOption::INDENT_CHARACTER) ? $this->parameterBag->get(
24+
XmlProcessorOption::INDENT_CHARACTER
25+
) : Indent::STYLE_SPACE;
26+
27+
return Indent::fromSizeAndStyle($size, $style);
28+
}
29+
}

packages/fractor-xml/src/XmlFileProcessor.php

+3-4
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
public function __construct(
2424
private DomDocumentFactory $domDocumentFactory,
2525
private Formatter $formatter,
26-
private iterable $rules
26+
private iterable $rules,
27+
private Indent $indent
2728
) {
2829
}
2930

@@ -45,9 +46,7 @@ public function handle(File $file, iterable $appliedRules): void
4546
$iterator->traverseDocument($file, $document);
4647

4748
$newXml = $this->saveXml($document);
48-
49-
// TODO make the indentation configurable in fractor config
50-
$newXml = $this->formatter->format(Indent::fromSizeAndStyle(4, Indent::STYLE_SPACE), $newXml);
49+
$newXml = $this->formatter->format($this->indent, $newXml);
5150

5251
if ($newXml === $originalXml) {
5352
return;

packages/fractor/src/Configuration/FractorConfigurationBuilder.php

+19
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ final class FractorConfigurationBuilder
4141
*/
4242
private array $imports = [];
4343

44+
/**
45+
* @var array<string, int|string>
46+
*/
47+
private array $options = [];
48+
4449
public function __invoke(ContainerConfigurator $containerConfigurator): void
4550
{
4651
Assert::allString($this->paths);
@@ -49,6 +54,10 @@ public function __invoke(ContainerConfigurator $containerConfigurator): void
4954
$parameters->set(Option::PATHS, $this->paths);
5055
$parameters->set(Option::SKIP, $this->skip);
5156

57+
foreach ($this->options as $optionName => $optionValue) {
58+
$parameters->set($optionName, $optionValue);
59+
}
60+
5261
$services = $containerConfigurator->services();
5362

5463
foreach ($this->rules as $rule) {
@@ -146,4 +155,14 @@ public function import(string $import): self
146155

147156
return $this;
148157
}
158+
159+
/**
160+
* @param array<string, string|int> $options
161+
*/
162+
public function withOptions(array $options): self
163+
{
164+
$this->options = $options;
165+
166+
return $this;
167+
}
149168
}
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,75 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<T3DataStructure>
3-
<sheets>
4-
<sDEF>
5-
<ROOT>
6-
<sheetTitle>sheetTitle</sheetTitle>
7-
<type>array</type>
8-
<el>
9-
<aColumn>
10-
<config>
11-
<type>input</type>
12-
<eval>email,trim,unique</eval>
13-
<required>1</required>
14-
</config>
15-
</aColumn>
16-
<differentColumn>
17-
<config>
18-
<type>input</type>
19-
<eval>trim,unique</eval>
20-
</config>
21-
</differentColumn>
22-
<wrongTypeColumn>
23-
<config>
24-
<type>text</type>
25-
<eval>email,trim,unique</eval>
26-
</config>
27-
</wrongTypeColumn>
28-
<alreadyMigratedColumn>
29-
<config>
30-
<type>email</type>
31-
</config>
32-
</alreadyMigratedColumn>
33-
</el>
34-
</ROOT>
35-
</sDEF>
36-
</sheets>
3+
<sheets>
4+
<sDEF>
5+
<ROOT>
6+
<sheetTitle>sheetTitle</sheetTitle>
7+
<type>array</type>
8+
<el>
9+
<aColumn>
10+
<config>
11+
<type>input</type>
12+
<eval>email,trim,unique</eval>
13+
<required>1</required>
14+
</config>
15+
</aColumn>
16+
<differentColumn>
17+
<config>
18+
<type>input</type>
19+
<eval>trim,unique</eval>
20+
</config>
21+
</differentColumn>
22+
<wrongTypeColumn>
23+
<config>
24+
<type>text</type>
25+
<eval>email,trim,unique</eval>
26+
</config>
27+
</wrongTypeColumn>
28+
<alreadyMigratedColumn>
29+
<config>
30+
<type>email</type>
31+
</config>
32+
</alreadyMigratedColumn>
33+
</el>
34+
</ROOT>
35+
</sDEF>
36+
</sheets>
3737
</T3DataStructure>
3838
-----
3939
<?xml version="1.0" encoding="UTF-8"?>
4040
<T3DataStructure>
41-
<sheets>
42-
<sDEF>
43-
<ROOT>
44-
<sheetTitle>sheetTitle</sheetTitle>
45-
<type>array</type>
46-
<el>
47-
<aColumn>
48-
<config>
49-
<type>email</type>
50-
<eval>unique</eval>
51-
<required>1</required>
52-
</config>
53-
</aColumn>
54-
<differentColumn>
55-
<config>
56-
<type>input</type>
57-
<eval>trim,unique</eval>
58-
</config>
59-
</differentColumn>
60-
<wrongTypeColumn>
61-
<config>
62-
<type>text</type>
63-
<eval>email,trim,unique</eval>
64-
</config>
65-
</wrongTypeColumn>
66-
<alreadyMigratedColumn>
67-
<config>
68-
<type>email</type>
69-
</config>
70-
</alreadyMigratedColumn>
71-
</el>
72-
</ROOT>
73-
</sDEF>
74-
</sheets>
41+
<sheets>
42+
<sDEF>
43+
<ROOT>
44+
<sheetTitle>sheetTitle</sheetTitle>
45+
<type>array</type>
46+
<el>
47+
<aColumn>
48+
<config>
49+
<type>email</type>
50+
<eval>unique</eval>
51+
<required>1</required>
52+
</config>
53+
</aColumn>
54+
<differentColumn>
55+
<config>
56+
<type>input</type>
57+
<eval>trim,unique</eval>
58+
</config>
59+
</differentColumn>
60+
<wrongTypeColumn>
61+
<config>
62+
<type>text</type>
63+
<eval>email,trim,unique</eval>
64+
</config>
65+
</wrongTypeColumn>
66+
<alreadyMigratedColumn>
67+
<config>
68+
<type>email</type>
69+
</config>
70+
</alreadyMigratedColumn>
71+
</el>
72+
</ROOT>
73+
</sDEF>
74+
</sheets>
7575
</T3DataStructure>

packages/typo3-fractor/tests/Rules/TYPO3v12/FlexForm/MigrateEmailFlagToEmailTypeFlexFormFractor/config/fractor.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@
33
declare(strict_types=1);
44

55
use a9f\Fractor\Configuration\FractorConfiguration;
6+
use a9f\Fractor\ValueObject\Indent;
7+
use a9f\FractorXml\Configuration\XmlProcessorOption;
68
use a9f\Typo3Fractor\TYPO3v12\FlexForm\MigrateEmailFlagToEmailTypeFlexFormFractor;
79

810
return FractorConfiguration::configure()
9-
->import(__DIR__ . '/../../../../../../config/application.php')
11+
->withOptions([
12+
XmlProcessorOption::INDENT_CHARACTER => Indent::STYLE_TAB,
13+
XmlProcessorOption::INDENT_SIZE => 1,
14+
])
1015
->withRules([MigrateEmailFlagToEmailTypeFlexFormFractor::class]);

0 commit comments

Comments
 (0)