|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace a9f\Typo3Fractor\TYPO3v13\TypoScript; |
| 6 | + |
| 7 | +use a9f\FractorTypoScript\AbstractTypoScriptFractor; |
| 8 | +use Helmich\TypoScriptParser\Parser\AST\NestedAssignment; |
| 9 | +use Helmich\TypoScriptParser\Parser\AST\Operator\Assignment; |
| 10 | +use Helmich\TypoScriptParser\Parser\AST\Operator\Copy; |
| 11 | +use Helmich\TypoScriptParser\Parser\AST\Operator\Reference; |
| 12 | +use Helmich\TypoScriptParser\Parser\AST\Statement; |
| 13 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
| 14 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 15 | + |
| 16 | +/** |
| 17 | + * @changelog https://docs.typo3.org/c/typo3/cms-core/main/en-us/Changelog/13.4/Deprecation-105076-PluginContentElementAndPluginSubTypes.html |
| 18 | + * @see \a9f\Typo3Fractor\Tests\TYPO3v13\TypoScript\MigratePluginContentElementAndPluginSubtypesFractor\MigratePluginContentElementAndPluginSubtypesFractorTest |
| 19 | + */ |
| 20 | +final class MigratePluginContentElementAndPluginSubtypesFractor extends AbstractTypoScriptFractor |
| 21 | +{ |
| 22 | + public function getRuleDefinition(): RuleDefinition |
| 23 | + { |
| 24 | + return new RuleDefinition('Migrate plugin content element and plugin subtypes (list_type)', [new CodeSample( |
| 25 | + <<<'CODE_SAMPLE' |
| 26 | +tt_content.list.20.examples_pi1 = USER |
| 27 | +tt_content.list.20.examples_pi1 { |
| 28 | + userFunc = MyVendor\Examples\Controller\ExampleController->example |
| 29 | +} |
| 30 | +
|
| 31 | +tt_content.list.20.examples_pi1 < plugin.tx_examples_pi1 |
| 32 | +CODE_SAMPLE |
| 33 | + , |
| 34 | + <<<'CODE_SAMPLE' |
| 35 | +tt_content.examples_pi1 = USER |
| 36 | +tt_content.examples_pi1 { |
| 37 | + userFunc = MyVendor\Examples\Controller\ExampleController->example |
| 38 | +} |
| 39 | +
|
| 40 | +tt_content.examples_pi1 < plugin.tx_examples_pi1 |
| 41 | +CODE_SAMPLE |
| 42 | + )]); |
| 43 | + } |
| 44 | + |
| 45 | + public function refactor(Statement $statement): null|Statement|int |
| 46 | + { |
| 47 | + if ($this->shouldSkip($statement)) { |
| 48 | + return null; |
| 49 | + } |
| 50 | + |
| 51 | + if ($statement instanceof NestedAssignment) { |
| 52 | + $rootLine = ['tt_content', 'list', '20', '#']; |
| 53 | + /** @var NestedAssignment|null $pluginStatement */ |
| 54 | + $pluginStatement = $this->findPluginStatement($statement, $rootLine); |
| 55 | + if ($pluginStatement === null) { |
| 56 | + return null; |
| 57 | + } |
| 58 | + |
| 59 | + $pluginStatement->object->absoluteName = str_replace( |
| 60 | + 'list.20.', |
| 61 | + '', |
| 62 | + $pluginStatement->object->absoluteName |
| 63 | + ); |
| 64 | + $pluginStatement->object->relativeName = str_replace( |
| 65 | + 'list.20.', |
| 66 | + '', |
| 67 | + $pluginStatement->object->relativeName |
| 68 | + ); |
| 69 | + |
| 70 | + if ($pluginStatement->object->absoluteName === $pluginStatement->object->relativeName) { |
| 71 | + return $pluginStatement; |
| 72 | + } |
| 73 | + $statement->statements[0] = $pluginStatement; |
| 74 | + } elseif ($statement instanceof Assignment || $statement instanceof Copy || $statement instanceof Reference) { |
| 75 | + $statement->object->absoluteName = str_replace('list.20.', '', $statement->object->absoluteName); |
| 76 | + $statement->object->relativeName = str_replace('list.20.', '', $statement->object->relativeName); |
| 77 | + } |
| 78 | + |
| 79 | + return $statement; |
| 80 | + } |
| 81 | + |
| 82 | + private function shouldSkip(Statement $statement): bool |
| 83 | + { |
| 84 | + if (! $statement instanceof NestedAssignment |
| 85 | + && ! $statement instanceof Assignment |
| 86 | + && ! $statement instanceof Copy |
| 87 | + && ! $statement instanceof Reference |
| 88 | + ) { |
| 89 | + return true; |
| 90 | + } |
| 91 | + |
| 92 | + if (! str_starts_with($statement->object->absoluteName, 'tt_content')) { |
| 93 | + return true; |
| 94 | + } |
| 95 | + |
| 96 | + return false; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * @param array<int, string> $rootLine |
| 101 | + */ |
| 102 | + private function findPluginStatement(Statement $statement, array $rootLine): ?Statement |
| 103 | + { |
| 104 | + if (! $statement instanceof NestedAssignment) { |
| 105 | + return null; |
| 106 | + } |
| 107 | + |
| 108 | + if (! isset($rootLine[0])) { |
| 109 | + return $statement; |
| 110 | + } |
| 111 | + |
| 112 | + $objectPath = $statement->object->relativeName; |
| 113 | + $parts = explode('.', $objectPath); |
| 114 | + foreach ($parts as $part) { |
| 115 | + $firstRootLineItem = $rootLine[0]; |
| 116 | + if ($firstRootLineItem === $part || $firstRootLineItem === '#') { |
| 117 | + array_shift($rootLine); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + if ($rootLine === []) { |
| 122 | + // we found it! |
| 123 | + return $statement; |
| 124 | + } |
| 125 | + |
| 126 | + return $this->findPluginStatement($statement->statements[0], $rootLine); |
| 127 | + } |
| 128 | +} |
0 commit comments