[!!!][TASK] Use request type constants everywhere 79/45379/5
authorBenni Mack <benni@typo3.org>
Sat, 19 Dec 2015 22:07:31 +0000 (23:07 +0100)
committerWouter Wolters <typo3@wouterwolters.nl>
Wed, 30 Dec 2015 17:38:00 +0000 (18:38 +0100)
Each entry point (application) now defines the actual
request type at a very early point of the request, so
each call within TYPO3 can use the predefined constants
at all times.

Previously this was done within the Bootstrap after loading
LocalConfiguration.php etc, and by evaluating other
parameters to detect which RequestType should be used.

By directly setting the option within each application, the
constants "TYPO3_enterInstallScript" and "TYPO3_cliMode"
can be removed, as well as $GLOBALS['TYPO3_AJAX'].

Resolves: #72368
Releases: master
Change-Id: I5080e425d70cb6d4c9a9573dbc20216c93cd3332
Reviewed-on: https://review.typo3.org/45379
Reviewed-by: Morton Jonuschat <m.jonuschat@mojocode.de>
Tested-by: Morton Jonuschat <m.jonuschat@mojocode.de>
Reviewed-by: Wouter Wolters <typo3@wouterwolters.nl>
Tested-by: Wouter Wolters <typo3@wouterwolters.nl>
15 files changed:
typo3/sysext/backend/Classes/Console/Application.php
typo3/sysext/backend/Classes/Http/Application.php
typo3/sysext/core/Build/UnitTestsBootstrap.php
typo3/sysext/core/Classes/Core/Bootstrap.php
typo3/sysext/core/Classes/Core/SystemEnvironmentBuilder.php
typo3/sysext/core/Classes/FormProtection/FormProtectionFactory.php
typo3/sysext/core/Classes/Utility/GeneralUtility.php
typo3/sysext/core/Classes/Utility/PathUtility.php
typo3/sysext/core/Documentation/Changelog/master/Breaking-72368-TYPO3ConstantsRemoved.rst [new file with mode: 0644]
typo3/sysext/core/Tests/FunctionalTestCaseBootstrapUtility.php
typo3/sysext/extbase/Classes/Core/Bootstrap.php
typo3/sysext/extbase/Classes/Service/EnvironmentService.php
typo3/sysext/frontend/Classes/Http/Application.php
typo3/sysext/install/Classes/Http/Application.php
typo3/sysext/install/ext_tables.php

index 07d5281..8feb4b9 100644 (file)
@@ -53,6 +53,7 @@ class Application implements ApplicationInterface
 
         $this->bootstrap = Bootstrap::getInstance()
             ->initializeClassLoader($classLoader)
+            ->setRequestType(TYPO3_REQUESTTYPE_BE | TYPO3_REQUESTTYPE_CLI)
             ->baseSetup($this->entryPointPath);
 
         foreach ($this->availableRequestHandlers as $requestHandler) {
@@ -85,7 +86,6 @@ class Application implements ApplicationInterface
     protected function defineLegacyConstants()
     {
         define('TYPO3_MODE', 'BE');
-        define('TYPO3_cliMode', true);
     }
 
     /**
index fe33adf..a48eb39 100644 (file)
@@ -58,11 +58,9 @@ class Application implements ApplicationInterface
 
         $this->bootstrap = Bootstrap::getInstance()
             ->initializeClassLoader($classLoader)
+            ->setRequestType(TYPO3_REQUESTTYPE_BE | (!empty($_GET['ajaxID']) ? TYPO3_REQUESTTYPE_AJAX : 0))
             ->baseSetup($this->entryPointPath);
 
-        // can be done here after the base setup is done
-        $this->defineAdditionalEntryPointRelatedConstants();
-
         // Redirect to install tool if base configuration is not found
         if (!$this->bootstrap->checkIfEssentialConfigurationExists()) {
             $this->bootstrap->redirectToInstallTool($this->entryPointPath);
@@ -108,14 +106,4 @@ class Application implements ApplicationInterface
         define('TYPO3_MODE', 'BE');
     }
 
-    /**
-     * Define values that are based on the current script
-     */
-    protected function defineAdditionalEntryPointRelatedConstants()
-    {
-        // Activate "AJAX" handler when called with the GET variable ajaxID
-        if (!empty(GeneralUtility::_GET('ajaxID'))) {
-            $GLOBALS['TYPO3_AJAX'] = true;
-        }
-    }
 }
index b5f153c..d41d734 100644 (file)
@@ -122,7 +122,7 @@ class UnitTestsBootstrap
     }
 
     /**
-     * Defines TYPO3_MODE, TYPO3_cliMode and sets the environment variable TYPO3_CONTEXT.
+     * Defines TYPO3_MODE and sets the environment variable TYPO3_CONTEXT.
      *
      * @return UnitTestsBootstrap fluent interface
      */
@@ -131,7 +131,6 @@ class UnitTestsBootstrap
         /** @var string */
         define('TYPO3_MODE', 'BE');
         /** @var string */
-        define('TYPO3_cliMode', true);
         putenv('TYPO3_CONTEXT=Testing');
 
         return $this;
@@ -191,6 +190,7 @@ class UnitTestsBootstrap
 
         Bootstrap::getInstance()
             ->initializeClassLoader($classLoader)
+            ->setRequestType(TYPO3_REQUESTTYPE_BE | TYPO3_REQUESTTYPE_CLI)
             ->baseSetup();
 
         return $this;
index 068f7c2..9313ce9 100644 (file)
@@ -116,6 +116,7 @@ class Bootstrap
         if (is_null(static::$instance)) {
             $applicationContext = getenv('TYPO3_CONTEXT') ?: (getenv('REDIRECT_TYPO3_CONTEXT') ?: 'Production');
             self::$instance = new static($applicationContext);
+            self::$instance->defineTypo3RequestTypes();
         }
         return static::$instance;
     }
@@ -185,10 +186,14 @@ class Bootstrap
      *
      * @param string $relativePathPart Relative path of entry script back to document root
      * @return Bootstrap
+     * @throws \RuntimeException when TYPO3_REQUESTTYPE was not set before, setRequestType() needs to be called before
      * @internal This is not a public API method, do not use in own extensions
      */
     public function baseSetup($relativePathPart = '')
     {
+        if (!defined('TYPO3_REQUESTTYPE')) {
+            throw new \RuntimeException('No Request Type was set, TYPO3 does not know in which context it is run.', 1450561838);
+        }
         SystemEnvironmentBuilder::run($relativePathPart);
         if (!self::$usesComposerClassLoading && ClassLoadingInformation::isClassLoadingInformationAvailable()) {
             ClassLoadingInformation::registerClassLoadingInformation();
@@ -396,8 +401,7 @@ class Bootstrap
             ->setDefaultTimezone()
             ->initializeL10nLocales()
             ->convertPageNotFoundHandlingToBoolean()
-            ->setMemoryLimit()
-            ->defineTypo3RequestTypes();
+            ->setMemoryLimit();
         if ($allowCaching) {
             $this->ensureClassLoadingInformationExists();
         }
@@ -717,8 +721,8 @@ class Bootstrap
     }
 
     /**
-     * Define TYPO3_REQUESTTYPE* constants
-     * so devs exactly know what type of request it is
+     * Define TYPO3_REQUESTTYPE* constants that can be used for developers to see if any context has been hit
+     * also see setRequestType(). Is done at the very beginning so these parameters are always available.
      *
      * @return Bootstrap
      */
@@ -729,7 +733,19 @@ class Bootstrap
         define('TYPO3_REQUESTTYPE_CLI', 4);
         define('TYPO3_REQUESTTYPE_AJAX', 8);
         define('TYPO3_REQUESTTYPE_INSTALL', 16);
-        define('TYPO3_REQUESTTYPE', (TYPO3_MODE == 'FE' ? TYPO3_REQUESTTYPE_FE : 0) | (TYPO3_MODE == 'BE' ? TYPO3_REQUESTTYPE_BE : 0) | (defined('TYPO3_cliMode') && TYPO3_cliMode ? TYPO3_REQUESTTYPE_CLI : 0) | (defined('TYPO3_enterInstallScript') && TYPO3_enterInstallScript ? TYPO3_REQUESTTYPE_INSTALL : 0) | ($GLOBALS['TYPO3_AJAX'] ? TYPO3_REQUESTTYPE_AJAX : 0));
+    }
+
+    /**
+     * Defines the TYPO3_REQUESTTYPE constant so the environment knows which context the request is running.
+     *
+     * @throws \RuntimeException if the method was already called during a request
+     * @return Bootstrap
+     */
+    public function setRequestType($requestType) {
+        if (defined('TYPO3_REQUESTTYPE')) {
+            throw new \RuntimeException('TYPO3_REQUESTTYPE has already been set, cannot be called multiple times', 1450561878);
+        }
+        define('TYPO3_REQUESTTYPE', $requestType);
         return $this;
     }
 
@@ -778,7 +794,6 @@ class Bootstrap
         unset($GLOBALS['TBE_MODULES_EXT']);
         unset($GLOBALS['TCA_DESCR']);
         unset($GLOBALS['LOCAL_LANG']);
-        unset($GLOBALS['TYPO3_AJAX']);
         return $this;
     }
 
index e41fc44..cd74bb0 100644 (file)
@@ -276,7 +276,7 @@ class SystemEnvironmentBuilder
      */
     protected static function getPathThisScript()
     {
-        if (defined('TYPO3_cliMode') && TYPO3_cliMode === true) {
+        if (TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_CLI) {
             return self::getPathThisScriptCli();
         } else {
             return self::getPathThisScriptNonCli();
index 56b2e60..0294fd4 100644 (file)
@@ -125,7 +125,7 @@ class FormProtectionFactory
      */
     protected static function isInstallToolSession()
     {
-        return defined('TYPO3_enterInstallScript') && TYPO3_enterInstallScript;
+        return (TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_INSTALL);
     }
 
     /**
index efe09ec..0c8edcc 100755 (executable)
@@ -3447,7 +3447,7 @@ class GeneralUtility
         $host = '';
         // If not called from the command-line, resolve on getIndpEnv()
         // Note that TYPO3_REQUESTTYPE is not used here as it may not yet be defined
-        if ($requestHost && (!defined('TYPO3_cliMode') || !TYPO3_cliMode)) {
+        if ($requestHost && !(TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_CLI)) {
             $host = self::getIndpEnv('HTTP_HOST');
         }
         if (!$host) {
@@ -4515,7 +4515,7 @@ class GeneralUtility
     {
         // For CLI logging name is <fqdn-hostname>:<TYPO3-path>
         // Note that TYPO3_REQUESTTYPE is not used here as it may not yet be defined
-        if (defined('TYPO3_cliMode') && TYPO3_cliMode) {
+        if (TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_CLI) {
             $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_div.php']['systemLogHost'] = self::getHostname(($requestHost = false)) . ':' . PATH_site;
         } else {
             $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_div.php']['systemLogHost'] = self::getIndpEnv('TYPO3_SITE_URL');
index dc4ec3a..7faf067 100644 (file)
@@ -42,7 +42,7 @@ class PathUtility
         if (self::isAbsolutePath($targetPath)) {
             if (StringUtility::beginsWith($targetPath, PATH_site)) {
                 $targetPath = self::stripPathSitePrefix($targetPath);
-                if (!defined('TYPO3_cliMode')) {
+                if (!(TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_CLI)) {
                     $targetPath = GeneralUtility::getIndpEnv('TYPO3_SITE_PATH') . $targetPath;
                 }
             }
@@ -52,7 +52,7 @@ class PathUtility
             // Make an absolute path out of it
             $targetPath = GeneralUtility::resolveBackPath(dirname(PATH_thisScript) . '/' . $targetPath);
             $targetPath = self::stripPathSitePrefix($targetPath);
-            if (!defined('TYPO3_cliMode')) {
+            if (!(TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_CLI)) {
                 $targetPath = GeneralUtility::getIndpEnv('TYPO3_SITE_PATH') . $targetPath;
             }
         }
diff --git a/typo3/sysext/core/Documentation/Changelog/master/Breaking-72368-TYPO3ConstantsRemoved.rst b/typo3/sysext/core/Documentation/Changelog/master/Breaking-72368-TYPO3ConstantsRemoved.rst
new file mode 100644 (file)
index 0000000..58be2d0
--- /dev/null
@@ -0,0 +1,30 @@
+==========================================
+Breaking: #72368 - TYPO3 Constants removed
+==========================================
+
+Description
+===========
+
+The PHP constants ``TYPO3_enterInstallScript`` and ``TYPO3_cliMode`` and the global variable ``$GLOBALS['TYPO3_AJAX']`` which were used when a TYPO3
+Request was initialized are removed. They have been replaced by an alternative to use the TYPO3_REQUESTTYPE constant at the very beginning of each
+TYPO3 request.
+
+
+Impact
+======
+
+Checking for the mentioned constants and global variable have no effect anymore and will lead to unexpected behaviour.
+
+If not checked if the constant even was defined, the application will stop immediately.
+
+
+Affected Installations
+======================
+
+Any installation which uses a third-party extension using these constants.
+
+
+Migration
+=========
+
+Use ``TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_CLI`` or ``TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_INSTALL`` instead.
index ca5b493..0e0a6d4 100644 (file)
@@ -460,11 +460,11 @@ class FunctionalTestCaseBootstrapUtility
         $_SERVER['argv'][0] = 'index.php';
 
         define('TYPO3_MODE', 'BE');
-        define('TYPO3_cliMode', true);
 
         $classLoader = require rtrim(realpath($this->instancePath . '/typo3'), '\\/') . '/../vendor/autoload.php';
         \TYPO3\CMS\Core\Core\Bootstrap::getInstance()
             ->initializeClassLoader($classLoader)
+            ->setRequestType(TYPO3_REQUESTTYPE_BE | TYPO3_REQUESTTYPE_CLI)
             ->baseSetup('')
             ->loadConfigurationAndInitialize(true)
             ->loadTypo3LoadedExtAndExtLocalconf(true)
index d6e7785..1c68824 100644 (file)
@@ -238,6 +238,6 @@ class Bootstrap implements \TYPO3\CMS\Extbase\Core\BootstrapInterface
      */
     protected function isInCliMode()
     {
-        return (defined('TYPO3_cliMode') && TYPO3_cliMode);
+        return (TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_CLI);
     }
 }
index ce3982d..c402c6c 100644 (file)
@@ -46,7 +46,7 @@ class EnvironmentService implements \TYPO3\CMS\Core\SingletonInterface
      */
     public function isEnvironmentInCliMode()
     {
-        return $this->isEnvironmentInBackendMode() && defined('TYPO3_cliMode') && TYPO3_cliMode === true;
+        return TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_CLI;
     }
 
     /**
index c086297..1e2eb9f 100644 (file)
@@ -53,6 +53,7 @@ class Application implements ApplicationInterface
 
         $this->bootstrap = Bootstrap::getInstance()
             ->initializeClassLoader($classLoader)
+            ->setRequestType(TYPO3_REQUESTTYPE_FE)
             ->baseSetup($this->entryPointPath);
 
         // Redirect to install tool if base configuration is not found
index c43d436..a2c058e 100644 (file)
@@ -50,6 +50,7 @@ class Application implements ApplicationInterface
 
         $this->bootstrap = Bootstrap::getInstance()
             ->initializeClassLoader($classLoader)
+            ->setRequestType(TYPO3_REQUESTTYPE_BE | TYPO3_REQUESTTYPE_INSTALL)
             ->baseSetup($this->entryPointPath);
 
         foreach ($this->availableRequestHandlers as $requestHandler) {
@@ -86,6 +87,5 @@ class Application implements ApplicationInterface
     protected function defineLegacyConstants()
     {
         define('TYPO3_MODE', 'BE');
-        define('TYPO3_enterInstallScript', '1');
     }
 }
index d7c7345..4f2ba51 100644 (file)
@@ -7,7 +7,7 @@ if (TYPO3_MODE === 'BE') {
     $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['reports']['tx_reports']['status']['providers']['security'][] = \TYPO3\CMS\Install\Report\SecurityStatusReport::class;
 
     // Only add the environment status report if not in CLI mode
-    if (!defined('TYPO3_cliMode') || !TYPO3_cliMode) {
+    if (TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_CLI) {
         $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['reports']['tx_reports']['status']['providers']['system'][] = \TYPO3\CMS\Install\Report\EnvironmentStatusReport::class;
     }