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 fractor-fluid and fractor-yaml to typo3-fractor #69

Merged
merged 1 commit into from
May 13, 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
2 changes: 2 additions & 0 deletions e2e/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
"a9f/fractor": "@dev",
"a9f/fractor-doc-generator": "@dev",
"a9f/fractor-extension-installer": "@dev",
"a9f/fractor-fluid": "@dev",
"a9f/fractor-xml": "@dev",
"a9f/fractor-yaml": "@dev",
"a9f/typo3-fractor": "@dev"
},
"require-dev": {
Expand Down
8 changes: 1 addition & 7 deletions fractor/src/Testing/PHPUnit/AbstractFractorTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use a9f\Fractor\Testing\Fixture\FixtureFileFinder;
use a9f\Fractor\Testing\Fixture\FixtureSplitter;
use Nette\Utils\FileSystem;
use Nette\Utils\Strings;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;

Expand Down Expand Up @@ -154,12 +153,7 @@ private function createInputFilePath(string $fixtureFilePath): string
{
$inputFileDirectory = dirname($fixtureFilePath);

// remove ".inc" suffix
if (str_ends_with($fixtureFilePath, '.inc')) {
$trimmedFixtureFilePath = Strings::substring($fixtureFilePath, 0, -4);
} else {
$trimmedFixtureFilePath = $fixtureFilePath;
}
$trimmedFixtureFilePath = $fixtureFilePath;

$fixtureBasename = pathinfo($trimmedFixtureFilePath, PATHINFO_BASENAME);
return $inputFileDirectory . '/' . $fixtureBasename;
Expand Down
4 changes: 3 additions & 1 deletion typo3-fractor/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
"a9f/fractor": "@dev",
"a9f/fractor-doc-generator": "@dev",
"a9f/fractor-extension-installer": "@dev",
"a9f/fractor-xml": "@dev"
"a9f/fractor-fluid": "@dev",
"a9f/fractor-xml": "@dev",
"a9f/fractor-yaml": "@dev"
},
"require-dev": {
"ergebnis/composer-normalize": "^2.42",
Expand Down
3 changes: 3 additions & 0 deletions typo3-fractor/config/typo3-10.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
<?php

use a9f\Typo3Fractor\TYPO3v10\Yaml\EmailFinisherFractor;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->defaults()
->autoconfigure()
->autowire();

$services->set(EmailFinisherFractor::class);
};
145 changes: 145 additions & 0 deletions typo3-fractor/rules/TYPO3v10/Yaml/EmailFinisherFractor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
<?php

declare(strict_types=1);

namespace a9f\Typo3Fractor\TYPO3v10\Yaml;

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

final class EmailFinisherFractor implements YamlFractorRule
{
/**
* @var string
*/
private const FINISHERS = 'finishers';

/**
* @var string
*/
private const OPTIONS = 'options';

/**
* @var string
*/
private const RECIPIENT_ADDRESS = 'recipientAddress';

/**
* @var string
*/
private const RECIPIENTS = 'recipients';

/**
* @var string
*/
private const VARIANTS = 'variants';

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Convert single recipient values to array for EmailFinisher', [
new CodeSample(
<<<'CODE_SAMPLE'
finishers:
-
options:
recipientAddress: [email protected]
recipientName: 'Bar'
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
finishers:
-
options:
recipients:
[email protected]: 'Bar'
CODE_SAMPLE
),
]);
}

public function refactor(array $yaml): array
{
if (array_key_exists(self::FINISHERS, $yaml)) {
$this->refactorFinishers($yaml[self::FINISHERS], $yaml);
}

if (array_key_exists(self::VARIANTS, $yaml)) {
foreach ($yaml[self::VARIANTS] as $variantKey => $variant) {
if (!array_key_exists(self::FINISHERS, $variant)) {
continue;
}

$this->refactorFinishers(
$variant[self::FINISHERS],
$yaml[self::VARIANTS][$variantKey]
);
}
}

return $yaml;
}

/**
* @param mixed[] $finishers
* @param mixed[] $yamlToModify
*/
private function refactorFinishers(array $finishers, array &$yamlToModify): void
{
foreach ($finishers as $finisherKey => $finisher) {
if (!array_key_exists('identifier', $finisher)) {
continue;
}

if (!in_array($finisher['identifier'], ['EmailToSender', 'EmailToReceiver'], true)) {
continue;
}

if (!array_key_exists(self::OPTIONS, $finisher)) {
continue;
}

$recipients = [];
foreach ((array) $finisher[self::OPTIONS] as $optionKey => $optionValue) {
if (!in_array(
$optionKey,
[
'replyToAddress',
'carbonCopyAddress',
'blindCarbonCopyAddress',
self::RECIPIENT_ADDRESS,
'recipientName',
],
true
)) {
continue;
}

if ($optionKey === 'replyToAddress') {
$yamlToModify[self::FINISHERS][$finisherKey][self::OPTIONS]['replyToRecipients'][] = $optionValue;
} elseif ($optionKey === 'carbonCopyAddress') {
$yamlToModify[self::FINISHERS][$finisherKey][self::OPTIONS]['carbonCopyRecipients'][] = $optionValue;
} elseif ($optionKey === 'blindCarbonCopyAddress') {
$yamlToModify[self::FINISHERS][$finisherKey][self::OPTIONS]['blindCarbonCopyRecipients'][] = $optionValue;
}

unset(
$yamlToModify[self::FINISHERS][$finisherKey][self::OPTIONS][$optionKey]
);
}

if (isset($finisher[self::OPTIONS][self::RECIPIENT_ADDRESS])) {
$recipients[$finisher[self::OPTIONS][self::RECIPIENT_ADDRESS]] = $finisher[self::OPTIONS]['recipientName'] ?: '';
}

if (isset($finisher[self::OPTIONS][self::RECIPIENTS])) {
$yamlToModify[self::FINISHERS][$finisherKey][self::OPTIONS][self::RECIPIENTS] = array_merge(
$recipients,
$yamlToModify[self::FINISHERS][$finisherKey][self::OPTIONS][self::RECIPIENTS]
);
} else {
$yamlToModify[self::FINISHERS][$finisherKey][self::OPTIONS][self::RECIPIENTS] = $recipients;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Rules\TYPO3v10\Yaml\EmailFinisherFractor;

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

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

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

protected function additionalConfigurationFiles(): array
{
return [
__DIR__ . '/config/config.php',
];
}

public function provideConfigFilePath(): ?string
{
return __DIR__ . '/config/fractor.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
finishers:
-
options:
recipients:
[email protected]: 'Bar'
subject: 'Kontaktanfrage von Website'
recipientAddress: [email protected]
recipientName: Stadtwerke Bonn
replyToAddress: [email protected]
senderAddress: '{email}'
senderName: '{lastName}'
carbonCopyAddress: [email protected]
blindCarbonCopyAddress: [email protected]
format: html
attachUploads: true
identifier: EmailToReceiver
-
options:
recipients:
[email protected]: 'Bar'
subject: 'Kontaktanfrage von Website'
recipientAddress: [email protected]
recipientName: Stadtwerke Bonn
replyToAddress: [email protected]
senderAddress: '{email}'
senderName: '{lastName}'
carbonCopyAddress: [email protected]
blindCarbonCopyAddress: [email protected]
format: html
attachUploads: true
identifier: EmailToSender
variants:
-
identifier: variant-1
condition: 'formValues["foo"] == "bar"'
finishers:
-
identifier: EmailToReceiver
options:
subject: 'Example Subject'
recipientAddress: [email protected]
recipientName: 'Example Recipient'
senderAddress: [email protected]
senderName: '{text-2} {text-1}'
replyToAddress: '{email}'
-----
finishers:
-
options:
recipients:
[email protected]: 'Stadtwerke Bonn'
[email protected]: Bar
subject: 'Kontaktanfrage von Website'
senderAddress: '{email}'
senderName: '{lastName}'
format: html
attachUploads: true
replyToRecipients:
- [email protected]
carbonCopyRecipients:
- [email protected]
blindCarbonCopyRecipients:
- [email protected]
identifier: EmailToReceiver
-
options:
recipients:
[email protected]: 'Stadtwerke Bonn'
[email protected]: Bar
subject: 'Kontaktanfrage von Website'
senderAddress: '{email}'
senderName: '{lastName}'
format: html
attachUploads: true
replyToRecipients:
- [email protected]
carbonCopyRecipients:
- [email protected]
blindCarbonCopyRecipients:
- [email protected]
identifier: EmailToSender
variants:
-
identifier: variant-1
condition: 'formValues["foo"] == "bar"'
finishers:
-
identifier: EmailToReceiver
options:
subject: 'Example Subject'
senderAddress: [email protected]
senderName: '{text-2} {text-1}'
replyToRecipients:
- '{email}'
recipients:
[email protected]: 'Example Recipient'
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
finishers:
-
options:
subject: 'Kontaktanfrage von Website'
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@

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

use a9f\Fractor\Configuration\FractorConfiguration;
use a9f\Typo3Fractor\Set\Typo3SetList;

return FractorConfiguration::configure()
->withSets([Typo3SetList::TYPO3_10]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

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

return static function (ContainerConfigurator $containerConfigurator): void {
$containerConfigurator->import(__DIR__ . '/../../../../../../config/application.php');
};