-
-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] MoveAllowTableOnStandardPagesToTCAConfigurationRector (#4161)
- Loading branch information
1 parent
da56ecf
commit be6e684
Showing
13 changed files
with
292 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
rules/TYPO312/v0/MoveAllowTableOnStandardPagesToTCAConfigurationRector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ssch\TYPO3Rector\TYPO312\v0; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\StaticCall; | ||
use PhpParser\Node\Stmt\Expression; | ||
use PhpParser\NodeTraverser; | ||
use PHPStan\Type\ObjectType; | ||
use Rector\PhpParser\Node\Value\ValueResolver; | ||
use Rector\Rector\AbstractRector; | ||
use Ssch\TYPO3Rector\Contract\FilesystemInterface; | ||
use Ssch\TYPO3Rector\Filesystem\FilesFinder; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @changelog https://docs.typo3.org/c/typo3/cms-core/main/en-us/Changelog/12.0/Deprecation-98487-ExtensionManagementUtilityallowTableOnStandardPages.html | ||
* @see \Ssch\TYPO3Rector\Tests\Rector\v12\v0\MoveAllowTableOnStandardPagesToTCAConfigurationRector\MoveAllowTableOnStandardPagesToTCAConfigurationRectorTest | ||
*/ | ||
final class MoveAllowTableOnStandardPagesToTCAConfigurationRector extends AbstractRector | ||
{ | ||
/** | ||
* @readonly | ||
*/ | ||
private FilesFinder $filesFinder; | ||
|
||
/** | ||
* @readonly | ||
*/ | ||
private ValueResolver $valueResolver; | ||
|
||
/** | ||
* @readonly | ||
*/ | ||
private FilesystemInterface $filesystem; | ||
|
||
public function __construct(FilesFinder $filesFinder, ValueResolver $valueResolver, FilesystemInterface $filesystem) | ||
{ | ||
$this->filesFinder = $filesFinder; | ||
$this->valueResolver = $valueResolver; | ||
$this->filesystem = $filesystem; | ||
} | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition( | ||
'Move method ExtensionManagementUtility::allowTableOnStandardPages to TCA configuration', | ||
[new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility; | ||
ExtensionManagementUtility::allowTableOnStandardPages('my_table'); | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
$GLOBALS['TCA']['my_table']['ctrl']['security']['ignorePageTypeRestriction'] | ||
CODE_SAMPLE | ||
)] | ||
); | ||
} | ||
|
||
public function getNodeTypes(): array | ||
{ | ||
return [Expression::class]; | ||
} | ||
|
||
/** | ||
* @param Expression $node | ||
*/ | ||
public function refactor(Node $node) | ||
{ | ||
$staticMethodCall = $node->expr; | ||
|
||
if (! $staticMethodCall instanceof StaticCall) { | ||
return null; | ||
} | ||
|
||
if ($this->shouldSkip($staticMethodCall)) { | ||
return null; | ||
} | ||
|
||
$tableArgument = $staticMethodCall->args[0] ?? null; | ||
|
||
if ($tableArgument === null) { | ||
return null; | ||
} | ||
|
||
$tableName = $this->valueResolver->getValue($tableArgument); | ||
|
||
$directoryName = dirname($this->file->getFilePath()); | ||
|
||
$newConfigurationFile = $directoryName . '/Configuration/TCA/Overrides/' . $tableName . '.php'; | ||
$this->writeConfigurationToFile($newConfigurationFile, $tableName); | ||
|
||
return NodeTraverser::REMOVE_NODE; | ||
} | ||
|
||
private function shouldSkip(StaticCall $staticMethodCall): bool | ||
{ | ||
if (! $this->nodeTypeResolver->isMethodStaticCallOrClassMethodObjectType( | ||
$staticMethodCall, | ||
new ObjectType('TYPO3\CMS\Core\Utility\ExtensionManagementUtility') | ||
)) { | ||
return true; | ||
} | ||
|
||
if (! $this->isName($staticMethodCall->name, 'allowTableOnStandardPages')) { | ||
return true; | ||
} | ||
|
||
return ! $this->filesFinder->isExtTables($this->file->getFilePath()); | ||
} | ||
|
||
private function writeConfigurationToFile(string $newConfigurationFile, string $tableName): void | ||
{ | ||
$content = sprintf( | ||
'$GLOBALS[\'TCA\'][\'%s\'][\'ctrl\'][\'security\'][\'ignorePageTypeRestriction\'] = true;', | ||
$tableName | ||
); | ||
$this->filesystem->write($newConfigurationFile, <<<CODE | ||
<?php | ||
{$content} | ||
CODE | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ssch\TYPO3Rector\Contract; | ||
|
||
interface FilesystemInterface | ||
{ | ||
public function write(string $location, string $contents): void; | ||
|
||
public function fileExists(string $location): bool; | ||
|
||
public function read(string $location): string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ssch\TYPO3Rector\Filesystem; | ||
|
||
use League\Flysystem\FilesystemOperator; | ||
use Ssch\TYPO3Rector\Contract\FilesystemInterface; | ||
|
||
final class FlysystemFilesystem implements FilesystemInterface | ||
{ | ||
/** | ||
* @readonly | ||
*/ | ||
private FilesystemOperator $filesystemOperator; | ||
|
||
public function __construct(FilesystemOperator $filesystemOperator) | ||
{ | ||
$this->filesystemOperator = $filesystemOperator; | ||
} | ||
|
||
public function write(string $location, string $contents): void | ||
{ | ||
$this->filesystemOperator->write($location, $contents); | ||
} | ||
|
||
public function fileExists(string $location): bool | ||
{ | ||
return $this->filesystemOperator->fileExists($location); | ||
} | ||
|
||
public function read(string $location): string | ||
{ | ||
return $this->filesystemOperator->read($location); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
...CAConfigurationRector/Assertions/tx_table_without_existing_tca_configuration_file.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<?php | ||
|
||
$GLOBALS['TCA']['tx_table_without_existing_tca_configuration_file']['ctrl']['security']['ignorePageTypeRestriction'] = true; |
19 changes: 19 additions & 0 deletions
19
...r/v12/v0/MoveAllowTableOnStandardPagesToTCAConfigurationRector/Fixture/ext_tables.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace Ssch\TYPO3Rector\Tests\Rector\v12\v0\MoveAllowTableOnStandardPagesToTCAConfigurationRector\Fixture; | ||
|
||
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility; | ||
|
||
ExtensionManagementUtility::allowTableOnStandardPages('tx_table_with_existing_tca_configuration_file'); | ||
ExtensionManagementUtility::allowTableOnStandardPages('tx_table_with_existing_tca_configuration_file_and_security_key'); | ||
ExtensionManagementUtility::allowTableOnStandardPages('tx_table_without_existing_tca_configuration_file'); | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Ssch\TYPO3Rector\Tests\Rector\v12\v0\MoveAllowTableOnStandardPagesToTCAConfigurationRector\Fixture; | ||
|
||
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility; | ||
|
||
?> |
51 changes: 51 additions & 0 deletions
51
...gesToTCAConfigurationRector/MoveAllowTableOnStandardPagesToTCAConfigurationRectorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ssch\TYPO3Rector\Tests\Rector\v12\v0\MoveAllowTableOnStandardPagesToTCAConfigurationRector; | ||
|
||
use Nette\Utils\FileSystem; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
use Ssch\TYPO3Rector\Contract\FilesystemInterface; | ||
|
||
final class MoveAllowTableOnStandardPagesToTCAConfigurationRectorTest extends AbstractRectorTestCase | ||
{ | ||
private FilesystemInterface $filesystem; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
$this->initializeFilesystem(); | ||
} | ||
|
||
public function test(): void | ||
{ | ||
$this->doTestFile(__DIR__ . '/Fixture/ext_tables.php.inc'); | ||
|
||
$this->assertThatConfigurationFileHasNewIgnorePageTypeRestriction( | ||
__DIR__ . '/Fixture/Configuration/TCA/Overrides/%s.php', | ||
'tx_table_without_existing_tca_configuration_file' | ||
); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/configured_rule.php'; | ||
} | ||
|
||
private function assertThatConfigurationFileHasNewIgnorePageTypeRestriction( | ||
string $pathToConfigurationFile, | ||
string $tableName | ||
): void { | ||
$pathToConfigurationFile = sprintf($pathToConfigurationFile, $tableName); | ||
$contents = $this->filesystem->read($pathToConfigurationFile); | ||
$expectedFile = sprintf(__DIR__ . '/Assertions/%s.php.inc', $tableName); | ||
self::assertStringEqualsFile($expectedFile, $contents); | ||
} | ||
|
||
private function initializeFilesystem(): void | ||
{ | ||
$this->filesystem = $this->getContainer() | ||
->get(FilesystemInterface::class); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...r/v12/v0/MoveAllowTableOnStandardPagesToTCAConfigurationRector/config/configured_rule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Config\RectorConfig; | ||
use Ssch\TYPO3Rector\TYPO312\v0\MoveAllowTableOnStandardPagesToTCAConfigurationRector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->import(__DIR__ . '/../../../../../../config/config.php'); | ||
$rectorConfig->rule(MoveAllowTableOnStandardPagesToTCAConfigurationRector::class); | ||
}; |