[TASK] Consolidate singleton retrieval 02/55802/7
authorBenjamin Franzke <bfr@qbus.de>
Sat, 17 Feb 2018 21:43:01 +0000 (22:43 +0100)
committerBenni Mack <benni@typo3.org>
Mon, 26 Feb 2018 15:43:51 +0000 (16:43 +0100)
There is Bootstrap::getInstance()->getEarlyInstance() and
GeneralUtility::makeInstance() to retrieve (global) early instances.
Sometimes the former, sometimes the latter is used (e.g. to
retrieve the PackageManager).

Classes should not know whether something is an early
instance or not. Implementation details like that should be
abstracted into a singleton container. That (currently) is
GeneralUtility::makeInstance().

As there is no obvious reason why different methods are used,
we now use GenerallyUtility::makeInstance() to retrieve singletons.

Add all early singleton instances to the GeneralUtility singleton array
and stop using Bootstrap::getEarlyInstance in classes outside Bootstrap.

One exception to this is the composer ClassLoader. That (obviously)
does not implement the TYPO3 SingletonInterface.
We now push that instance to ClassLoadingInformation during the
bootstrap. (similar to how the PackageManager is pushed to the
ExtensionManagementUtility).

Releases: master
Resolves: #83966
Change-Id: Icf3bef5f51a6142d9d1dcdc9b3700a86d7bb7f78
Reviewed-on: https://review.typo3.org/55802
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Frank Naegler <frank.naegler@typo3.org>
Tested-by: Frank Naegler <frank.naegler@typo3.org>
Reviewed-by: Benni Mack <benni@typo3.org>
Tested-by: Benni Mack <benni@typo3.org>
typo3/sysext/core/Classes/Core/Bootstrap.php
typo3/sysext/core/Classes/Core/ClassLoadingInformation.php
typo3/sysext/core/Classes/Package/DependencyResolver.php
typo3/sysext/install/Classes/Controller/InstallerController.php
typo3/sysext/install/Classes/Http/Application.php

index a270f3b..a1eb51b 100644 (file)
@@ -218,6 +218,7 @@ class Bootstrap
     public function initializeClassLoader($classLoader)
     {
         $this->setEarlyInstance(\Composer\Autoload\ClassLoader::class, $classLoader);
+        ClassLoadingInformation::setClassLoader($classLoader);
         if (defined('TYPO3_COMPOSER_MODE') && TYPO3_COMPOSER_MODE) {
             self::$usesComposerClassLoading = true;
         }
@@ -449,14 +450,14 @@ class Bootstrap
     {
         /** @var \TYPO3\CMS\Core\Package\PackageManager $packageManager */
         $packageManager = new $packageManagerClassName();
+        GeneralUtility::setSingletonInstance(\TYPO3\CMS\Core\Package\PackageManager::class, $packageManager);
         $this->setEarlyInstance(\TYPO3\CMS\Core\Package\PackageManager::class, $packageManager);
         ExtensionManagementUtility::setPackageManager($packageManager);
-        $packageManager->injectCoreCache($this->getEarlyInstance(\TYPO3\CMS\Core\Cache\CacheManager::class)->getCache('cache_core'));
+        $packageManager->injectCoreCache(GeneralUtility::makeInstance(\TYPO3\CMS\Core\Cache\CacheManager::class)->getCache('cache_core'));
         $dependencyResolver = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Package\DependencyResolver::class);
         $dependencyResolver->injectDependencyOrderingService(GeneralUtility::makeInstance(\TYPO3\CMS\Core\Service\DependencyOrderingService::class));
         $packageManager->injectDependencyResolver($dependencyResolver);
         $packageManager->initialize();
-        GeneralUtility::setSingletonInstance(\TYPO3\CMS\Core\Package\PackageManager::class, $packageManager);
         return $this;
     }
 
@@ -470,8 +471,7 @@ class Bootstrap
     {
         $packages = $GLOBALS['TYPO3_CONF_VARS']['EXT']['runtimeActivatedPackages'] ?? [];
         if (!empty($packages)) {
-            /** @var \TYPO3\CMS\Core\Package\PackageManager $packageManager */
-            $packageManager = $this->getEarlyInstance(\TYPO3\CMS\Core\Package\PackageManager::class);
+            $packageManager = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Package\PackageManager::class);
             foreach ($packages as $runtimeAddedPackageKey) {
                 $packageManager->activatePackageDuringRuntime($runtimeAddedPackageKey);
             }
@@ -683,7 +683,7 @@ class Bootstrap
      */
     public function setFinalCachingFrameworkCacheConfiguration()
     {
-        $this->getEarlyInstance(\TYPO3\CMS\Core\Cache\CacheManager::class)->setCacheConfigurations($GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']);
+        GeneralUtility::makeInstance(\TYPO3\CMS\Core\Cache\CacheManager::class)->setCacheConfigurations($GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']);
         return $this;
     }
 
@@ -841,14 +841,14 @@ class Bootstrap
         $cacheIdentifier = 'BackendRoutesFromPackages_' . sha1((TYPO3_version . PATH_site . 'BackendRoutesFromPackages'));
 
         /** @var $codeCache \TYPO3\CMS\Core\Cache\Frontend\FrontendInterface */
-        $codeCache = $this->getEarlyInstance(\TYPO3\CMS\Core\Cache\CacheManager::class)->getCache('cache_core');
+        $codeCache = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Cache\CacheManager::class)->getCache('cache_core');
         $routesFromPackages = [];
         if ($codeCache->has($cacheIdentifier)) {
             // substr is necessary, because the php frontend wraps php code around the cache value
             $routesFromPackages = unserialize(substr($codeCache->get($cacheIdentifier), 6, -2));
         } else {
             // Loop over all packages and check for a Configuration/Backend/Routes.php file
-            $packageManager = $this->getEarlyInstance(\TYPO3\CMS\Core\Package\PackageManager::class);
+            $packageManager = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Package\PackageManager::class);
             $packages = $packageManager->getActivePackages();
             foreach ($packages as $package) {
                 $routesFileNameForPackage = $package->getPackagePath() . 'Configuration/Backend/Routes.php';
index dbdba97..8eaf7a4 100644 (file)
@@ -56,6 +56,22 @@ class ClassLoadingInformation
     const AUTOLOAD_CLASSALIASMAP_FILENAME = 'autoload_classaliasmap.php';
 
     /**
+     * @var ClassLoader
+     */
+    protected static $classLoader;
+
+    /**
+     * Sets the package manager instance
+     *
+     * @param ClassLoader $classLoader
+     * @internal
+     */
+    public static function setClassLoader(ClassLoader $classLoader)
+    {
+        static::$classLoader = $classLoader;
+    }
+
+    /**
      * Checks if the autoload_classmap.php exists and we are not in testing context.
      * Used to see if the ClassLoadingInformationGenerator should be called.
      *
@@ -187,7 +203,7 @@ class ClassLoadingInformation
      */
     protected static function getClassLoader()
     {
-        return Bootstrap::getInstance()->getEarlyInstance(ClassLoader::class);
+        return static::$classLoader;
     }
 
     /**
@@ -209,8 +225,7 @@ class ClassLoadingInformation
     protected static function getActiveExtensionPackages()
     {
         $activeExtensionPackages = [];
-        /** @var PackageManager $packageManager */
-        $packageManager = Bootstrap::getInstance()->getEarlyInstance(PackageManager::class);
+        $packageManager = GeneralUtility::makeInstance(PackageManager::class);
         foreach ($packageManager->getActivePackages() as $package) {
             if ($package->getValueFromComposerManifest('type') === 'typo3-cms-framework') {
                 // Skip all core packages as the class loading info is prepared for them already
index 2f860a3..16006a8 100644 (file)
@@ -14,8 +14,8 @@ namespace TYPO3\CMS\Core\Package;
  * The TYPO3 project - inspiring people to share!
  */
 
-use TYPO3\CMS\Core\Core\Bootstrap;
 use TYPO3\CMS\Core\Service\DependencyOrderingService;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
 
 /**
  * This class takes care about dependencies between packages.
@@ -154,8 +154,7 @@ class DependencyResolver
     protected function findFrameworkPackages(array $packageStateConfiguration)
     {
         $frameworkPackageKeys = [];
-        /** @var PackageManager $packageManager */
-        $packageManager = Bootstrap::getInstance()->getEarlyInstance(\TYPO3\CMS\Core\Package\PackageManager::class);
+        $packageManager = GeneralUtility::makeInstance(PackageManager::class);
         foreach ($packageStateConfiguration as $packageKey => $packageConfiguration) {
             /** @var Package $package */
             $package = $packageManager->getPackage($packageKey);
index 6686ae6..d7b5898 100644 (file)
@@ -157,7 +157,7 @@ class InstallerController
 
             // Create a PackageStates.php with all packages activated marked as "part of factory default"
             if (!file_exists(PATH_typo3conf . 'PackageStates.php')) {
-                $packageManager = Bootstrap::getInstance()->getEarlyInstance(PackageManager::class);
+                $packageManager = GeneralUtility::makeInstance(PackageManager::class);
                 $packages = $packageManager->getAvailablePackages();
                 foreach ($packages as $package) {
                     if ($package instanceof PackageInterface
index 5648d2e..a751aa3 100644 (file)
@@ -99,8 +99,7 @@ class Application extends AbstractApplication
             }
             $cacheConfigurationsWithCachesSetToNullBackend[$cacheName] = $cacheConfiguration;
         }
-        /** @var $cacheManager \TYPO3\CMS\Core\Cache\CacheManager */
-        $cacheManager = $this->bootstrap->getEarlyInstance(\TYPO3\CMS\Core\Cache\CacheManager::class);
+        $cacheManager = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Cache\CacheManager::class);
         $cacheManager->setCacheConfigurations($cacheConfigurationsWithCachesSetToNullBackend);
     }