Skip to content

Commit 7d8e499

Browse files
committed
[TASK] Improve TypoScript processor
Resolves: #196
1 parent cdcf7e3 commit 7d8e499

File tree

2 files changed

+52
-9
lines changed

2 files changed

+52
-9
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace a9f\FractorTypoScript\Factory;
6+
7+
use a9f\Fractor\Application\ValueObject\File;
8+
use a9f\Fractor\ValueObject\Indent;
9+
use Helmich\TypoScriptParser\Parser\Printer\PrettyPrinterConfiguration;
10+
11+
final class PrettyPrinterConfigurationFactory
12+
{
13+
public function createPrettyPrinterConfiguration(File $file): PrettyPrinterConfiguration
14+
{
15+
// keep original TypoScript format
16+
$indent = Indent::fromFile($file);
17+
18+
$prettyPrinterConfiguration = PrettyPrinterConfiguration::create();
19+
20+
if ($indent->isSpace()) {
21+
$prettyPrinterConfiguration = $prettyPrinterConfiguration->withSpaceIndentation($indent->length());
22+
} else {
23+
$prettyPrinterConfiguration = $prettyPrinterConfiguration->withTabs();
24+
}
25+
26+
return $prettyPrinterConfiguration->withEmptyLineBreaks();
27+
}
28+
}

packages/fractor-typoscript/src/TypoScriptFileProcessor.php

+24-9
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,30 @@
77
use a9f\Fractor\Application\Contract\FileProcessor;
88
use a9f\Fractor\Application\ValueObject\File;
99
use a9f\FractorTypoScript\Contract\TypoScriptFractor;
10+
use a9f\FractorTypoScript\Factory\PrettyPrinterConfigurationFactory;
11+
use Helmich\TypoScriptParser\Parser\ParseError;
1012
use Helmich\TypoScriptParser\Parser\Parser;
1113
use Helmich\TypoScriptParser\Parser\Printer\PrettyPrinter;
12-
use Helmich\TypoScriptParser\Parser\Printer\PrettyPrinterConfiguration;
14+
use Helmich\TypoScriptParser\Tokenizer\TokenizerException;
1315
use Symfony\Component\Console\Output\BufferedOutput;
1416

1517
/**
1618
* @implements FileProcessor<TypoScriptFractor>
1719
*/
1820
final readonly class TypoScriptFileProcessor implements FileProcessor
1921
{
22+
private BufferedOutput $output;
23+
2024
/**
2125
* @param iterable<TypoScriptFractor> $rules
2226
*/
2327
public function __construct(
2428
private iterable $rules,
2529
private Parser $parser,
26-
private PrettyPrinter $printer
30+
private PrettyPrinter $printer,
31+
private PrettyPrinterConfigurationFactory $prettyPrinterConfigurationFactory
2732
) {
33+
$this->output = new BufferedOutput();
2834
}
2935

3036
public function canHandle(File $file): bool
@@ -34,15 +40,24 @@ public function canHandle(File $file): bool
3440

3541
public function handle(File $file, iterable $appliedRules): void
3642
{
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);
3848

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);
4153

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+
}
4661
}
4762

4863
public function allowedFileExtensions(): array

0 commit comments

Comments
 (0)