Skip to content

Commit

Permalink
Merge pull request #213 from andreaswolf/issue-197
Browse files Browse the repository at this point in the history
[FEATURE] Keep TypoScript format
  • Loading branch information
andreaswolf authored Aug 29, 2024
2 parents 2ef9813 + 8aa5260 commit 0444960
Show file tree
Hide file tree
Showing 16 changed files with 235 additions and 18 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,26 @@ return FractorConfiguration::configure()
])
```

If you want to adjust the format of your typoscript files, you can configure it this way:

```php
<?php

use a9f\Fractor\Configuration\FractorConfiguration;
use a9f\FractorTypoScript\Configuration\TypoScriptProcessorOption;
use Helmich\TypoScriptParser\Parser\Printer\PrettyPrinterConfiguration;

return FractorConfiguration::configure()
->withOptions([
TypoScriptProcessorOption::INDENT_SIZE => 2,
TypoScriptProcessorOption::INDENT_CHARACTER => PrettyPrinterConfiguration::INDENTATION_STYLE_SPACES,
//TypoScriptProcessorOption::INDENT_CHARACTER => 'auto', // this detects the indent from the file and keeps it
TypoScriptProcessorOption::ADD_CLOSING_GLOBAL => false,
TypoScriptProcessorOption::INCLUDE_EMPTY_LINE_BREAKS => true,
TypoScriptProcessorOption::INDENT_CONDITIONS => true,
])
```

## Processing

Execute Fractor from the command line, passing the path to your configuration file as an argument:
Expand Down
5 changes: 5 additions & 0 deletions packages/fractor-typoscript/config/application.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use a9f\FractorTypoScript\Contract\TypoScriptFractor;
use a9f\FractorTypoScript\TypoScriptFileProcessor;
use a9f\FractorTypoScript\ValueObject\TypoScriptPrettyPrinterFormatConfiguration;
use Helmich\TypoScriptParser\Parser\Parser;
use Helmich\TypoScriptParser\Parser\Printer\PrettyPrinter;
use Helmich\TypoScriptParser\Tokenizer\Tokenizer;
Expand All @@ -26,7 +27,11 @@
->public();
$services->set(PrettyPrinter::class);

$services->set('fractor.typoscript_processor.pretty_printer', TypoScriptPrettyPrinterFormatConfiguration::class)
->factory([null, 'createFromParameterBag']);

$services->set(TypoScriptFileProcessor::class)
->arg('$typoScriptPrettyPrinterFormatConfiguration', service('fractor.typoscript_processor.pretty_printer'))
->arg('$rules', tagged_iterator('fractor.typoscript_rule'));

$containerBuilder->registerForAutoconfiguration(TypoScriptFractor::class)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace a9f\FractorTypoScript\Configuration;

final class TypoScriptProcessorOption
{
public const INDENT_SIZE = 'typoscript-processor-indent-size';

public const INDENT_CHARACTER = 'typoscript-processor-indent-character';

public const ADD_CLOSING_GLOBAL = 'typoscript-processor-add-closing-global';

public const INCLUDE_EMPTY_LINE_BREAKS = 'typoscript-processor-include-empty-line-breaks';

public const INDENT_CONDITIONS = 'typoscript-processor-indent-conditions';
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,46 @@

use a9f\Fractor\Application\ValueObject\File;
use a9f\Fractor\ValueObject\Indent;
use a9f\FractorTypoScript\ValueObject\TypoScriptPrettyPrinterFormatConfiguration;
use Helmich\TypoScriptParser\Parser\Printer\PrettyPrinterConfiguration;

final class PrettyPrinterConfigurationFactory
{
public function createPrettyPrinterConfiguration(File $file): PrettyPrinterConfiguration
{
// keep original TypoScript format
$indent = Indent::fromFile($file);

public function createPrettyPrinterConfiguration(
File $file,
TypoScriptPrettyPrinterFormatConfiguration $prettyPrinterFormatConfiguration
): PrettyPrinterConfiguration {
$prettyPrinterConfiguration = PrettyPrinterConfiguration::create();

if ($indent->isSpace()) {
$prettyPrinterConfiguration = $prettyPrinterConfiguration->withSpaceIndentation($indent->length());
} else {
if ($prettyPrinterFormatConfiguration->style === 'auto') {
// keep original TypoScript format
$indent = Indent::fromFile($file);

if ($indent->isSpace()) {
$prettyPrinterConfiguration = $prettyPrinterConfiguration->withSpaceIndentation($indent->length());
} else {
$prettyPrinterConfiguration = $prettyPrinterConfiguration->withTabs();
}
} elseif ($prettyPrinterFormatConfiguration->style === PrettyPrinterConfiguration::INDENTATION_STYLE_TABS) {
$prettyPrinterConfiguration = $prettyPrinterConfiguration->withTabs();
} else {
$prettyPrinterConfiguration = $prettyPrinterConfiguration->withSpaceIndentation(
$prettyPrinterFormatConfiguration->size
);
}

if ($prettyPrinterFormatConfiguration->indentConditions) {
$prettyPrinterConfiguration = $prettyPrinterConfiguration->withIndentConditions();
}

if ($prettyPrinterFormatConfiguration->addClosingGlobal) {
$prettyPrinterConfiguration = $prettyPrinterConfiguration->withClosingGlobalStatement();
}

if ($prettyPrinterFormatConfiguration->includeEmptyLineBreaks) {
return $prettyPrinterConfiguration->withEmptyLineBreaks();
}

return $prettyPrinterConfiguration->withEmptyLineBreaks();
return $prettyPrinterConfiguration;
}
}
9 changes: 7 additions & 2 deletions packages/fractor-typoscript/src/TypoScriptFileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use a9f\Fractor\Application\ValueObject\File;
use a9f\FractorTypoScript\Contract\TypoScriptFractor;
use a9f\FractorTypoScript\Factory\PrettyPrinterConfigurationFactory;
use a9f\FractorTypoScript\ValueObject\TypoScriptPrettyPrinterFormatConfiguration;
use Helmich\TypoScriptParser\Parser\ParseError;
use Helmich\TypoScriptParser\Parser\Parser;
use Helmich\TypoScriptParser\Parser\Printer\PrettyPrinter;
Expand All @@ -28,7 +29,8 @@ public function __construct(
private iterable $rules,
private Parser $parser,
private PrettyPrinter $printer,
private PrettyPrinterConfigurationFactory $prettyPrinterConfigurationFactory
private PrettyPrinterConfigurationFactory $prettyPrinterConfigurationFactory,
private TypoScriptPrettyPrinterFormatConfiguration $typoScriptPrettyPrinterFormatConfiguration
) {
$this->output = new BufferedOutput();
}
Expand All @@ -47,7 +49,10 @@ public function handle(File $file, iterable $appliedRules): void
$statements = $statementsIterator->traverseDocument($file, $statements);

$this->printer->setPrettyPrinterConfiguration(
$this->prettyPrinterConfigurationFactory->createPrettyPrinterConfiguration($file)
$this->prettyPrinterConfigurationFactory->createPrettyPrinterConfiguration(
$file,
$this->typoScriptPrettyPrinterFormatConfiguration
)
);
$this->printer->printStatements($statements, $this->output);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace a9f\FractorTypoScript\ValueObject;

use a9f\FractorTypoScript\Configuration\TypoScriptProcessorOption;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;

final readonly class TypoScriptPrettyPrinterFormatConfiguration
{
public function __construct(
public int $size,
public string $style,
public bool $addClosingGlobal,
public bool $includeEmptyLineBreaks,
public bool $indentConditions
) {
}

public static function createFromParameterBag(ParameterBagInterface $parameterBag): self
{
$size = $parameterBag->has(TypoScriptProcessorOption::INDENT_SIZE)
? $parameterBag->get(TypoScriptProcessorOption::INDENT_SIZE)
: 4;
$size = is_int($size) ? $size : 4;

$style = $parameterBag->has(TypoScriptProcessorOption::INDENT_CHARACTER)
? $parameterBag->get(TypoScriptProcessorOption::INDENT_CHARACTER)
: 'auto';
$style = is_string($style) ? $style : 'auto';

$addClosingGlobal = $parameterBag->has(TypoScriptProcessorOption::ADD_CLOSING_GLOBAL)
? (bool) $parameterBag->get(TypoScriptProcessorOption::ADD_CLOSING_GLOBAL)
: true;

$includeEmptyLineBreaks = $parameterBag->has(TypoScriptProcessorOption::INCLUDE_EMPTY_LINE_BREAKS)
? (bool) $parameterBag->get(TypoScriptProcessorOption::INCLUDE_EMPTY_LINE_BREAKS)
: true;

$indentConditions = $parameterBag->has(TypoScriptProcessorOption::INDENT_CONDITIONS)
&& (bool) $parameterBag->get(TypoScriptProcessorOption::INDENT_CONDITIONS);

return new self($size, $style, $addClosingGlobal, $includeEmptyLineBreaks, $indentConditions);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace a9f\FractorTypoScript\Tests\Fixtures;

use a9f\FractorTypoScript\AbstractTypoScriptFractor;
use a9f\FractorTypoScript\TypoScriptStatementsIterator;
use Helmich\TypoScriptParser\Parser\AST\Operator\Assignment;
use Helmich\TypoScriptParser\Parser\AST\Statement;
use Symfony\Component\DependencyInjection\Exception\BadMethodCallException;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

final class DummyTypoScriptFractorRule extends AbstractTypoScriptFractor
{
public function refactor(Statement $statement): null|Statement|int
{
if (! $statement instanceof Assignment) {
return null;
}

if ($statement->object->absoluteName !== 'config.spamProtectEmailAddresses'
|| $statement->value->value !== 'ascii'
) {
return null;
}

return TypoScriptStatementsIterator::REMOVE_NODE;
}

public function getRuleDefinition(): RuleDefinition
{
throw new BadMethodCallException('Not implemented yet');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
page = PAGE
page.10 = TEXT
page.10.value = german

[siteLanguage('languageId') == 1]
page.10.value = english
[global]

config.spamProtectEmailAddresses = ascii
-----
page = PAGE
page.10 = TEXT
page.10.value = german

[siteLanguage('languageId') == 1]
page.10.value = english
[global]
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace a9f\FractorTypoScript\Tests\TypoScriptFileProcessor;

use a9f\Fractor\Testing\PHPUnit\AbstractFractorTestCase;
use a9f\FractorTypoScript\Tests\Fixtures\DummyTypoScriptFractorRule;
use PHPUnit\Framework\Attributes\DataProvider;

class TypoScriptFileProcessorTest extends AbstractFractorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
$this->assertThatRuleIsApplied($filePath, DummyTypoScriptFractorRule::class);
}

public static function provideData(): \Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixtures', '*.typoscript');
}

public function provideConfigFilePath(): ?string
{
return __DIR__ . '/config/fractor.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

use a9f\Fractor\Configuration\FractorConfiguration;
use a9f\FractorTypoScript\Configuration\TypoScriptProcessorOption;
use a9f\FractorTypoScript\Tests\Fixtures\DummyTypoScriptFractorRule;
use Helmich\TypoScriptParser\Parser\Printer\PrettyPrinterConfiguration;

return FractorConfiguration::configure()
->withOptions([
TypoScriptProcessorOption::INDENT_SIZE => 4,
TypoScriptProcessorOption::INDENT_CHARACTER => PrettyPrinterConfiguration::INDENTATION_STYLE_SPACES,
TypoScriptProcessorOption::ADD_CLOSING_GLOBAL => false,
TypoScriptProcessorOption::INCLUDE_EMPTY_LINE_BREAKS => true,
TypoScriptProcessorOption::INDENT_CONDITIONS => true,
])
->withRules([DummyTypoScriptFractorRule::class]);
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace a9f\FractorTypoScript\Tests\Fixture;
namespace a9f\FractorTypoScript\Tests\TypoScriptStatementsIterator\Fixture;

use a9f\Fractor\Application\ValueObject\File;
use a9f\FractorTypoScript\Contract\TypoScriptNodeVisitor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

declare(strict_types=1);

namespace a9f\FractorTypoScript\Tests;
namespace a9f\FractorTypoScript\Tests\TypoScriptStatementsIterator;

use a9f\Fractor\Application\ValueObject\File;
use a9f\Fractor\DependencyInjection\ContainerContainerBuilder;
use a9f\Fractor\Exception\ShouldNotHappenException;
use a9f\FractorTypoScript\Tests\Fixture\StatementCollectingVisitor;
use a9f\FractorTypoScript\Tests\TypoScriptStatementsIterator\Fixture\StatementCollectingVisitor;
use a9f\FractorTypoScript\TypoScriptStatementsIterator;
use Helmich\TypoScriptParser\Parser\Parser;
use PHPUnit\Framework\Attributes\Test;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator, ContainerBuilder $containerBuilder): void {
$containerConfigurator->import(__DIR__ . '/../../config/application.php');
$containerConfigurator->import(__DIR__ . '/../../../config/application.php');
};
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ final class FractorConfigurationBuilder
private array $imports = [];

/**
* @var array<string, int|string>
* @var array<string, string|int|bool>
*/
private array $options = [];

Expand Down Expand Up @@ -157,7 +157,7 @@ public function import(string $import): self
}

/**
* @param array<string, string|int> $options
* @param array<string, string|int|bool> $options
*/
public function withOptions(array $options): self
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public function refactor(Statement $statement): null|Statement|int
}

if ($statement->object->absoluteName !== 'config.spamProtectEmailAddresses'
|| $statement->value->value !== 'ascii') {
|| $statement->value->value !== 'ascii'
) {
return null;
}

Expand Down

0 comments on commit 0444960

Please sign in to comment.