Skip to content

Commit bcd09a2

Browse files
authored
[FEATURE] Make allowed file extensions configurable for all file processors (#343)
1 parent 0102389 commit bcd09a2

File tree

20 files changed

+386
-15
lines changed

20 files changed

+386
-15
lines changed

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,42 @@ Possible values for `TypoScriptProcessorOption::CONDITION_TERMINATION`:
168168
- `PrettyPrinterConditionTermination::EnforceGlobal` will always end with `[global]`
169169
- `PrettyPrinterConditionTermination::EnforceEnd` will always end with `[end]`
170170

171+
### Configure the allowed file extensions
172+
173+
By default, Fractor processes files based on their extensions. You can customize which file extensions each processor should handle by configuring the `ALLOWED_FILE_EXTENSIONS` option.
174+
175+
The following are the default extensions processed for each file type:
176+
177+
- **Fluid**: `html`, `xml`, `txt`
178+
- **TypoScript**: `typoscript`, `tsconfig`, `ts`
179+
- **XML**: `xml`
180+
- **YAML**: `yaml`, `yml`
181+
182+
To override these defaults, use the configuration that looks similiar to the following:
183+
184+
```php
185+
<?php
186+
187+
use a9f\Fractor\Configuration\FractorConfiguration;
188+
use a9f\FractorFluid\Configuration\FluidProcessorOption;
189+
use a9f\FractorTypoScript\Configuration\TypoScriptProcessorOption;
190+
use a9f\FractorXml\Configuration\XmlProcessorOption;
191+
use a9f\FractorYaml\Configuration\YamlProcessorOption;
192+
193+
return FractorConfiguration::configure()
194+
->withOptions([
195+
// Only process html files
196+
FluidProcessorOption::ALLOWED_FILE_EXTENSIONS => ['html'],
197+
// Remove ts files and only process typoscript and tsconfig
198+
TypoScriptProcessorOption::ALLOWED_FILE_EXTENSIONS => ['typoscript', 'tsconfig'],
199+
// Also process xlf files
200+
XmlProcessorOption::ALLOWED_FILE_EXTENSIONS => ['xml', 'xlf'],
201+
// Keep defaults - can be omitted in that case
202+
YamlProcessorOption::ALLOWED_FILE_EXTENSIONS => ['yaml', 'yml'],
203+
]);
204+
205+
```
206+
171207
## Processing
172208

173209
Before executing the code migrations, run the following command to see a preview of what Fractor will do:

packages/fractor-fluid/config/application.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
use a9f\FractorFluid\Contract\FluidFractorRule;
66
use a9f\FractorFluid\FluidFileProcessor;
7+
use a9f\FractorFluid\ValueObject\FluidFormatConfiguration;
78
use Symfony\Component\DependencyInjection\ContainerBuilder;
89
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
10+
use function Symfony\Component\DependencyInjection\Loader\Configurator\service;
911
use function Symfony\Component\DependencyInjection\Loader\Configurator\tagged_iterator;
1012

1113
return static function (ContainerConfigurator $containerConfigurator, ContainerBuilder $containerBuilder): void {
@@ -16,8 +18,12 @@
1618

1719
$services->load('a9f\\FractorFluid\\', __DIR__ . '/../src/');
1820

21+
$services->set('fractor.fluid_processor.format_configuration', FluidFormatConfiguration::class)
22+
->factory([null, 'createFromParameterBag']);
23+
1924
$services->set(FluidFileProcessor::class)
20-
->arg('$rules', tagged_iterator('fractor.fluid_rule'));
25+
->arg('$rules', tagged_iterator('fractor.fluid_rule'))
26+
->arg('$fluidFormatConfiguration', service('fractor.fluid_processor.format_configuration'));
2127

2228
$containerBuilder->registerForAutoconfiguration(FluidFractorRule::class)->addTag('fractor.fluid_rule');
2329
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace a9f\FractorFluid\Configuration;
6+
7+
final class FluidProcessorOption
8+
{
9+
public const ALLOWED_FILE_EXTENSIONS = 'fluid-processor-allowed-file-extensions';
10+
}

packages/fractor-fluid/src/FluidFileProcessor.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use a9f\Fractor\Application\ValueObject\File;
1010
use a9f\Fractor\Caching\Detector\ChangedFilesDetector;
1111
use a9f\FractorFluid\Contract\FluidFractorRule;
12+
use a9f\FractorFluid\ValueObject\FluidFormatConfiguration;
1213

1314
/**
1415
* @implements FileProcessor<FluidFractorRule>
@@ -20,7 +21,8 @@
2021
*/
2122
public function __construct(
2223
private iterable $rules,
23-
private ChangedFilesDetector $changedFilesDetector
24+
private ChangedFilesDetector $changedFilesDetector,
25+
private FluidFormatConfiguration $fluidFormatConfiguration
2426
) {
2527
}
2628

@@ -43,9 +45,12 @@ public function handle(File $file, iterable $appliedRules): void
4345
}
4446
}
4547

48+
/**
49+
* @return list<non-empty-string>
50+
*/
4651
public function allowedFileExtensions(): array
4752
{
48-
return ['html', 'xml', 'txt'];
53+
return $this->fluidFormatConfiguration->allowedFileExtensions;
4954
}
5055

5156
public function getAllRules(): iterable
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace a9f\FractorFluid\ValueObject;
6+
7+
use a9f\FractorFluid\Configuration\FluidProcessorOption;
8+
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
9+
10+
final readonly class FluidFormatConfiguration
11+
{
12+
/**
13+
* @param list<non-empty-string> $allowedFileExtensions
14+
*/
15+
public function __construct(
16+
public array $allowedFileExtensions
17+
) {
18+
}
19+
20+
public static function createFromParameterBag(ParameterBagInterface $parameterBag): self
21+
{
22+
/** @var list<non-empty-string> $allowedFileExtensions */
23+
$allowedFileExtensions = $parameterBag->has(FluidProcessorOption::ALLOWED_FILE_EXTENSIONS)
24+
? $parameterBag->get(FluidProcessorOption::ALLOWED_FILE_EXTENSIONS)
25+
: ['html', 'xml', 'txt'];
26+
27+
return new self($allowedFileExtensions);
28+
}
29+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace a9f\FractorFluid\Tests\FluidFileProcessor;
6+
7+
use a9f\FractorFluid\Configuration\FluidProcessorOption;
8+
use a9f\FractorFluid\ValueObject\FluidFormatConfiguration;
9+
use PHPUnit\Framework\Attributes\Test;
10+
use PHPUnit\Framework\TestCase;
11+
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
12+
13+
final class FluidFileProcessorConfigurationTest extends TestCase
14+
{
15+
#[Test]
16+
public function defaultFileExtensions(): void
17+
{
18+
$parameterBag = new ParameterBag([]);
19+
$configuration = FluidFormatConfiguration::createFromParameterBag($parameterBag);
20+
21+
self::assertSame(['html', 'xml', 'txt'], $configuration->allowedFileExtensions);
22+
}
23+
24+
#[Test]
25+
public function customFileExtensions(): void
26+
{
27+
$parameterBag = new ParameterBag([
28+
FluidProcessorOption::ALLOWED_FILE_EXTENSIONS => ['html'],
29+
]);
30+
$configuration = FluidFormatConfiguration::createFromParameterBag($parameterBag);
31+
32+
self::assertSame(['html'], $configuration->allowedFileExtensions);
33+
}
34+
35+
#[Test]
36+
public function multipleCustomFileExtensions(): void
37+
{
38+
$parameterBag = new ParameterBag([
39+
FluidProcessorOption::ALLOWED_FILE_EXTENSIONS => ['html', 'xml'],
40+
]);
41+
$configuration = FluidFormatConfiguration::createFromParameterBag($parameterBag);
42+
43+
self::assertSame(['html', 'xml'], $configuration->allowedFileExtensions);
44+
}
45+
}

packages/fractor-typoscript/src/Configuration/TypoScriptProcessorOption.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,6 @@ final class TypoScriptProcessorOption
1717
public const INDENT_CONDITIONS = 'typoscript-processor-indent-conditions';
1818

1919
public const CONDITION_TERMINATION = 'typoscript-processor-condition-termination';
20+
21+
public const ALLOWED_FILE_EXTENSIONS = 'typoscript-processor-allowed-file-extensions';
2022
}

packages/fractor-typoscript/src/TypoScriptFileProcessor.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function __construct(
3939

4040
public function canHandle(File $file): bool
4141
{
42-
return in_array($file->getFileExtension(), $this->allowedFileExtensions());
42+
return in_array($file->getFileExtension(), $this->allowedFileExtensions(), true);
4343
}
4444

4545
public function handle(File $file, iterable $appliedRules): void
@@ -75,10 +75,12 @@ public function handle(File $file, iterable $appliedRules): void
7575
}
7676
}
7777

78+
/**
79+
* @return list<non-empty-string>
80+
*/
7881
public function allowedFileExtensions(): array
7982
{
80-
// TODO this should be configurable
81-
return ['typoscript', 'tsconfig', 'ts'];
83+
return $this->typoScriptPrettyPrinterFormatConfiguration->allowedFileExtensions;
8284
}
8385

8486
public function getAllRules(): iterable

packages/fractor-typoscript/src/ValueObject/TypoScriptPrettyPrinterFormatConfiguration.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,17 @@
1010

1111
final readonly class TypoScriptPrettyPrinterFormatConfiguration
1212
{
13+
/**
14+
* @param list<non-empty-string> $allowedFileExtensions
15+
*/
1316
public function __construct(
1417
public int $size,
1518
public string $style,
1619
public bool $addClosingGlobal,
1720
public bool $includeEmptyLineBreaks,
1821
public bool $indentConditions,
19-
public PrettyPrinterConditionTermination $conditionTermination
22+
public PrettyPrinterConditionTermination $conditionTermination,
23+
public array $allowedFileExtensions
2024
) {
2125
}
2226

@@ -48,13 +52,19 @@ public static function createFromParameterBag(ParameterBagInterface $parameterBa
4852
? $parameterBag->get(TypoScriptProcessorOption::CONDITION_TERMINATION)
4953
: PrettyPrinterConditionTermination::Keep;
5054

55+
/** @var list<non-empty-string> $allowedFileExtensions */
56+
$allowedFileExtensions = $parameterBag->has(TypoScriptProcessorOption::ALLOWED_FILE_EXTENSIONS)
57+
? $parameterBag->get(TypoScriptProcessorOption::ALLOWED_FILE_EXTENSIONS)
58+
: ['typoscript', 'tsconfig', 'ts'];
59+
5160
return new self(
5261
$size,
5362
$style,
5463
$addClosingGlobal,
5564
$includeEmptyLineBreaks,
5665
$indentConditions,
57-
$conditionTermination
66+
$conditionTermination,
67+
$allowedFileExtensions
5868
);
5969
}
6070
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace a9f\FractorTypoScript\Tests\TypoScriptFileProcessor;
6+
7+
use a9f\FractorTypoScript\Configuration\TypoScriptProcessorOption;
8+
use a9f\FractorTypoScript\ValueObject\TypoScriptPrettyPrinterFormatConfiguration;
9+
use PHPUnit\Framework\Attributes\Test;
10+
use PHPUnit\Framework\TestCase;
11+
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
12+
13+
final class TypoScriptFileProcessorConfigurationTest extends TestCase
14+
{
15+
#[Test]
16+
public function defaultFileExtensions(): void
17+
{
18+
$parameterBag = new ParameterBag([]);
19+
$configuration = TypoScriptPrettyPrinterFormatConfiguration::createFromParameterBag($parameterBag);
20+
21+
self::assertSame(['typoscript', 'tsconfig', 'ts'], $configuration->allowedFileExtensions);
22+
}
23+
24+
#[Test]
25+
public function customFileExtensions(): void
26+
{
27+
$parameterBag = new ParameterBag([
28+
TypoScriptProcessorOption::ALLOWED_FILE_EXTENSIONS => ['typoscript', 'tss'],
29+
]);
30+
$configuration = TypoScriptPrettyPrinterFormatConfiguration::createFromParameterBag($parameterBag);
31+
32+
self::assertSame(['typoscript', 'tss'], $configuration->allowedFileExtensions);
33+
}
34+
35+
#[Test]
36+
public function multipleCustomFileExtensions(): void
37+
{
38+
$parameterBag = new ParameterBag([
39+
TypoScriptProcessorOption::ALLOWED_FILE_EXTENSIONS => ['typoscript', 'tsconfig', 'tss', 'tsc'],
40+
]);
41+
$configuration = TypoScriptPrettyPrinterFormatConfiguration::createFromParameterBag($parameterBag);
42+
43+
self::assertSame(['typoscript', 'tsconfig', 'tss', 'tsc'], $configuration->allowedFileExtensions);
44+
}
45+
}

0 commit comments

Comments
 (0)