Skip to content

Commit

Permalink
[TASK] Make stateless Bootstrap methods static
Browse files Browse the repository at this point in the history
Methods that do not use $this but mainly act as utility
may be used statically.
The previous non-static chainable behaviour is preserved
by returning static::$instance. (Invoking a static method
non statically does not trigger E_NOTICE or alike.)

The motivation for this change is to deprecate the
Bootstrap instanciated usage at some (later) point
in order to reduce global state.

The following methods are affected:
 * initializeLanguageObject
 * initializeBackendAuthentication
 * initializeBackendUser
 * initializeBackendRouter
 * loadExtTables
 * loadBaseTca
 * loadTypo3LoadedExtAndExtLocalconf
 * unsetReservedGlobalVariables
 * startOutputBuffering
 * disableCoreCache

Releases: master
Resolves: #83952
Change-Id: I59d3027c5d10326d7bab2ae02e6ff0eb836f23e4
Reviewed-on: https://review.typo3.org/55775
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
Reviewed-by: Frank Naegler <frank.naegler@typo3.org>
Tested-by: Frank Naegler <frank.naegler@typo3.org>
  • Loading branch information
bnf authored and NeoBlack committed Mar 1, 2018
1 parent 48eec02 commit eabae6a
Show file tree
Hide file tree
Showing 31 changed files with 85 additions and 101 deletions.
Expand Up @@ -49,7 +49,7 @@ public function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
Bootstrap::getInstance()->initializeBackendAuthentication();
Bootstrap::initializeBackendAuthentication();

$isTestOnly = $input->getOption('check');
$isSilent = $output->getVerbosity() !== OutputInterface::VERBOSITY_QUIET;
Expand Down
Expand Up @@ -63,9 +63,8 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
. ' to use the TYPO3 API.', E_USER_DEPRECATED);
}

Bootstrap::getInstance()
->initializeBackendRouter()
->loadExtTables();
Bootstrap::initializeBackendRouter();
Bootstrap::loadExtTables();

// Add the route path to the request
$request = $request->withAttribute('routePath', $pathToRoute);
Expand Down
Expand Up @@ -53,11 +53,10 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
{
$pathToRoute = $request->getAttribute('routePath', '/login');

Bootstrap::getInstance()
->initializeBackendUser()
// @todo: once this logic is in this method, the redirect URL should be handled as response here
->initializeBackendAuthentication($this->isLoggedInBackendUserRequired($pathToRoute))
->initializeLanguageObject();
Bootstrap::initializeBackendUser();
// @todo: once this logic is in this method, the redirect URL should be handled as response here
Bootstrap::initializeBackendAuthentication($this->isLoggedInBackendUserRequired($pathToRoute));
Bootstrap::initializeLanguageObject();

return $handler->handle($request);
}
Expand Down
Expand Up @@ -50,7 +50,7 @@ protected function setUp()
$this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/backend/Tests/Functional/Fixtures/tx_irretutorial_1ncsv_hotel.xml');

$this->setUpBackendUserFromFixture(1);
Bootstrap::getInstance()->initializeLanguageObject();
Bootstrap::initializeLanguageObject();

$this->subject = new FormInlineAjaxController();
}
Expand Down
Expand Up @@ -58,7 +58,7 @@ protected function setUp()
$this->backendUser = $this->setUpBackendUserFromFixture(1);
$this->backendUser->workspace = 0;

Bootstrap::getInstance()->initializeLanguageObject();
Bootstrap::initializeLanguageObject();
$this->actionService = GeneralUtility::makeInstance(ActionService::class);

$this->importDataSet(__DIR__ . '/Fixtures/pages.xml');
Expand Down
Expand Up @@ -35,7 +35,7 @@ protected function setUp()
parent::setUp();

$this->setUpBackendUserFromFixture(1);
Bootstrap::getInstance()->initializeLanguageObject();
Bootstrap::initializeLanguageObject();

$this->importCSVDataSet(ORIGINAL_ROOT . 'typo3/sysext/backend/Tests/Functional/Domain/Repository/Localization/Fixtures/DefaultPagesAndContent.csv');

Expand Down
2 changes: 1 addition & 1 deletion typo3/sysext/core/Classes/Console/CommandApplication.php
Expand Up @@ -60,7 +60,7 @@ public function __construct($classLoader)
*/
public function run(callable $execute = null)
{
$handler = GeneralUtility::makeInstance(CommandRequestHandler::class, $this->bootstrap);
$handler = GeneralUtility::makeInstance(CommandRequestHandler::class);
$handler->handleRequest(new ArgvInput());

if ($execute !== null) {
Expand Down
22 changes: 6 additions & 16 deletions typo3/sysext/core/Classes/Console/CommandRequestHandler.php
Expand Up @@ -28,26 +28,17 @@
*/
class CommandRequestHandler implements RequestHandlerInterface
{
/**
* Instance of the current TYPO3 bootstrap
* @var Bootstrap
*/
protected $bootstrap;

/**
* Instance of the symfony application
* @var Application
*/
protected $application;

/**
* Constructor handing over the bootstrap
*
* @param Bootstrap $bootstrap
* Constructor initializing the symfony application
*/
public function __construct(Bootstrap $bootstrap)
public function __construct()
{
$this->bootstrap = $bootstrap;
$this->application = new Application('TYPO3 CMS', TYPO3_version);
}

Expand All @@ -60,11 +51,10 @@ public function handleRequest(InputInterface $input)
{
$output = new ConsoleOutput();

$this->bootstrap
->loadExtTables()
// create the BE_USER object (not logged in yet)
->initializeBackendUser(CommandLineUserAuthentication::class)
->initializeLanguageObject();
Bootstrap::loadExtTables();
// create the BE_USER object (not logged in yet)
Bootstrap::initializeBackendUser(CommandLineUserAuthentication::class);
Bootstrap::initializeLanguageObject();
// Make sure output is not buffered, so command-line output and interaction can take place
ob_clean();

Expand Down
64 changes: 32 additions & 32 deletions typo3/sysext/core/Classes/Core/Bootstrap.php
Expand Up @@ -110,13 +110,13 @@ public static function getInstance()
* Prevent any unwanted output that may corrupt AJAX/compression.
* This does not interfere with "die()" or "echo"+"exit()" messages!
*
* @return Bootstrap
* @return Bootstrap|null
* @internal This is not a public API method, do not use in own extensions
*/
public function startOutputBuffering()
public static function startOutputBuffering()
{
ob_start();
return $this;
return static::$instance;
}

/**
Expand Down Expand Up @@ -337,13 +337,13 @@ protected function initializeRuntimeActivatedPackagesFromConfiguration()
* Load ext_localconf of extensions
*
* @param bool $allowCaching
* @return Bootstrap
* @return Bootstrap|null
* @internal This is not a public API method, do not use in own extensions
*/
public function loadTypo3LoadedExtAndExtLocalconf($allowCaching = true)
public static function loadTypo3LoadedExtAndExtLocalconf($allowCaching = true)
{
ExtensionManagementUtility::loadExtLocalconf($allowCaching);
return $this;
return static::$instance;
}

/**
Expand All @@ -368,15 +368,15 @@ public function populateLocalConfiguration()
/**
* Set cache_core to null backend, effectively disabling eg. the cache for ext_localconf and PackageManager etc.
*
* @return \TYPO3\CMS\Core\Core\Bootstrap
* @return Bootstrap|null
* @internal This is not a public API method, do not use in own extensions
*/
public function disableCoreCache()
public static function disableCoreCache()
{
$GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']['cache_core']['backend']
= \TYPO3\CMS\Core\Cache\Backend\NullBackend::class;
unset($GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']['cache_core']['options']);
return $this;
return static::$instance;
}

/**
Expand Down Expand Up @@ -545,10 +545,10 @@ public function setFinalCachingFrameworkCacheConfiguration()
* Unsetting reserved global variables:
* Those are set in "ext:core/ext_tables.php" file:
*
* @return Bootstrap
* @return Bootstrap|null
* @internal This is not a public API method, do not use in own extensions
*/
public function unsetReservedGlobalVariables()
public static function unsetReservedGlobalVariables()
{
unset($GLOBALS['PAGES_TYPES']);
unset($GLOBALS['TCA']);
Expand All @@ -559,7 +559,7 @@ public function unsetReservedGlobalVariables()
unset($GLOBALS['TBE_MODULES_EXT']);
unset($GLOBALS['TCA_DESCR']);
unset($GLOBALS['LOCAL_LANG']);
return $this;
return static::$instance;
}

/**
Expand All @@ -568,13 +568,13 @@ public function unsetReservedGlobalVariables()
* This will mainly set up $TCA through extMgm API.
*
* @param bool $allowCaching True, if loading TCA from cache is allowed
* @return Bootstrap
* @return Bootstrap|null
* @internal This is not a public API method, do not use in own extensions
*/
public function loadBaseTca(bool $allowCaching = true): Bootstrap
public static function loadBaseTca(bool $allowCaching = true)
{
ExtensionManagementUtility::loadBaseTca($allowCaching);
return $this;
return static::$instance;
}

/**
Expand All @@ -584,22 +584,22 @@ public function loadBaseTca(bool $allowCaching = true): Bootstrap
* or the according cache file if exists.
*
* @param bool $allowCaching True, if reading compiled ext_tables file from cache is allowed
* @return Bootstrap
* @return Bootstrap|null
* @internal This is not a public API method, do not use in own extensions
*/
public function loadExtTables(bool $allowCaching = true): Bootstrap
public static function loadExtTables(bool $allowCaching = true)
{
ExtensionManagementUtility::loadExtTables($allowCaching);
$this->runExtTablesPostProcessingHooks();
return $this;
static::runExtTablesPostProcessingHooks();
return static::$instance;
}

/**
* Check for registered ext tables hooks and run them
*
* @throws \UnexpectedValueException
*/
protected function runExtTablesPostProcessingHooks()
protected static function runExtTablesPostProcessingHooks()
{
foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['GLOBAL']['extTablesInclusion-PostProcessing'] ?? [] as $className) {
/** @var $hookObject \TYPO3\CMS\Core\Database\TableConfigurationPostProcessingHookInterface */
Expand All @@ -618,10 +618,10 @@ protected function runExtTablesPostProcessingHooks()
* Initialize the Routing for the TYPO3 Backend
* Loads all routes registered inside all packages and stores them inside the Router
*
* @return Bootstrap
* @return Bootstrap|null
* @internal This is not a public API method, do not use in own extensions
*/
public function initializeBackendRouter()
public static function initializeBackendRouter()
{
// See if the Routes.php from all active packages have been built together already
$cacheIdentifier = 'BackendRoutesFromPackages_' . sha1((TYPO3_version . PATH_site . 'BackendRoutesFromPackages'));
Expand Down Expand Up @@ -669,51 +669,51 @@ public function initializeBackendRouter()
$route = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Routing\Route::class, $path, $options);
$router->addRoute($name, $route);
}
return $this;
return static::$instance;
}

/**
* Initialize backend user object in globals
*
* @param string $className usually \TYPO3\CMS\Core\Authentication\BackendUserAuthentication::class but can be used for CLI
* @return Bootstrap
* @return Bootstrap|null
* @internal This is not a public API method, do not use in own extensions
*/
public function initializeBackendUser($className = \TYPO3\CMS\Core\Authentication\BackendUserAuthentication::class)
public static function initializeBackendUser($className = \TYPO3\CMS\Core\Authentication\BackendUserAuthentication::class)
{
/** @var $backendUser \TYPO3\CMS\Core\Authentication\BackendUserAuthentication */
$backendUser = GeneralUtility::makeInstance($className);
// The global must be available very early, because methods below
// might trigger code which relies on it. See: #45625
$GLOBALS['BE_USER'] = $backendUser;
$backendUser->start();
return $this;
return static::$instance;
}

/**
* Initializes and ensures authenticated access
*
* @internal This is not a public API method, do not use in own extensions
* @param bool $proceedIfNoUserIsLoggedIn if set to TRUE, no forced redirect to the login page will be done
* @return \TYPO3\CMS\Core\Core\Bootstrap
* @return Bootstrap|null
*/
public function initializeBackendAuthentication($proceedIfNoUserIsLoggedIn = false)
public static function initializeBackendAuthentication($proceedIfNoUserIsLoggedIn = false)
{
$GLOBALS['BE_USER']->backendCheckLogin($proceedIfNoUserIsLoggedIn);
return $this;
return static::$instance;
}

/**
* Initialize language object
*
* @return Bootstrap
* @return Bootstrap|null
* @internal This is not a public API method, do not use in own extensions
*/
public function initializeLanguageObject()
public static function initializeLanguageObject()
{
/** @var $GLOBALS['LANG'] \TYPO3\CMS\Core\Localization\LanguageService */
$GLOBALS['LANG'] = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Localization\LanguageService::class);
$GLOBALS['LANG']->init($GLOBALS['BE_USER']->uc['lang']);
return $this;
return static::$instance;
}
}
Expand Up @@ -87,7 +87,7 @@ protected function setUp()
$this->backendUser->workspace = 0;

$this->actionService = $this->getActionService();
Bootstrap::getInstance()->initializeLanguageObject();
Bootstrap::initializeLanguageObject();
}

protected function tearDown()
Expand Down
Expand Up @@ -421,9 +421,8 @@ public function reloadCaches()
{
$this->reloadOpcache();
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::loadExtLocalconf(false);
\TYPO3\CMS\Core\Core\Bootstrap::getInstance()
->loadBaseTca(false)
->loadExtTables(false);
\TYPO3\CMS\Core\Core\Bootstrap::loadBaseTca(false);
\TYPO3\CMS\Core\Core\Bootstrap::loadExtTables(false);
}

/**
Expand Down
Expand Up @@ -80,10 +80,9 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
// like $GLOBALS['LANG'] for labels in the language of the BE User, the router, and ext_tables.php for all modules
// So things like Frontend Editing and Admin Panel can use this for generating links to the TYPO3 Backend.
if ($GLOBALS['BE_USER'] instanceof FrontendBackendUserAuthentication) {
Bootstrap::getInstance()
->initializeLanguageObject()
->initializeBackendRouter()
->loadExtTables();
Bootstrap::initializeLanguageObject();
Bootstrap::initializeBackendRouter();
Bootstrap::loadExtTables();
// Initialize admin panel since simulation settings are required here
$GLOBALS['BE_USER']->initializeAdminPanel();
}
Expand Down
2 changes: 1 addition & 1 deletion typo3/sysext/impexp/Classes/Command/ImportCommand.php
Expand Up @@ -80,7 +80,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$io = new SymfonyStyle($input, $output);

// Ensure the _cli_ user is authenticated
Bootstrap::getInstance()->initializeBackendAuthentication();
Bootstrap::initializeBackendAuthentication();

$pageId = (int)$input->getArgument('pageId');

Expand Down
Expand Up @@ -66,7 +66,7 @@ protected function setUp()

$backendUser = $this->setUpBackendUserFromFixture(1);
$backendUser->workspace = 0;
Bootstrap::getInstance()->initializeLanguageObject();
Bootstrap::initializeLanguageObject();
}

/**
Expand Down
Expand Up @@ -56,10 +56,9 @@ protected function initializeStandaloneView(ServerRequestInterface $request, str
*/
protected function loadExtLocalconfDatabaseAndExtTables()
{
\TYPO3\CMS\Core\Core\Bootstrap::getInstance()
->loadTypo3LoadedExtAndExtLocalconf(false)
->unsetReservedGlobalVariables()
->loadBaseTca(false)
->loadExtTables(false);
\TYPO3\CMS\Core\Core\Bootstrap::loadTypo3LoadedExtAndExtLocalconf(false);
\TYPO3\CMS\Core\Core\Bootstrap::unsetReservedGlobalVariables();
\TYPO3\CMS\Core\Core\Bootstrap::loadBaseTca(false);
\TYPO3\CMS\Core\Core\Bootstrap::loadExtTables(false);
}
}
Expand Up @@ -1115,10 +1115,9 @@ protected function importDatabaseData()
*/
protected function loadExtLocalconfDatabaseAndExtTables()
{
\TYPO3\CMS\Core\Core\Bootstrap::getInstance()
->loadTypo3LoadedExtAndExtLocalconf(false)
->unsetReservedGlobalVariables()
->loadBaseTca(false)
->loadExtTables(false);
\TYPO3\CMS\Core\Core\Bootstrap::loadTypo3LoadedExtAndExtLocalconf(false);
\TYPO3\CMS\Core\Core\Bootstrap::unsetReservedGlobalVariables();
\TYPO3\CMS\Core\Core\Bootstrap::loadBaseTca(false);
\TYPO3\CMS\Core\Core\Bootstrap::loadExtTables(false);
}
}

0 comments on commit eabae6a

Please sign in to comment.