Commit 20ec656f authored by Benni Mack's avatar Benni Mack Committed by Susanne Moog
Browse files

[TASK] Use Environment API to fetch application context

The application context is stored in the Environment class since TYPO3 v9,
which is the correct place instead of GeneralUtility::getApplicationContext().

Resolves: #89631
Releases: master
Change-Id: I55de17ef3b9cbd0962a2c604913c736bd52ac6d3
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/62247


Tested-by: default avatarTYPO3com <noreply@typo3.com>
Tested-by: Georg Ringer's avatarGeorg Ringer <georg.ringer@gmail.com>
Tested-by: Susanne Moog's avatarSusanne Moog <look@susi.dev>
Reviewed-by: Georg Ringer's avatarGeorg Ringer <georg.ringer@gmail.com>
Reviewed-by: Susanne Moog's avatarSusanne Moog <look@susi.dev>
parent dd7893a1
......@@ -295,7 +295,7 @@ class SystemInformationToolbarItem implements ToolbarItemInterface
*/
protected function getApplicationContext()
{
$applicationContext = GeneralUtility::getApplicationContext();
$applicationContext = Environment::getContext();
$this->systemInformation[] = [
'title' => 'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:toolbarItems.sysinfo.applicationcontext',
'value' => (string)$applicationContext,
......
......@@ -16,6 +16,7 @@ namespace TYPO3\CMS\Core\Authentication;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Core\Cache\CacheManager;
use TYPO3\CMS\Core\Core\Environment;
use TYPO3\CMS\Core\Database\Connection;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder;
......@@ -464,7 +465,7 @@ class BackendUserAuthentication extends AbstractUserAuthentication
if ((int)$GLOBALS['BE_USER']->user['ses_backuserid'] !== 0) {
return false;
}
if (GeneralUtility::getApplicationContext()->isDevelopment() && $this->isAdmin()) {
if (Environment::getContext()->isDevelopment() && $this->isAdmin()) {
return true;
}
$systemMaintainers = $GLOBALS['TYPO3_CONF_VARS']['SYS']['systemMaintainers'] ?? [];
......
......@@ -26,6 +26,7 @@ use TYPO3\CMS\Core\Context\VisibilityAspect;
use TYPO3\CMS\Core\Context\WorkspaceAspect;
use TYPO3\CMS\Core\Core\ApplicationInterface;
use TYPO3\CMS\Core\Core\Bootstrap;
use TYPO3\CMS\Core\Core\Environment;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
......@@ -52,7 +53,7 @@ class CommandApplication implements ApplicationInterface
$this->application = new Application('TYPO3 CMS', sprintf(
'%s (Application Context: <comment>%s</comment>)',
TYPO3_version,
GeneralUtility::getApplicationContext()
Environment::getContext()
));
$this->application->setAutoExit(false);
}
......
......@@ -22,6 +22,7 @@ use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\ConsoleOutput;
use TYPO3\CMS\Core\Authentication\CommandLineUserAuthentication;
use TYPO3\CMS\Core\Core\Bootstrap;
use TYPO3\CMS\Core\Core\Environment;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
......@@ -46,7 +47,7 @@ class CommandRequestHandler implements RequestHandlerInterface
$this->application = new Application('TYPO3 CMS', sprintf(
'%s (Application Context: <comment>%s</comment>)',
TYPO3_version,
GeneralUtility::getApplicationContext()
Environment::getContext()
));
}
......
......@@ -214,7 +214,7 @@ class ClassLoadingInformation
*/
protected static function isTestingContext()
{
return GeneralUtility::getApplicationContext()->isTesting();
return Environment::getContext()->isTesting();
}
/**
......
......@@ -16,7 +16,7 @@ namespace TYPO3\CMS\Core\ExpressionLanguage;
* The TYPO3 project - inspiring people to share!
*/
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Core\Environment;
class SiteConditionProvider extends AbstractProvider
{
......@@ -27,7 +27,7 @@ class SiteConditionProvider extends AbstractProvider
$typo3->branch = TYPO3_branch;
$typo3->devIpMask = trim($GLOBALS['TYPO3_CONF_VARS']['SYS']['devIPmask']);
$this->expressionLanguageVariables = [
'applicationContext' => (string)GeneralUtility::getApplicationContext(),
'applicationContext' => (string)Environment::getContext(),
'typo3' => $typo3,
];
}
......
......@@ -16,6 +16,7 @@ namespace TYPO3\CMS\Core\ExpressionLanguage;
* The TYPO3 project - inspiring people to share!
*/
use TYPO3\CMS\Core\Core\Environment;
use TYPO3\CMS\Core\ExpressionLanguage\FunctionsProvider\Typo3ConditionFunctionsProvider;
use TYPO3\CMS\Core\Utility\GeneralUtility;
......@@ -34,7 +35,7 @@ class TypoScriptConditionProvider extends AbstractProvider
$typo3->devIpMask = trim($GLOBALS['TYPO3_CONF_VARS']['SYS']['devIPmask']);
$this->expressionLanguageVariables = [
'request' => GeneralUtility::makeInstance(RequestWrapper::class, $GLOBALS['TYPO3_REQUEST'] ?? null),
'applicationContext' => (string)GeneralUtility::getApplicationContext(),
'applicationContext' => (string)Environment::getContext(),
'typo3' => $typo3,
];
$this->expressionLanguageProviders = [
......
......@@ -1291,7 +1291,7 @@ class PageRenderer implements SingletonInterface
}
$packages = GeneralUtility::makeInstance(PackageManager::class)->getActivePackages();
$isDevelopment = GeneralUtility::getApplicationContext()->isDevelopment();
$isDevelopment = Environment::getContext()->isDevelopment();
$cacheIdentifier = 'requireJS_' . md5(implode(',', array_keys($packages)) . ($isDevelopment ? ':dev' : '') . GeneralUtility::getIndpEnv('TYPO3_REQUEST_SCRIPT'));
/** @var FrontendInterface $cache */
$cache = static::$cache ?? GeneralUtility::makeInstance(CacheManager::class)->getCache('assets');
......
......@@ -20,7 +20,6 @@ use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerInterface;
use TYPO3\CMS\Core\Cache\CacheManager;
use TYPO3\CMS\Core\Core\ApplicationContext;
use TYPO3\CMS\Core\Core\Bootstrap;
use TYPO3\CMS\Core\Core\ClassLoadingInformation;
use TYPO3\CMS\Core\Core\Environment;
use TYPO3\CMS\Core\Http\RequestFactory;
......@@ -84,6 +83,7 @@ class GeneralUtility
* The application context
*
* @var \TYPO3\CMS\Core\Core\ApplicationContext
* @deprecated will be removed in TYPO3 v11.
*/
protected static $applicationContext;
......@@ -3783,7 +3783,7 @@ class GeneralUtility
*
* @param \TYPO3\CMS\Core\Core\ApplicationContext $applicationContext
* @throws \RuntimeException if applicationContext is overridden
* @internal This is not a public API method, do not use in own extensions
* @internal This is not a public API method, do not use in own extensions, will probably be removed in TYPO3 v11.
*/
public static function presetApplicationContext(ApplicationContext $applicationContext)
{
......@@ -3800,7 +3800,7 @@ class GeneralUtility
* variable in between multiple tests before it is re-initialized using presetApplicationContext()
* which otherwise throws an exception if the internal variable is already set.
*
* @internal May be changed or removed any time
* @internal May be changed or removed any time, will probably be removed in TYPO3 v11.
*/
public static function resetApplicationContext(): void
{
......@@ -3811,9 +3811,11 @@ class GeneralUtility
* Get the ApplicationContext
*
* @return \TYPO3\CMS\Core\Core\ApplicationContext
* @deprecated since TYPO3 v10.2, will be removed in TYPO3 v11, use Environment::getContext() instead.
*/
public static function getApplicationContext()
{
trigger_error('GeneralUtility::getApplicationContext() has been superseded by Environment API. This method will be removed in TYPO3 v11. Use Environment::getContext() instead.', E_USER_DEPRECATED);
return static::$applicationContext;
}
......
.. include:: ../../Includes.txt
======================================================================
Deprecation: #89631 - Use Environment API to fetch application context
======================================================================
See :issue:`89631`
Description
===========
The Environment API, introduced in TYPO3 v9.3, allows access to the current Application Context (Production, Testing or Development).
The method :php:`GeneralUtility::getApplicationContext()` has been deprecated, as the same information is now available in :php:`TYPO3\CMS\Core\Core\Environment::getContext()`.
Impact
======
Calling the GeneralUtility method will trigger a PHP deprecation warning.
Affected Installations
======================
Any TYPO3 installation with a third-party extension calling the method directly.
Migration
=========
Use the Environment API call and substitute the method directly.
.. index:: PHP-API, FullyScanned, ext:core
\ No newline at end of file
......@@ -24,6 +24,7 @@ use TYPO3\CMS\Core\Configuration\TypoScript\ConditionMatching\AbstractConditionM
use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Core\Context\DateTimeAspect;
use TYPO3\CMS\Core\Core\ApplicationContext;
use TYPO3\CMS\Core\Core\Environment;
use TYPO3\CMS\Core\Http\ServerRequest;
use TYPO3\CMS\Core\Package\PackageInterface;
use TYPO3\CMS\Core\Package\PackageManager;
......@@ -35,10 +36,7 @@ use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
*/
class AbstractConditionMatcherTest extends UnitTestCase
{
/**
* @var ApplicationContext
*/
protected $backupApplicationContext;
protected $backupEnvironment = true;
/**
* @var AbstractConditionMatcher|\PHPUnit\Framework\MockObject\MockObject|\TYPO3\TestingFramework\Core\AccessibleObjectInterface
......@@ -78,7 +76,6 @@ class AbstractConditionMatcherTest extends UnitTestCase
GeneralUtility::setSingletonInstance(PackageManager::class, $packageManagerProphecy->reveal());
$this->initConditionMatcher();
$this->backupApplicationContext = GeneralUtility::getApplicationContext();
}
protected function initConditionMatcher()
......@@ -90,15 +87,6 @@ class AbstractConditionMatcherTest extends UnitTestCase
$this->conditionMatcher->setLogger(new NullLogger());
}
/**
* Tear down
*/
protected function tearDown(): void
{
Fixtures\GeneralUtilityFixture::setApplicationContext($this->backupApplicationContext);
parent::tearDown();
}
/**
* @return array
*/
......@@ -249,9 +237,18 @@ class AbstractConditionMatcherTest extends UnitTestCase
*/
public function evaluateConditionCommonReturnsTrueForMatchingContexts($matchingContextCondition): void
{
/** @var ApplicationContext $applicationContext */
$applicationContext = new ApplicationContext('Production/Staging/Server2');
Fixtures\GeneralUtilityFixture::setApplicationContext($applicationContext);
Environment::initialize(
new ApplicationContext('Production/Staging/Server2'),
true,
false,
Environment::getProjectPath(),
Environment::getPublicPath(),
Environment::getVarPath(),
Environment::getConfigPath(),
Environment::getBackendPath() . '/index.php',
Environment::isWindows() ? 'WINDOWS' : 'UNIX'
);
$this->initConditionMatcher();
// Test expression language
......@@ -283,9 +280,17 @@ class AbstractConditionMatcherTest extends UnitTestCase
*/
public function evaluateConditionCommonReturnsNullForNotMatchingApplicationContexts($notMatchingApplicationContextCondition): void
{
/** @var ApplicationContext $applicationContext */
$applicationContext = new ApplicationContext('Production/Staging/Server2');
Fixtures\GeneralUtilityFixture::setApplicationContext($applicationContext);
Environment::initialize(
new ApplicationContext('Production/Staging/Server2'),
true,
false,
Environment::getProjectPath(),
Environment::getPublicPath(),
Environment::getVarPath(),
Environment::getConfigPath(),
Environment::getBackendPath() . '/index.php',
Environment::isWindows() ? 'WINDOWS' : 'UNIX'
);
$this->initConditionMatcher();
// Test expression language
......
<?php
namespace TYPO3\CMS\Core\Tests\Unit\Configuration\TypoScript\ConditionMatching\Fixtures;
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
/**
* Fixture for TYPO3\CMS\Core\Utility\GeneralUtility
*/
class GeneralUtilityFixture extends \TYPO3\CMS\Core\Utility\GeneralUtility
{
/**
* @param \TYPO3\CMS\Core\Core\ApplicationContext $applicationContext
*/
public static function setApplicationContext($applicationContext)
{
static::$applicationContext = $applicationContext;
}
}
......@@ -874,7 +874,7 @@ class ContentObjectRenderer implements LoggerAwareInterface
$exceptionHandlerClassName = null;
$tsfe = $this->getTypoScriptFrontendController();
if (!isset($tsfe->config['config']['contentObjectExceptionHandler'])) {
if (GeneralUtility::getApplicationContext()->isProduction()) {
if (Environment::getContext()->isProduction()) {
$exceptionHandlerClassName = '1';
}
} else {
......
......@@ -28,6 +28,7 @@ use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Core\Context\UserAspect;
use TYPO3\CMS\Core\Context\WorkspaceAspect;
use TYPO3\CMS\Core\Core\ApplicationContext;
use TYPO3\CMS\Core\Core\Environment;
use TYPO3\CMS\Core\Domain\Repository\PageRepository;
use TYPO3\CMS\Core\Http\ServerRequest;
use TYPO3\CMS\Core\Http\Uri;
......@@ -131,6 +132,8 @@ class ContentObjectRendererTest extends UnitTestCase
*/
protected $cacheManager;
protected $backupEnvironment = true;
/**
* Set up
*/
......@@ -2346,13 +2349,19 @@ class ContentObjectRendererTest extends UnitTestCase
*/
public function exceptionHandlerIsEnabledByDefaultInProductionContext(): void
{
$backupApplicationContext = GeneralUtility::getApplicationContext();
Fixtures\GeneralUtilityFixture::setApplicationContext(new ApplicationContext('Production'));
Environment::initialize(
new ApplicationContext('Production'),
true,
false,
Environment::getProjectPath(),
Environment::getPublicPath(),
Environment::getVarPath(),
Environment::getConfigPath(),
Environment::getBackendPath() . '/index.php',
Environment::isWindows() ? 'WINDOWS' : 'UNIX'
);
$contentObjectFixture = $this->createContentObjectThrowingExceptionFixture();
$this->subject->render($contentObjectFixture, []);
Fixtures\GeneralUtilityFixture::setApplicationContext($backupApplicationContext);
}
/**
......
<?php
namespace TYPO3\CMS\Frontend\Tests\Unit\ContentObject\Fixtures;
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
/**
* Fixture for TYPO3\CMS\Core\Utility\GeneralUtility
*/
class GeneralUtilityFixture extends \TYPO3\CMS\Core\Utility\GeneralUtility
{
/**
* @param \TYPO3\CMS\Core\Core\ApplicationContext $applicationContext
*/
public static function setApplicationContext($applicationContext)
{
static::$applicationContext = $applicationContext;
}
}
......@@ -14,6 +14,7 @@ namespace TYPO3\CMS\Install\Configuration\Context;
* The TYPO3 project - inspiring people to share!
*/
use TYPO3\CMS\Core\Core\Environment;
use TYPO3\CMS\Install\Configuration;
/**
......@@ -63,9 +64,8 @@ class DebugPreset extends Configuration\AbstractPreset
*/
public function getPriority()
{
$context = \TYPO3\CMS\Core\Utility\GeneralUtility::getApplicationContext();
$priority = $this->priority;
if ($context->isDevelopment()) {
if (Environment::getContext()->isDevelopment()) {
$priority = $priority + 20;
}
return $priority;
......
......@@ -14,6 +14,7 @@ namespace TYPO3\CMS\Install\Configuration\Context;
* The TYPO3 project - inspiring people to share!
*/
use TYPO3\CMS\Core\Core\Environment;
use TYPO3\CMS\Install\Configuration;
/**
......@@ -63,9 +64,8 @@ class LivePreset extends Configuration\AbstractPreset
*/
public function getPriority()
{
$context = \TYPO3\CMS\Core\Utility\GeneralUtility::getApplicationContext();
$priority = $this->priority;
if ($context->isProduction()) {
if (Environment::getContext()->isProduction()) {
$priority = $priority + 20;
}
return $priority;
......
......@@ -70,7 +70,7 @@ class InstallerController
public function initAction(): ResponseInterface
{
$bust = $GLOBALS['EXEC_TIME'];
if (!GeneralUtility::getApplicationContext()->isDevelopment()) {
if (!Environment::getContext()->isDevelopment()) {
$bust = GeneralUtility::hmac(TYPO3_version . Environment::getProjectPath());
}
$view = $this->initializeStandaloneView('Installer/Init.html');
......
......@@ -44,7 +44,7 @@ class LayoutController extends AbstractController
public function initAction(ServerRequestInterface $request): ResponseInterface
{
$bust = $GLOBALS['EXEC_TIME'];
if (!GeneralUtility::getApplicationContext()->isDevelopment()) {
if (!Environment::getContext()->isDevelopment()) {
$bust = GeneralUtility::hmac(TYPO3_version . Environment::getProjectPath());
}
$view = $this->initializeStandaloneView($request, 'Layout/Init.html');
......
......@@ -21,6 +21,7 @@ use TYPO3\CMS\Core\Configuration\ConfigurationManager;
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
use TYPO3\CMS\Core\Configuration\Loader\YamlFileLoader;
use TYPO3\CMS\Core\Core\Bootstrap;
use TYPO3\CMS\Core\Core\Environment;
use TYPO3\CMS\Core\Crypto\PasswordHashing\PasswordHashFactory;
use TYPO3\CMS\Core\Database\Connection;
use TYPO3\CMS\Core\Database\ConnectionPool;
......@@ -134,7 +135,7 @@ class SettingsController extends AbstractController
$formProtection = FormProtectionFactory::get(InstallToolFormProtection::class);
$view->assignMultiple([
'systemMaintainerWriteToken' => $formProtection->generateToken('installTool', 'systemMaintainerWrite'),
'systemMaintainerIsDevelopmentContext' => GeneralUtility::getApplicationContext()->isDevelopment(),
'systemMaintainerIsDevelopmentContext' => Environment::getContext()->isDevelopment(),
]);
$connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment