Skip to content

Commit

Permalink
[TASK] Use the new ExtensionConfiguration API (#1649)
Browse files Browse the repository at this point in the history
Resolves: #925
  • Loading branch information
sabbelasichon authored Nov 24, 2020
1 parent d59f230 commit 8c97a7d
Show file tree
Hide file tree
Showing 8 changed files with 222 additions and 1 deletion.
3 changes: 3 additions & 0 deletions config/v9/typo3-90.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Ssch\TYPO3Rector\Rector\v9\v0\ReplaceAnnotationRector;
use Ssch\TYPO3Rector\Rector\v9\v0\SubstituteCacheWrapperMethodsRector;
use Ssch\TYPO3Rector\Rector\v9\v0\SubstituteConstantParsetimeStartRector;
use Ssch\TYPO3Rector\Rector\v9\v0\UseExtensionConfigurationApiRector;
use Ssch\TYPO3Rector\Rector\v9\v0\UseLogMethodInsteadOfNewLog2Rector;
use Ssch\TYPO3Rector\Rector\v9\v0\UseNewComponentIdForPageTreeRector;
use Ssch\TYPO3Rector\Rector\v9\v0\UseRenderingContextGetControllerContextRector;
Expand Down Expand Up @@ -94,4 +95,6 @@
$services->set(UseNewComponentIdForPageTreeRector::class);

$services->set(RefactorBackendUtilityGetPagesTSconfigRector::class);

$services->set(UseExtensionConfigurationApiRector::class);
};
2 changes: 1 addition & 1 deletion src/Helper/Typo3NodeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ final class Typo3NodeResolver
/**
* @var string
*/
private const GLOBALS = 'GLOBALS';
public const GLOBALS = 'GLOBALS';

public function isMethodCallOnGlobals(Node $node, string $methodCall, string $global): bool
{
Expand Down
134 changes: 134 additions & 0 deletions src/Rector/v9/v0/UseExtensionConfigurationApiRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<?php

declare(strict_types=1);

namespace Ssch\TYPO3Rector\Rector\v9\v0;

use PhpParser\Node;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\FuncCall;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\RectorDefinition\CodeSample;
use Rector\Core\RectorDefinition\RectorDefinition;
use Ssch\TYPO3Rector\Helper\Typo3NodeResolver;
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
use TYPO3\CMS\Core\Utility\GeneralUtility;

/**
* @see https://docs.typo3.org/c/typo3/cms-core/master/en-us/Changelog/9.0/Deprecation-82254-DeprecateGLOBALSTYPO3_CONF_VARSEXTextConf.html
*/
final class UseExtensionConfigurationApiRector extends AbstractRector
{
public function getNodeTypes(): array
{
return [FuncCall::class];
}

/**
* @param FuncCall $node
*/
public function refactor(Node $node): ?Node
{
if (! $this->isName($node->name, 'unserialize')) {
return null;
}

$firstArgument = array_shift($node->args);

if (null === $firstArgument) {
return null;
}

if (! $firstArgument->value instanceof ArrayDimFetch) {
return null;
}

$extensionConfiguration = $firstArgument->value;

if ($this->shouldSkip($extensionConfiguration)) {
return null;
}

return $this->createMethodCall($this->createStaticCall(GeneralUtility::class, 'makeInstance', [
$this->createClassConstantReference(ExtensionConfiguration::class),
]), 'get', [$extensionConfiguration->dim]);
}

/**
* @codeCoverageIgnore
*/
public function getDefinition(): RectorDefinition
{
return new RectorDefinition(
'Use the new ExtensionConfiguration API instead of $GLOBALS[\'TYPO3_CONF_VARS\'][\'EXT\'][\'extConf\'][\'foo\']',
[
new CodeSample(<<<'PHP'
$extensionConfiguration2 = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['foo'], ['allowed_classes' => false]);
PHP
, <<<'PHP'
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
use TYPO3\CMS\Core\Utility\GeneralUtility;
$extensionConfiguration2 = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get('foo');
PHP
),

]);
}

private function shouldSkip(ArrayDimFetch $node): bool
{
$extConf = $node->var;
if (! $extConf instanceof ArrayDimFetch) {
return true;
}

if (null === $extConf->dim) {
return true;
}

if (! $this->isValue($extConf->dim, 'extConf')) {
return true;
}

if (! property_exists($node->var, 'var')) {
return true;
}

$ext = $node->var->var;
if (! $ext instanceof ArrayDimFetch) {
return true;
}

if (null === $ext->dim) {
return true;
}

if (! $this->isValue($ext->dim, 'EXT')) {
return true;
}

$typo3ConfVars = $node->var->var->var;
if (! $typo3ConfVars instanceof ArrayDimFetch) {
return true;
}

if (null === $typo3ConfVars->dim) {
return true;
}

if (! $this->isValue($typo3ConfVars->dim, 'TYPO3_CONF_VARS')) {
return true;
}

$globals = $node->var->var->var->var;
if (! $this->isName($globals, Typo3NodeResolver::GLOBALS)) {
return true;
}

if (null === $node->dim) {
return true;
}

return ! $this->isName($node->dim, '_EXTKEY') && null === $this->getValue($node->dim);
}
}
25 changes: 25 additions & 0 deletions stubs/Core/Configuration/ExtensionConfiguration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
declare(strict_types=1);

namespace TYPO3\CMS\Core\Configuration;


if (class_exists(ExtensionConfiguration::class)) {
return;
}

final class ExtensionConfiguration
{
/**
* @return array
*/
public function get(string $extension, string $path = '')
{
return [];
}

public function set(string $extension, string $path = '', $value = null): void
{
}

}
4 changes: 4 additions & 0 deletions stubs/Core/Utility/ExtensionManagementUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,8 @@ public static function configureModule($moduleSignature, $modulePath): array
public static function loadExtLocalconf(bool $false): void
{
}

public static function extRelPath(string $key): void
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

$extensionConfiguration2 = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['foo'], ['allowed_classes' => false]);

$_EXTKEY = 'foo';
call_user_func(static function () use($_EXTKEY) {
$_EXTCONF = isset($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf'][$_EXTKEY]) ? unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf'][$_EXTKEY], ['allowed_classes' => false]) : [];
});

?>
-----
<?php

use TYPO3\CMS\Core\Utility\GeneralUtility;

use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
$extensionConfiguration2 = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get('foo');
$_EXTKEY = 'foo';
call_user_func(static function () use($_EXTKEY) {
$_EXTCONF = isset($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf'][$_EXTKEY]) ? GeneralUtility::makeInstance(ExtensionConfiguration::class)->get($_EXTKEY) : [];
});

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Ssch\TYPO3Rector\Tests\Rector\v9\v0\UseExtensionConfigurationApi;

use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Ssch\TYPO3Rector\Rector\v9\v0\UseExtensionConfigurationApiRector;
use Symplify\SmartFileSystem\SmartFileInfo;

final class UseExtensionConfigurationApiRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideDataForTest()
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->doTestFileInfo($fileInfo);
}

public function provideDataForTest(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

protected function getRectorClass(): string
{
return UseExtensionConfigurationApiRector::class;
}
}
1 change: 1 addition & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
$GLOBALS['TYPO3_CONF_VARS']['DB']['Connections']['Default']['password'] = 'password';
$GLOBALS['TYPO3_CONF_VARS']['DB']['Connections']['Default']['host'] = 'host';
$GLOBALS['TYPO3_CONF_VARS']['BE']['fileDenyPattern'] = FileNameValidator::DEFAULT_FILE_DENY_PATTERN;
$GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['foo'] = 'a:6:{s:9:"loginLogo";s:8:"logo.jpg";s:19:"loginHighlightColor";s:7:"#000000";s:20:"loginBackgroundImage";s:8:"logo.jpg";s:13:"loginFootnote";s:8:"Footnote";s:11:"backendLogo";s:0:"";s:14:"backendFavicon";s:0:"";}';

define('TYPO3_MODE', 'BE');
define('TYPO3_URL_MAILINGLISTS', 'http://lists.typo3.org/cgi-bin/mailman/listinfo');
Expand Down

0 comments on commit 8c97a7d

Please sign in to comment.