[BUGFIX] Do not depend on global env to define cli mode 16/57816/15
authorHelmut Hummel <typo3@helhum.io>
Sun, 5 Aug 2018 10:42:50 +0000 (12:42 +0200)
committerChristian Kuhn <lolli@schwarzbu.ch>
Tue, 21 Aug 2018 08:42:20 +0000 (10:42 +0200)
Instead of depending on a global environment variable
to set the TYPO3 environment in cli mode, we inject the mode
properly from within the entry point.

We now move the code to initialize the environment
to SystemEnvironmentBuilder:run() where we can evaluate
the request type passed to this method, to properly
set cli or non cli mode.

Depending on a global env to set the TYPO3 environment
would require hacks, like changing the global env to
properly select the cli mode (currently done in testing framework).
Using an application sub context for that purpose isn't ideal either,
because at best we would need to also test for magic contexts
like "Production/Frontend" or "Development/Frontend" and at worst
it conflicts with user defined contexts. It wouldn't be possible
to define a sub context and still properly start a frontend application
within this context.

composer req --dev typo3/testing-framework ~4.4.1

Resolves: #85757
Releases: master
Change-Id: Ia64d358e731d9ddf717497ae120acb009e0f26fa
Reviewed-on: https://review.typo3.org/57816
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Benni Mack <benni@typo3.org>
Tested-by: Benni Mack <benni@typo3.org>
Reviewed-by: Daniel Goerz <daniel.goerz@posteo.de>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
composer.json
composer.lock
typo3/sysext/core/Classes/Core/Bootstrap.php
typo3/sysext/core/Classes/Core/SystemEnvironmentBuilder.php
typo3/sysext/core/composer.json

index dc0a3f3..80fa7bc 100644 (file)
@@ -68,7 +68,7 @@
                "fiunchinho/phpunit-randomizer": "^4.0",
                "friendsofphp/php-cs-fixer": "^2.12.2",
                "typo3/cms-styleguide": "~9.2.0",
-               "typo3/testing-framework": "~4.4.0"
+               "typo3/testing-framework": "~4.4.1"
        },
        "suggest": {
                "ext-gd": "GDlib/Freetype is required for building images with text (GIFBUILDER) and can also be used to scale images",
index 0a764f7..c159a6f 100644 (file)
@@ -4,7 +4,7 @@
         "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
         "This file is @generated automatically"
     ],
-    "content-hash": "58c52e49b6f1fda5147175014600f5cc",
+    "content-hash": "05058f1ae500b8d7826e1b616caaf3ed",
     "packages": [
         {
             "name": "cogpowered/finediff",
         },
         {
             "name": "typo3/testing-framework",
-            "version": "4.4.0",
+            "version": "4.4.1",
             "source": {
                 "type": "git",
                 "url": "https://github.com/TYPO3/testing-framework.git",
-                "reference": "45b4b0bc28382e03f5c7738d5417ee1ee848034f"
+                "reference": "b0a8b49939817cbbd14e67e98b1c6c9fc34a68da"
             },
             "dist": {
                 "type": "zip",
-                "url": "https://api.github.com/repos/TYPO3/testing-framework/zipball/45b4b0bc28382e03f5c7738d5417ee1ee848034f",
-                "reference": "45b4b0bc28382e03f5c7738d5417ee1ee848034f",
+                "url": "https://api.github.com/repos/TYPO3/testing-framework/zipball/b0a8b49939817cbbd14e67e98b1c6c9fc34a68da",
+                "reference": "b0a8b49939817cbbd14e67e98b1c6c9fc34a68da",
                 "shasum": ""
             },
             "require": {
                 "tests",
                 "typo3"
             ],
-            "time": "2018-08-16T11:07:05+00:00"
+            "time": "2018-08-20T11:20:22+00:00"
         },
         {
             "name": "webmozart/assert",
index 0bfe475..336f6bc 100644 (file)
@@ -73,9 +73,6 @@ class Bootstrap
         bool $failsafe = false
     ): ContainerInterface {
         $requestId = substr(md5(uniqid('', true)), 0, 13);
-        $applicationContext = static::createApplicationContext();
-        SystemEnvironmentBuilder::initializeEnvironment($applicationContext);
-        GeneralUtility::presetApplicationContext($applicationContext);
 
         static::initializeClassLoader($classLoader);
         if (!Environment::isComposerMode() && ClassLoadingInformation::isClassLoadingInformationAvailable()) {
@@ -257,6 +254,7 @@ class Bootstrap
     /**
      * @return ApplicationContext
      * @internal This is not a public API method, do not use in own extensions
+     * @deprecated Will be removed with v10
      */
     public static function createApplicationContext(): ApplicationContext
     {
@@ -318,10 +316,7 @@ class Bootstrap
         }
         // @deprecated: remove this code block in TYPO3 v10.0
         if (GeneralUtility::getApplicationContext() === null) {
-            $applicationContext = static::createApplicationContext();
             SystemEnvironmentBuilder::run($entryPointLevel);
-            SystemEnvironmentBuilder::initializeEnvironment($applicationContext);
-            GeneralUtility::presetApplicationContext($applicationContext);
         }
         if (!Environment::isComposerMode() && ClassLoadingInformation::isClassLoadingInformationAvailable()) {
             ClassLoadingInformation::registerClassLoadingInformation();
index 5719ed1..9638fea 100644 (file)
@@ -86,11 +86,22 @@ class SystemEnvironmentBuilder
         self::defineTypo3RequestTypes();
         self::setRequestType($requestType | ($requestType === self::REQUESTTYPE_BE && strpos($_REQUEST['route'] ?? '', '/ajax/') === 0 ? TYPO3_REQUESTTYPE_AJAX : 0));
         self::defineLegacyConstants($requestType === self::REQUESTTYPE_FE ? 'FE' : 'BE');
-        self::definePaths($entryPointLevel);
+        self::definePaths($entryPointLevel, $requestType);
         self::checkMainPathsExist();
         self::initializeGlobalVariables();
         self::initializeGlobalTimeTrackingVariables();
         self::initializeBasicErrorReporting();
+
+        $applicationContext = static::createApplicationContext();
+        self::initializeEnvironment($applicationContext, $requestType);
+        GeneralUtility::presetApplicationContext($applicationContext);
+    }
+
+    protected static function createApplicationContext(): ApplicationContext
+    {
+        $applicationContext = getenv('TYPO3_CONTEXT') ?: (getenv('REDIRECT_TYPO3_CONTEXT') ?: 'Production');
+
+        return new ApplicationContext($applicationContext);
     }
 
     /**
@@ -171,10 +182,11 @@ class SystemEnvironmentBuilder
      * Calculate all required base paths and set as constants.
      *
      * @param int $entryPointLevel Number of subdirectories where the entry script is located under the document root
+     * @param int $requestType
      */
-    protected static function definePaths($entryPointLevel = 0)
+    protected static function definePaths($entryPointLevel = 0, int $requestType)
     {
-        $isCli = PHP_SAPI === 'cli' && getenv('TYPO3_CONTEXT') !== 'Testing/Frontend';
+        $isCli = self::isCliRequestType($requestType);
         // Absolute path of the entry script that was called
         $scriptPath = GeneralUtility::fixWindowsFilePath(self::getPathThisScript($isCli));
         $rootPath = self::getRootPathFromScriptPath($scriptPath, $entryPointLevel);
@@ -268,12 +280,10 @@ class SystemEnvironmentBuilder
      * Initialize the Environment class
      *
      * @param ApplicationContext $context
+     * @param int|null $requestType
      */
-    public static function initializeEnvironment(ApplicationContext $context)
+    public static function initializeEnvironment(ApplicationContext $context, int $requestType = null)
     {
-        // TYPO3_CONTEXT is set in testing framework to check for frontend calls
-        // forked from a CLI process
-        $isCli = PHP_SAPI === 'cli' && (string)$context !== 'Testing/Frontend';
         // Absolute path of the entry script that was called
         $scriptPath = PATH_thisScript;
         $sitePath = rtrim(PATH_site, '/');
@@ -293,7 +303,7 @@ class SystemEnvironmentBuilder
         $isDifferentRootPath = ($projectRootPath && $projectRootPath !== $sitePath);
         Environment::initialize(
             $context,
-            $isCli,
+            self::isCliRequestType($requestType),
             self::usesComposerClassLoading(),
             $isDifferentRootPath ? $projectRootPath : $sitePath,
             $sitePath,
@@ -536,4 +546,20 @@ class SystemEnvironmentBuilder
         }
         define('TYPO3_MODE', $mode);
     }
+
+    /**
+     * Checks if request type is cli.
+     * Falls back to check PHP_SAPI in case request type is not provided
+     *
+     * @param int|null $requestType
+     * @return bool
+     */
+    protected static function isCliRequestType(?int $requestType): bool
+    {
+        if ($requestType === null) {
+            $requestType = PHP_SAPI === 'cli' ? self::REQUESTTYPE_CLI : self::REQUESTTYPE_FE;
+        }
+
+        return ($requestType & self::REQUESTTYPE_CLI) === self::REQUESTTYPE_CLI;
+    }
 }
index 86767c7..1578a14 100644 (file)
@@ -50,7 +50,7 @@
                "fiunchinho/phpunit-randomizer": "^4.0",
                "friendsofphp/php-cs-fixer": "^2.12.2",
                "typo3/cms-styleguide": "~9.2.0",
-               "typo3/testing-framework": "~4.4.0"
+               "typo3/testing-framework": "~4.4.1"
        },
        "suggest": {
                "ext-fileinfo": "Used for proper file type detection in the file abstraction layer",