7
7
use a9f \Fractor \Application \Contract \FileProcessor ;
8
8
use a9f \Fractor \Application \ValueObject \File ;
9
9
use a9f \FractorTypoScript \Contract \TypoScriptFractor ;
10
+ use a9f \FractorTypoScript \Factory \PrettyPrinterConfigurationFactory ;
11
+ use Helmich \TypoScriptParser \Parser \ParseError ;
10
12
use Helmich \TypoScriptParser \Parser \Parser ;
11
13
use Helmich \TypoScriptParser \Parser \Printer \PrettyPrinter ;
12
- use Helmich \TypoScriptParser \Parser \ Printer \ PrettyPrinterConfiguration ;
14
+ use Helmich \TypoScriptParser \Tokenizer \ TokenizerException ;
13
15
use Symfony \Component \Console \Output \BufferedOutput ;
14
16
15
17
/**
16
18
* @implements FileProcessor<TypoScriptFractor>
17
19
*/
18
20
final readonly class TypoScriptFileProcessor implements FileProcessor
19
21
{
22
+ private BufferedOutput $ output ;
23
+
20
24
/**
21
25
* @param iterable<TypoScriptFractor> $rules
22
26
*/
23
27
public function __construct (
24
28
private iterable $ rules ,
25
29
private Parser $ parser ,
26
- private PrettyPrinter $ printer
30
+ private PrettyPrinter $ printer ,
31
+ private PrettyPrinterConfigurationFactory $ prettyPrinterConfigurationFactory
27
32
) {
33
+ $ this ->output = new BufferedOutput ();
28
34
}
29
35
30
36
public function canHandle (File $ file ): bool
@@ -34,15 +40,24 @@ public function canHandle(File $file): bool
34
40
35
41
public function handle (File $ file , iterable $ appliedRules ): void
36
42
{
37
- $ statements = $ this ->parser ->parseString ($ file ->getContent ());
43
+ try {
44
+ $ statements = $ this ->parser ->parseString ($ file ->getContent ());
45
+
46
+ $ statementsIterator = new TypoScriptStatementsIterator ($ this ->rules );
47
+ $ statements = $ statementsIterator ->traverseDocument ($ file , $ statements );
38
48
39
- $ statementsIterator = new TypoScriptStatementsIterator ($ this ->rules );
40
- $ statements = $ statementsIterator ->traverseDocument ($ file , $ statements );
49
+ $ this ->printer ->setPrettyPrinterConfiguration (
50
+ $ this ->prettyPrinterConfigurationFactory ->createPrettyPrinterConfiguration ($ file )
51
+ );
52
+ $ this ->printer ->printStatements ($ statements , $ this ->output );
41
53
42
- $ output = new BufferedOutput ();
43
- $ this ->printer ->setPrettyPrinterConfiguration (PrettyPrinterConfiguration::create () ->withEmptyLineBreaks ());
44
- $ this ->printer ->printStatements ($ statements , $ output );
45
- $ file ->changeFileContent ($ output ->fetch ());
54
+ $ newTypoScriptContent = $ this ->output ->fetch ();
55
+ $ typoScriptContent = rtrim ($ newTypoScriptContent ) . "\n" ;
56
+ $ file ->changeFileContent ($ typoScriptContent );
57
+ } catch (TokenizerException ) {
58
+ return ;
59
+ } catch (ParseError ) {
60
+ }
46
61
}
47
62
48
63
public function allowedFileExtensions (): array
0 commit comments