Skip to content

Commit

Permalink
[FEATURE] MoveAllowTableOnStandardPagesToTCAConfigurationRector (#4161)
Browse files Browse the repository at this point in the history
  • Loading branch information
sabbelasichon authored Apr 9, 2024
1 parent da56ecf commit be6e684
Show file tree
Hide file tree
Showing 13 changed files with 292 additions and 1 deletion.
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
},
"require": {
"php": "^7.4 || ^8.0",
"league/flysystem": "^2.0",
"league/flysystem-memory": "^2.0",
"nette/utils": "^3.2.10 || ^4.0.4",
"nikic/php-parser": "^4.18.0",
"phpstan/phpstan": "^1.10.56",
Expand Down
17 changes: 17 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@

declare(strict_types=1);

use League\Flysystem\Filesystem;
use League\Flysystem\InMemory\InMemoryFilesystemAdapter;
use League\Flysystem\Local\LocalFilesystemAdapter;
use Rector\Config\RectorConfig;
use Rector\Testing\PHPUnit\StaticPHPUnitEnvironment;
use Ssch\TYPO3Rector\Configuration\Typo3Option;
use Ssch\TYPO3Rector\Contract\FilesystemInterface;
use Ssch\TYPO3Rector\Filesystem\FileInfoFactory;
use Ssch\TYPO3Rector\Filesystem\FilesFinder;
use Ssch\TYPO3Rector\Filesystem\FlysystemFilesystem;
use Ssch\TYPO3Rector\NodeAnalyzer\ExtbaseControllerRedirectAnalyzer;
use Ssch\TYPO3Rector\NodeFactory\InjectMethodFactory;
use Ssch\TYPO3Rector\NodeFactory\Typo3GlobalsFactory;
Expand All @@ -23,4 +29,15 @@
$rectorConfig->singleton(InjectMethodFactory::class);
$rectorConfig->singleton(Typo3GlobalsFactory::class);
$rectorConfig->singleton(Typo3NodeResolver::class);
$rectorConfig->bind(FilesystemInterface::class, static function () {
$argv = $_SERVER['argv'] ?? [];
$isDryRun = in_array('--dry-run', $argv) || in_array('-n', $argv);
if (StaticPHPUnitEnvironment::isPHPUnitRun() || $isDryRun) {
$adapter = new InMemoryFilesystemAdapter();
} else {
$adapter = new LocalFilesystemAdapter('/');
}

return new FlysystemFilesystem(new Filesystem($adapter));
});
};
2 changes: 2 additions & 0 deletions config/v12/typo3-120.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Ssch\TYPO3Rector\TYPO312\v0\ImplementSiteLanguageAwareInterfaceRector;
use Ssch\TYPO3Rector\TYPO312\v0\MigrateContentObjectRendererLastTypoLinkPropertiesRector;
use Ssch\TYPO3Rector\TYPO312\v0\MigrateQueryBuilderExecuteRector;
use Ssch\TYPO3Rector\TYPO312\v0\MoveAllowTableOnStandardPagesToTCAConfigurationRector;
use Ssch\TYPO3Rector\TYPO312\v0\RemoveMailerAdapterInterfaceRector;
use Ssch\TYPO3Rector\TYPO312\v0\RemoveRelativeToCurrentScriptArgumentsRector;
use Ssch\TYPO3Rector\TYPO312\v0\RemoveTSFEConvOutputCharsetCallsRector;
Expand Down Expand Up @@ -160,4 +161,5 @@
$rectorConfig->rule(MigrateContentObjectRendererLastTypoLinkPropertiesRector::class);
$rectorConfig->rule(UsePageDoktypeRegistryRector::class);
$rectorConfig->rule(UseServerRequestInsteadOfGeneralUtilityPostRector::class);
$rectorConfig->rule(MoveAllowTableOnStandardPagesToTCAConfigurationRector::class);
};
2 changes: 1 addition & 1 deletion ecs.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
// on php 8.1, it adds space on &$variable
__DIR__ . '/utils/generator/templates/rules',
AssignmentInConditionSniff::class,
DeclareStrictTypesFixer::class => ['*/Fixture/*'],
DeclareStrictTypesFixer::class => ['*/Fixture/*', '*/Assertions/*'],
]);

$ecsConfig->rule(DeclareStrictTypesFixer::class);
Expand Down
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ parameters:
excludePaths:
- "*/Expected/*"
- '*tests/*/Fixture/*'
- '*tests/*/Assertions/*'
- '*tests/*/Source/*'
- '*tests/Source/*'
- 'utils/generator/templates/*'
Expand Down
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
);
}
}
14 changes: 14 additions & 0 deletions src/Contract/FilesystemInterface.php
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;
}
36 changes: 36 additions & 0 deletions src/Filesystem/FlysystemFilesystem.php
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);
}
}
5 changes: 5 additions & 0 deletions stubs/TYPO3/CMS/Core/Utility/ExtensionManagementUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,9 @@ public static function getFileFieldTCAConfig($fieldName, array $customSettingOve
public static function addTCAcolumns($string, array $columns) {

}

public static function allowTableOnStandardPages(string $table)
{

}
}
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;
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;

?>
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);
}
}
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);
};

0 comments on commit be6e684

Please sign in to comment.