Skip to content

Commit 9a206bb

Browse files
authored
[FEATURE] Add MigrateIncludeTypoScriptSyntaxFractor (#284)
Fixes: sabbelasichon/typo3-rector#4324
1 parent 058d425 commit 9a206bb

File tree

18 files changed

+575
-3
lines changed

18 files changed

+575
-3
lines changed

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
"composer-plugin-api": "^2.0",
1717
"ergebnis/json-printer": "^3.5",
1818
"eta-orionis/composer-json-manipulator": "^1.0.1",
19-
"helmich/typo3-typoscript-parser": "^2.6",
19+
"helmich/typo3-typoscript-parser": "^2.7.0",
20+
"league/flysystem": "^2.0 || ^3.0",
21+
"league/flysystem-memory": "^2.0 || ^3.0",
2022
"nette/utils": "^4.0",
2123
"ondram/ci-detector": "^4.2",
2224
"phpstan/phpstan": "^1.10.9",

packages/fractor-typoscript/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"php": "^8.2",
1515
"a9f/fractor": "^0.5",
1616
"a9f/fractor-extension-installer": "^0.5",
17-
"helmich/typo3-typoscript-parser": "^2.6",
17+
"helmich/typo3-typoscript-parser": "^2.7.0",
1818
"symplify/rule-doc-generator-contracts": "^11.2",
1919
"webmozart/assert": "^1.11"
2020
},

packages/fractor-typoscript/config/application.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use a9f\FractorTypoScript\Contract\TypoScriptFractor;
66
use a9f\FractorTypoScript\TypoScriptFileProcessor;
77
use a9f\FractorTypoScript\ValueObject\TypoScriptPrettyPrinterFormatConfiguration;
8+
use Helmich\TypoScriptParser\Parser\AST\Builder;
89
use Helmich\TypoScriptParser\Parser\Parser;
910
use Helmich\TypoScriptParser\Parser\Printer\PrettyPrinter;
1011
use Helmich\TypoScriptParser\Tokenizer\Tokenizer;
@@ -25,6 +26,8 @@
2526
$services->set(Parser::class)
2627
->arg('$tokenizer', service(Tokenizer::class))
2728
->public();
29+
$services->set(Builder::class)
30+
->public();
2831
$services->set(PrettyPrinter::class);
2932

3033
$services->set('fractor.typoscript_processor.pretty_printer', TypoScriptPrettyPrinterFormatConfiguration::class)

packages/fractor/composer.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
],
1313
"require": {
1414
"php": "^8.2",
15+
"league/flysystem": "^2.0 || ^3.0",
16+
"league/flysystem-memory": "^2.0 || ^3.0",
1517
"nette/utils": "^4.0",
1618
"ondram/ci-detector": "^4.2",
1719
"sebastian/diff": "^4.0 || ^5.0 || ^6.0",

packages/fractor/config/application.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@
1212
use a9f\Fractor\Configuration\ValueObject\SkipConfiguration;
1313
use a9f\Fractor\Console\Application\FractorApplication;
1414
use a9f\Fractor\Console\Output\OutputFormatterCollector;
15+
use a9f\Fractor\Contract\FilesystemInterface;
1516
use a9f\Fractor\Differ\ConsoleDiffer;
1617
use a9f\Fractor\Differ\Contract\Differ;
18+
use a9f\Fractor\FileSystem\FilesystemFactory;
19+
use a9f\Fractor\FileSystem\FlysystemFilesystem;
20+
use League\Flysystem\FilesystemAdapter;
1721
use Symfony\Component\Console\Attribute\AsCommand;
1822
use Symfony\Component\DependencyInjection\ChildDefinition;
1923
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -82,6 +86,15 @@ static function (ChildDefinition $definition, AsCommand $attribute): void {
8286
}
8387
);
8488

89+
$services->set(\League\Flysystem\Filesystem::class)->public();
90+
$services->set(FilesystemAdapter::class)->public();
91+
92+
$services->set(FilesystemFactory::class)
93+
->arg('$projectDir', '/');
94+
$services->set(FilesystemInterface::class)->factory([service(FilesystemFactory::class), 'create']);
95+
$services->set(FlysystemFilesystem::class)
96+
->arg('$filesystemOperator', service(\League\Flysystem\Filesystem::class));
97+
8598
$services->set('parameter_bag', ContainerBag::class)
8699
->args([service('service_container')])
87100
->alias(ContainerBagInterface::class, 'parameter_bag')
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace a9f\Fractor\Contract;
6+
7+
use League\Flysystem\DirectoryListing;
8+
use League\Flysystem\FilesystemReader;
9+
use League\Flysystem\StorageAttributes;
10+
11+
interface FilesystemInterface
12+
{
13+
/**
14+
* @param array<string, mixed> $config
15+
*/
16+
public function write(string $location, string $contents, array $config = []): void;
17+
18+
public function fileExists(string $location): bool;
19+
20+
public function read(string $location): string;
21+
22+
/**
23+
* @param array<string, mixed> $config
24+
*/
25+
public function move(string $source, string $destination, array $config = []): void;
26+
27+
/**
28+
* @return DirectoryListing<StorageAttributes>
29+
*/
30+
public function listContents(string $location, bool $deep = FilesystemReader::LIST_SHALLOW): DirectoryListing;
31+
32+
public function appendToFile(string $location, string $content): void;
33+
34+
public function delete(string $location): void;
35+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace a9f\Fractor\FileSystem;
6+
7+
use a9f\Fractor\Contract\FilesystemInterface;
8+
use League\Flysystem\Filesystem;
9+
use League\Flysystem\InMemory\InMemoryFilesystemAdapter;
10+
use League\Flysystem\Local\LocalFilesystemAdapter;
11+
use Rector\Testing\PHPUnit\StaticPHPUnitEnvironment;
12+
13+
final readonly class FilesystemFactory
14+
{
15+
public function __construct(
16+
private string $projectDir
17+
) {
18+
}
19+
20+
public function create(): FilesystemInterface
21+
{
22+
$argv = $_SERVER['argv'] ?? [];
23+
$isDryRun = in_array('--dry-run', $argv, true) || in_array('-n', $argv, true);
24+
25+
if ($isDryRun || StaticPHPUnitEnvironment::isPHPUnitRun()) {
26+
$adapter = new InMemoryFilesystemAdapter();
27+
} else {
28+
$adapter = new LocalFilesystemAdapter($this->projectDir);
29+
}
30+
31+
return new FlysystemFilesystem(new Filesystem($adapter));
32+
}
33+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace a9f\Fractor\FileSystem;
6+
7+
use a9f\Fractor\Contract\FilesystemInterface;
8+
use League\Flysystem\DirectoryListing;
9+
use League\Flysystem\FilesystemOperator;
10+
use League\Flysystem\FilesystemReader;
11+
use League\Flysystem\UnableToDeleteFile;
12+
use League\Flysystem\UnableToReadFile;
13+
use League\Flysystem\UnableToWriteFile;
14+
15+
final readonly class FlysystemFilesystem implements FilesystemInterface
16+
{
17+
public function __construct(
18+
private FilesystemOperator $filesystemOperator
19+
) {
20+
}
21+
22+
public function write(string $location, string $contents, array $config = []): void
23+
{
24+
try {
25+
$this->filesystemOperator->write($location, $contents, $config);
26+
} catch (UnableToWriteFile $e) {
27+
throw new \RuntimeException(sprintf('Failed to write file "%s": %s', $location, $e->getMessage()), 0, $e);
28+
}
29+
}
30+
31+
public function fileExists(string $location): bool
32+
{
33+
return $this->filesystemOperator->fileExists($location);
34+
}
35+
36+
public function read(string $location): string
37+
{
38+
try {
39+
return $this->filesystemOperator->read($location);
40+
} catch (UnableToReadFile $e) {
41+
throw new \RuntimeException(sprintf('Failed to read file "%s": %s', $location, $e->getMessage()), 0, $e);
42+
}
43+
}
44+
45+
public function move(string $source, string $destination, array $config = []): void
46+
{
47+
$this->filesystemOperator->move($source, $destination, $config);
48+
}
49+
50+
public function listContents(string $location, bool $deep = FilesystemReader::LIST_SHALLOW): DirectoryListing
51+
{
52+
return $this->filesystemOperator->listContents($location, $deep);
53+
}
54+
55+
public function appendToFile(string $location, string $content): void
56+
{
57+
$existingContent = $this->read($location);
58+
59+
$existingContent .= PHP_EOL . $content;
60+
61+
$this->write($location, $existingContent);
62+
}
63+
64+
public function delete(string $location): void
65+
{
66+
try {
67+
$this->filesystemOperator->delete($location);
68+
} catch (UnableToDeleteFile $e) {
69+
throw new \RuntimeException(sprintf('Failed to delete file "%s": %s', $location, $e->getMessage()), 0, $e);
70+
}
71+
}
72+
}

packages/typo3-fractor/docs/typo3-fractor-rules.md

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 31 Rules Overview
1+
# 32 Rules Overview
22

33
## AbstractMessageGetSeverityFluidFractor
44

@@ -121,6 +121,42 @@ Migrate eval int and double2 to type number
121121

122122
<br>
123123

124+
## MigrateIncludeTypoScriptSyntaxFractor
125+
126+
Migrate INCLUDE_TYPOSCRIPT TypoScript syntax to `@import`
127+
128+
- class: [`a9f\Typo3Fractor\TYPO3v13\TypoScript\MigrateIncludeTypoScriptSyntaxFractor`](../rules/TYPO3v13/TypoScript/MigrateIncludeTypoScriptSyntaxFractor.php)
129+
130+
```diff
131+
-<INCLUDE_TYPOSCRIPT: source="FILE:EXT:my_extension/Configuration/TypoScript/myMenu.typoscript">
132+
+@import 'EXT:my_extension/Configuration/TypoScript/myMenu.typoscript'
133+
```
134+
135+
<br>
136+
137+
```diff
138+
-<INCLUDE_TYPOSCRIPT: source="DIR:EXT:my_extension/Configuration/TypoScript/" extensions="typoscript">
139+
+@import 'EXT:my_extension/Configuration/TypoScript/*.typoscript'
140+
```
141+
142+
<br>
143+
144+
```diff
145+
-<INCLUDE_TYPOSCRIPT: source="DIR:EXT:my_extension/Configuration/TypoScript/" extensions="typoscript,ts">
146+
+@import 'EXT:my_extension/Configuration/TypoScript/*.typoscript'
147+
```
148+
149+
<br>
150+
151+
```diff
152+
-<INCLUDE_TYPOSCRIPT: source="FILE:EXT:my_extension/Configuration/TypoScript/user.typoscript" condition="[frontend.user.isLoggedIn]">
153+
+[frontend.user.isLoggedIn]
154+
+ @import 'EXT:my_extension/Configuration/TypoScript/user.typoscript'
155+
+[end]
156+
```
157+
158+
<br>
159+
124160
## MigrateInternalTypeFolderToTypeFolderFlexFormFractor
125161

126162
Migrates TCA internal_type into new new TCA type folder
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<INCLUDE_TYPOSCRIPT: source="DIR:EXT:extension1/Configuration/TypoScript/Includes/" extensions="ts">
2+
-----
3+
@import 'EXT:extension1/Configuration/TypoScript/Includes/*.typoscript'

0 commit comments

Comments
 (0)