Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Add TranslationFileFractor for Yaml #78

Merged
merged 1 commit into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions typo3-fractor/rules/TYPO3v10/Yaml/TranslationFileFractor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

declare(strict_types=1);

namespace a9f\Typo3Fractor\TYPO3v10\Yaml;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this have a namespace like …\Typo3Fractor\Rules\…?


use a9f\FractorYaml\Contract\YamlFractorRule;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

final class TranslationFileFractor implements YamlFractorRule
{
/**
* @var string
*/
private const TRANSLATION_FILE_KEY = 'translationFile';

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Use key translationFiles instead of translationFile', [
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO this should have some reference to EXT:form in it, e.g. "Rewrite EXT:form configuration files to use key "translationFiles" instead of "translationFile" in renderingOptions."

new CodeSample(
<<<'CODE_SAMPLE'
TYPO3:
CMS:
Form:
prototypes:
standard:
formElementsDefinition:
Form:
renderingOptions:
translation:
translationFile:
10: 'EXT:form/Resources/Private/Language/locallang.xlf'
20: 'EXT:myextension/Resources/Private/Language/locallang.xlf'
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
TYPO3:
CMS:
Form:
prototypes:
standard:
formElementsDefinition:
Form:
renderingOptions:
translation:
translationFiles:
20: 'EXT:myextension/Resources/Private/Language/locallang.xlf'
CODE_SAMPLE
),
]);
}

public function refactor(array $yaml): array
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should have a common understanding of the return values of these methods in all different file types. IMO, there's three possible return types:

  • a changed (part of) the file structure
  • null = no change
  • some flags to signal "throw this away", "stop modifying it" etc.

{
return $this->refactorTranslationFile($yaml);
}

/**
* @param mixed[] $yaml
* @return mixed[]
* @see https://github.com/TYPO3/typo3/blob/10.4/typo3/sysext/form/Classes/Controller/FormEditorController.php#L653-L689
*/
private function refactorTranslationFile(array &$yaml): array
{
foreach ($yaml as &$section) {
if (! is_array($section)) {
continue;
}

if (array_key_exists(self::TRANSLATION_FILE_KEY, $section)
&& is_array($section[self::TRANSLATION_FILE_KEY])
) {
$section['translationFiles'] = $this->buildNewTranslations($section[self::TRANSLATION_FILE_KEY]);
unset($section[self::TRANSLATION_FILE_KEY]);
}

$this->refactorTranslationFile($section);
}

unset($section);

return $yaml;
}

/**
* @param array<int, string> $oldTranslations
*
* @return array<int, string>
*/
private function buildNewTranslations(array $oldTranslations): array
{
return array_filter(
$oldTranslations,
static fn ($oldTranslationFile): bool => ! \str_starts_with((string) $oldTranslationFile, 'EXT:form')
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
TYPO3:
CMS:
Form:
prototypes:
standard:
formElementsDefinition:
Form:
renderingOptions:
translation:
translationFile:
10: 'EXT:form/Resources/Private/Language/locallang.xlf'
20: 'EXT:myextension/Resources/Private/Language/locallang.xlf'
-----
TYPO3:
CMS:
Form:
prototypes:
standard:
formElementsDefinition:
Form:
renderingOptions:
translation:
translationFiles:
20: 'EXT:myextension/Resources/Private/Language/locallang.xlf'
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Rules\TYPO3v10\Yaml\TranslationFileFractor;

use a9f\Fractor\Testing\PHPUnit\AbstractFractorTestCase;
use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;

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

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

public function provideConfigFilePath(): ?string
{
return __DIR__ . '/config/fractor.php';
}

protected function additionalConfigurationFiles(): array
{
return [__DIR__ . '/config/config.php'];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
$containerConfigurator->import(__DIR__ . '/../../../../../../config/application.php');
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

use a9f\Fractor\Configuration\FractorConfiguration;
use a9f\Typo3Fractor\TYPO3v10\Yaml\TranslationFileFractor;

return FractorConfiguration::configure()
->withRules([TranslationFileFractor::class]);