From 6db9a9dccdfb5673449e77d2412f2c842ca79df3 Mon Sep 17 00:00:00 2001 From: Daniel Gohlke <daniel.gohlke@extco.de> Date: Fri, 21 Feb 2020 23:31:30 +0100 Subject: [PATCH] [BUGFIX] Return class alias respecting namespace The resolveControllerAliasFromControllerClassName returns an alias respecting subfolders in controller namespace to avoid same aliases for different controllers and to load the templates from corresponding directories. Resolves: #90480 Releases: master Change-Id: I3ed691e126579a4cfcc35f9589beb017b06a2f82 Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/63361 Tested-by: TYPO3com <noreply@typo3.com> Tested-by: Susanne Moog <look@susi.dev> Tested-by: Alexander Schnitzler <git@alexanderschnitzler.de> Reviewed-by: Susanne Moog <look@susi.dev> Reviewed-by: Alexander Schnitzler <git@alexanderschnitzler.de> --- .../Classes/Utility/ExtensionUtility.php | 42 ++++++++++++++++--- .../Unit/Utility/ExtensionUtilityTest.php | 4 ++ 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/typo3/sysext/extbase/Classes/Utility/ExtensionUtility.php b/typo3/sysext/extbase/Classes/Utility/ExtensionUtility.php index ec6ebe9f38e5..189767c79cf2 100644 --- a/typo3/sysext/extbase/Classes/Utility/ExtensionUtility.php +++ b/typo3/sysext/extbase/Classes/Utility/ExtensionUtility.php @@ -14,6 +14,8 @@ namespace TYPO3\CMS\Extbase\Utility; * The TYPO3 project - inspiring people to share! */ +use TYPO3\CMS\Core\Utility\StringUtility; + /** * Utilities to manage plugins and modules of an extension. Also useful to auto-generate the autoloader registry * file ext_autoload.php. @@ -310,15 +312,43 @@ tt_content.' . $pluginSignature . ' { */ public static function resolveControllerAliasFromControllerClassName(string $controllerClassName): string { - if (strrpos($controllerClassName, 'Controller') === false) { + // This method has been adjusted for TYPO3 10.3 to mitigate the issue that controller aliases + // could not longer be calculated from controller classes when calling + // \TYPO3\CMS\Extbase\Utility\ExtensionUtility::configurePlugin(). + // + // The idea for version 11 is to let the user choose a controller alias and to check for its + // uniqueness per plugin. That way, the core does no longer rely on the namespace of + // controller classes to be in a specific format. + // + // todo: Change the way plugins are registered and enforce a controller alias to be set by + // the user to also free the core from guessing a simple alias by looking at the + // class name. This makes it possible to choose controller class names without a + // controller suffix. + + $strLen = strlen('Controller'); + + if (!StringUtility::endsWith($controllerClassName, 'Controller')) { return ''; } - return trim(substr( - $controllerClassName, - (int)strrpos($controllerClassName, '\\'), - -strlen('Controller') - ), '\\'); + $controllerClassNameWithoutControllerSuffix = substr($controllerClassName, 0, -$strLen); + + if (strrpos($controllerClassNameWithoutControllerSuffix, 'Controller\\') === false) { + $positionOfLastSlash = (int)strrpos($controllerClassNameWithoutControllerSuffix, '\\'); + $positionOfLastSlash += $positionOfLastSlash === 0 ? 0 : 1; + + return substr($controllerClassNameWithoutControllerSuffix, $positionOfLastSlash); + } + + $positionOfControllerNamespacePart = (int)strrpos( + $controllerClassNameWithoutControllerSuffix, + 'Controller\\' + ); + + return substr( + $controllerClassNameWithoutControllerSuffix, + $positionOfControllerNamespacePart + $strLen + 1 + ); } /** diff --git a/typo3/sysext/extbase/Tests/Unit/Utility/ExtensionUtilityTest.php b/typo3/sysext/extbase/Tests/Unit/Utility/ExtensionUtilityTest.php index 9cd4b5a8b799..a3685ae529b3 100644 --- a/typo3/sysext/extbase/Tests/Unit/Utility/ExtensionUtilityTest.php +++ b/typo3/sysext/extbase/Tests/Unit/Utility/ExtensionUtilityTest.php @@ -480,6 +480,10 @@ class ExtensionUtilityTest extends UnitTestCase '', 'Foo\\Bar\\baz\\qUx\\Foocontroller', ], + 'Controller in arbitrary namespace with subfolder in Controller namespace' => [ + 'Baz\\Foo', + 'Foo\\Bar\\Controller\\Baz\\FooController', + ], ]; } -- GitLab