From cc4da7d9d310125ed702bbda1b83b1180de30d6f Mon Sep 17 00:00:00 2001
From: Benni Mack <benni@typo3.org>
Date: Fri, 23 Sep 2022 00:45:47 +0200
Subject: [PATCH] [TASK] Deprecate Environment::getBackendPath()

As the hard-coded "/typo3/" path might change later,
it is good to rely on the BackendEntryPointResolver
for future development.

Resolves: #99638
Releases: main
Change-Id: I9cf0eb52bcb22a4656eddccf72eb8b1456a458d7
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/75859
Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
---
 .../sysext/core/Classes/Core/Environment.php  |  2 +
 ...cation-99638-EnvironmentgetBackendPath.rst | 41 +++++++++++++++++++
 .../Tests/Unit/Utility/GeneralUtilityTest.php | 14 ++++---
 .../Php/MethodCallStaticMatcher.php           |  9 +++-
 4 files changed, 60 insertions(+), 6 deletions(-)
 create mode 100644 typo3/sysext/core/Documentation/Changelog/12.2/Deprecation-99638-EnvironmentgetBackendPath.rst

diff --git a/typo3/sysext/core/Classes/Core/Environment.php b/typo3/sysext/core/Classes/Core/Environment.php
index c065df09e6c3..4ee7e21d9d39 100644
--- a/typo3/sysext/core/Classes/Core/Environment.php
+++ b/typo3/sysext/core/Classes/Core/Environment.php
@@ -252,9 +252,11 @@ class Environment
     /**
      * Previously known as PATH_typo3
      * Please note that this might be gone at some point
+     * @deprecated you should not rely on this method, as the backend path might change.
      */
     public static function getBackendPath(): string
     {
+        trigger_error('Environment::getBackendPath() will be removed in TYPO3 v13.0.', E_USER_DEPRECATED);
         return self::getPublicPath() . '/typo3';
     }
 
diff --git a/typo3/sysext/core/Documentation/Changelog/12.2/Deprecation-99638-EnvironmentgetBackendPath.rst b/typo3/sysext/core/Documentation/Changelog/12.2/Deprecation-99638-EnvironmentgetBackendPath.rst
new file mode 100644
index 000000000000..fed6bf59dbc0
--- /dev/null
+++ b/typo3/sysext/core/Documentation/Changelog/12.2/Deprecation-99638-EnvironmentgetBackendPath.rst
@@ -0,0 +1,41 @@
+.. include:: /Includes.rst.txt
+
+.. _deprecation-99638-1674127318:
+
+===================================================
+Deprecation: #99638 - Environment::getBackendPath()
+===================================================
+
+See :issue:`99638`
+
+Description
+===========
+
+TYPO3's Backend path "/typo3" is currently resolved statically in :php:`Environment::getBackendPath()` to return the full path to the backend entrypoint.
+
+However, as TYPO3's code base is evolving, the usages to the hardcoded path have been reduced
+and the functionality is now migrated into a new :php:`BackendEntryPointResolver` class,
+which allows for dynamically adjusting the entry point in the future.
+
+
+Impact
+======
+
+Calling the method will trigger a PHP deprecation warning.
+
+
+Affected installations
+======================
+
+TYPO3 installations with custom extensions using the method in PHP code.
+
+
+Migration
+=========
+
+Check for the extension scanner, and see if the code is necessary, or if any alternative,
+such as the :php:`BackendEntryPointResolver` or :php:`NormalizedParams` might be better
+suited as third-party extensions should not rely on the hard-coded paths to resources from
+:file:`typo3/*` anymore.
+
+.. index:: PHP-API, FullyScanned, ext:core
\ No newline at end of file
diff --git a/typo3/sysext/core/Tests/Unit/Utility/GeneralUtilityTest.php b/typo3/sysext/core/Tests/Unit/Utility/GeneralUtilityTest.php
index 370284c9c40b..3497cdd642b1 100644
--- a/typo3/sysext/core/Tests/Unit/Utility/GeneralUtilityTest.php
+++ b/typo3/sysext/core/Tests/Unit/Utility/GeneralUtilityTest.php
@@ -1419,7 +1419,8 @@ class GeneralUtilityTest extends UnitTestCase
             Environment::getPublicPath(),
             Environment::getVarPath(),
             Environment::getConfigPath(),
-            Environment::getBackendPath() . '/index.php',
+            // needs to be a subpath in order to validate ".." references
+            Environment::getPublicPath() . '/typo3/index.php',
             Environment::isWindows() ? 'WINDOWS' : 'UNIX'
         );
         self::assertEquals($path, GeneralUtility::sanitizeLocalUrl($path));
@@ -1439,7 +1440,8 @@ class GeneralUtilityTest extends UnitTestCase
             Environment::getPublicPath(),
             Environment::getVarPath(),
             Environment::getConfigPath(),
-            Environment::getBackendPath() . '/index.php',
+            // needs to be a subpath in order to validate ".." references
+            Environment::getPublicPath() . '/typo3/index.php',
             Environment::isWindows() ? 'WINDOWS' : 'UNIX'
         );
         self::assertEquals(rawurlencode($path), GeneralUtility::sanitizeLocalUrl(rawurlencode($path)));
@@ -1490,7 +1492,8 @@ class GeneralUtilityTest extends UnitTestCase
             Environment::getPublicPath(),
             Environment::getVarPath(),
             Environment::getConfigPath(),
-            Environment::getBackendPath() . '/index.php',
+            // needs to be a subpath in order to validate ".." references
+            Environment::getPublicPath() . '/typo3/index.php',
             Environment::isWindows() ? 'WINDOWS' : 'UNIX'
         );
         $_SERVER['HTTP_HOST'] = $host;
@@ -1512,7 +1515,8 @@ class GeneralUtilityTest extends UnitTestCase
             Environment::getPublicPath(),
             Environment::getVarPath(),
             Environment::getConfigPath(),
-            Environment::getBackendPath() . '/index.php',
+            // needs to be a subpath in order to validate ".." references
+            Environment::getPublicPath() . '/typo3/index.php',
             Environment::isWindows() ? 'WINDOWS' : 'UNIX'
         );
         $_SERVER['HTTP_HOST'] = $host;
@@ -1553,7 +1557,7 @@ class GeneralUtilityTest extends UnitTestCase
             Environment::getPublicPath(),
             Environment::getVarPath(),
             Environment::getConfigPath(),
-            Environment::getBackendPath() . '/index.php',
+            Environment::getPublicPath() . '/typo3/index.php',
             Environment::isWindows() ? 'WINDOWS' : 'UNIX'
         );
         $_SERVER['HTTP_HOST'] = 'localhost';
diff --git a/typo3/sysext/install/Configuration/ExtensionScanner/Php/MethodCallStaticMatcher.php b/typo3/sysext/install/Configuration/ExtensionScanner/Php/MethodCallStaticMatcher.php
index f501ea803231..68c1d49742b6 100644
--- a/typo3/sysext/install/Configuration/ExtensionScanner/Php/MethodCallStaticMatcher.php
+++ b/typo3/sysext/install/Configuration/ExtensionScanner/Php/MethodCallStaticMatcher.php
@@ -1443,11 +1443,18 @@ return [
             'Deprecation-99579-BackendUtilityGetFuncCheck.rst',
         ],
     ],
-    'TYPO3\CMS\Backend\Utility\GeneralUtility::_GPmerged' => [
+    'TYPO3\CMS\Core\Utility\GeneralUtility::_GPmerged' => [
         'numberOfMandatoryArguments' => 1,
         'maximumNumberOfArguments' => 1,
         'restFiles' => [
             'Deprecation-99615-GeneralUtilityGPMerged.rst',
         ],
     ],
+    'TYPO3\CMS\Core\Core\Environment::getBackendPath' => [
+        'numberOfMandatoryArguments' => 0,
+        'maximumNumberOfArguments' => 0,
+        'restFiles' => [
+            'Deprecation-99638-EnvironmentgetBackendPath.rst',
+        ],
+    ],
 ];
-- 
GitLab