Skip to content

Commit

Permalink
[FEATURE] Keep TypoScript format
Browse files Browse the repository at this point in the history
Resolves: #197
  • Loading branch information
simonschaufi committed Aug 28, 2024
1 parent 2ef9813 commit 36be90a
Show file tree
Hide file tree
Showing 17 changed files with 294 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
6 changes: 6 additions & 0 deletions packages/fractor-typoscript/config/application.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
declare(strict_types=1);

use a9f\FractorTypoScript\Contract\TypoScriptFractor;
use a9f\FractorTypoScript\Factory\PrettyPrinterFormatFactory;
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 +28,11 @@
->public();
$services->set(PrettyPrinter::class);

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

$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->getStyle() === 'auto') {
// keep original TypoScript format
$indent = Indent::fromFile($file);

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

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

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

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

return $prettyPrinterConfiguration->withEmptyLineBreaks();
return $prettyPrinterConfiguration;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace a9f\FractorTypoScript\Factory;

use a9f\FractorTypoScript\Configuration\TypoScriptProcessorOption;
use a9f\FractorTypoScript\ValueObject\TypoScriptPrettyPrinterFormatConfiguration;
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;

final readonly class PrettyPrinterFormatFactory
{
public function __construct(
private ContainerBagInterface $parameterBag
) {
}

public function create(): TypoScriptPrettyPrinterFormatConfiguration
{
$size = $this->parameterBag->has(TypoScriptProcessorOption::INDENT_SIZE)
? $this->parameterBag->get(TypoScriptProcessorOption::INDENT_SIZE)
: 4;

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

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

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

$indentConditions = $this->parameterBag->has(TypoScriptProcessorOption::INDENT_CONDITIONS)
? $this->parameterBag->get(TypoScriptProcessorOption::INDENT_CONDITIONS)
: false;

return TypoScriptPrettyPrinterFormatConfiguration::fromValues(
$size,
$style,
$addClosingGlobal,
$includeEmptyLineBreaks,
$indentConditions
);
}
}
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,55 @@
<?php

declare(strict_types=1);

namespace a9f\FractorTypoScript\ValueObject;

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

/**
* @phpstan-param \Helmich\TypoScriptParser\Parser\Printer\PrettyPrinterConfiguration::INDENTATION_STYLE_* $style
*/
public static function fromValues(
int $size,
string $style,
bool $addClosingGlobal,
bool $includeEmptyLineBreaks,
bool $indentConditions
): self {
return new self($size, $style, $addClosingGlobal, $includeEmptyLineBreaks, $indentConditions);
}

public function getSize(): int
{
return $this->size;
}

public function getStyle(): string
{
return $this->style;
}

public function shouldAddClosingGlobal(): bool
{
return $this->addClosingGlobal;
}

public function shouldIncludeEmptyLineBreaks(): bool
{
return $this->includeEmptyLineBreaks;
}

public function shouldIndentConditions(): bool
{
return $this->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,19 @@
<?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::INDENT_CHARACTER => 'auto',
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');
};
Loading

0 comments on commit 36be90a

Please sign in to comment.