‪TYPO3CMS  10.4
ClassLoadingInformation.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
16 namespace ‪TYPO3\CMS\Core\Core;
17 
18 use Composer\Autoload\ClassLoader;
19 use TYPO3\ClassAliasLoader\ClassAliasMap;
23 use TYPO3\CMS\Core\Package\PackageManager;
25 
35 {
39  const ‪AUTOLOAD_INFO_DIR = 'autoload/';
40 
44  const ‪AUTOLOAD_INFO_DIR_TESTS = 'autoload-tests/';
45 
49  const ‪AUTOLOAD_CLASSMAP_FILENAME = 'autoload_classmap.php';
50 
54  const ‪AUTOLOAD_PSR4_FILENAME = 'autoload_psr4.php';
55 
59  const ‪AUTOLOAD_CLASSALIASMAP_FILENAME = 'autoload_classaliasmap.php';
60 
64  protected static ‪$classLoader;
65 
72  public static function ‪setClassLoader(ClassLoader ‪$classLoader)
73  {
74  static::$classLoader = ‪$classLoader;
75  }
76 
83  public static function ‪isClassLoadingInformationAvailable()
84  {
85  return file_exists(self::getClassLoadingInformationDirectory() . self::AUTOLOAD_CLASSMAP_FILENAME);
86  }
87 
91  public static function ‪dumpClassLoadingInformation()
92  {
94  $composerClassLoader = static::getClassLoader();
95  $activeExtensionPackages = static::getActiveExtensionPackages();
96 
98  $generator = GeneralUtility::makeInstance(ClassLoadingInformationGenerator::class, $composerClassLoader, $activeExtensionPackages, ‪Environment::getPublicPath() . '/', self::isTestingContext());
99  $classInfoFiles = $generator->buildAutoloadInformationFiles();
100  ‪GeneralUtility::writeFile(self::getClassLoadingInformationDirectory() . self::AUTOLOAD_CLASSMAP_FILENAME, $classInfoFiles['classMapFile']);
101  ‪GeneralUtility::writeFile(self::getClassLoadingInformationDirectory() . self::AUTOLOAD_PSR4_FILENAME, $classInfoFiles['psr-4File']);
102 
103  $classAliasMapFile = $generator->buildClassAliasMapFile();
104  ‪GeneralUtility::writeFile(self::getClassLoadingInformationDirectory() . self::AUTOLOAD_CLASSALIASMAP_FILENAME, $classAliasMapFile);
105  }
106 
111  public static function ‪updateClassLoadingInformationAfterPackageDeactivation(AfterPackageDeactivationEvent $event): void
112  {
114  return;
115  }
116  static::dumpClassLoadingInformation();
117  }
118 
123  public static function ‪updateClassLoadingInformationAfterPackageActivation(AfterPackageActivationEvent $event): void
124  {
126  return;
127  }
128  static::dumpClassLoadingInformation();
129  }
130 
135  public static function ‪registerClassLoadingInformation()
136  {
137  $composerClassLoader = static::getClassLoader();
138 
140  if (file_exists($dynamicClassAliasMapFile)) {
141  $classAliasMap = require $dynamicClassAliasMapFile;
142  if (is_array($classAliasMap) && !empty($classAliasMap['aliasToClassNameMapping']) && !empty($classAliasMap['classNameToAliasMapping'])) {
143  ClassAliasMap::addAliasMap($classAliasMap);
144  }
145  }
146 
148  if (file_exists($dynamicClassMapFile)) {
149  $classMap = require $dynamicClassMapFile;
150  if (!empty($classMap) && is_array($classMap)) {
151  $composerClassLoader->addClassMap($classMap);
152  }
153  }
154 
156  if (file_exists($dynamicPsr4File)) {
157  $psr4 = require $dynamicPsr4File;
158  if (is_array($psr4)) {
159  foreach ($psr4 as $prefix => $paths) {
160  $composerClassLoader->setPsr4($prefix, $paths);
161  }
162  }
163  }
164  }
165 
172  public static function ‪registerTransientClassLoadingInformationForPackage(PackageInterface $package)
173  {
174  $composerClassLoader = static::getClassLoader();
175  $activeExtensionPackages = static::getActiveExtensionPackages();
176 
178  $generator = GeneralUtility::makeInstance(ClassLoadingInformationGenerator::class, $composerClassLoader, $activeExtensionPackages, ‪Environment::getPublicPath() . '/', self::isTestingContext());
179 
180  $classInformation = $generator->buildClassLoadingInformationForPackage($package);
181  $composerClassLoader->addClassMap($classInformation['classMap']);
182  foreach ($classInformation['psr-4'] as $prefix => $paths) {
183  $composerClassLoader->setPsr4($prefix, $paths);
184  }
185  $classAliasMap = $generator->buildClassAliasMapForPackage($package);
186  if (is_array($classAliasMap) && !empty($classAliasMap['aliasToClassNameMapping']) && !empty($classAliasMap['classNameToAliasMapping'])) {
187  ClassAliasMap::addAliasMap($classAliasMap);
188  }
189  }
190 
194  protected static function ‪getClassLoadingInformationDirectory()
195  {
196  if (self::isTestingContext()) {
198  }
200  }
201 
208  public static function ‪getClassNameForAlias($alias)
209  {
210  return ClassAliasMap::getClassNameForAlias($alias);
211  }
212 
217  protected static function ‪ensureAutoloadInfoDirExists()
218  {
220  if (!file_exists($autoloadInfoDir)) {
221  ‪GeneralUtility::mkdir_deep($autoloadInfoDir);
222  }
223  }
224 
231  protected static function ‪getClassLoader()
232  {
233  return static::$classLoader;
234  }
235 
242  protected static function ‪isTestingContext()
243  {
245  }
246 
252  protected static function ‪getActiveExtensionPackages()
253  {
254  $activeExtensionPackages = [];
255  $packageManager = GeneralUtility::makeInstance(PackageManager::class);
256  foreach ($packageManager->getActivePackages() as $package) {
257  if ($package->getValueFromComposerManifest('type') === 'typo3-cms-framework') {
258  // Skip all core packages as the class loading info is prepared for them already
259  continue;
260  }
261  $activeExtensionPackages[] = $package;
262  }
263  return $activeExtensionPackages;
264  }
265 }
‪TYPO3\CMS\Core\Core\ClassLoadingInformation\AUTOLOAD_PSR4_FILENAME
‪const AUTOLOAD_PSR4_FILENAME
Definition: ClassLoadingInformation.php:54
‪TYPO3\CMS\Core\Core\Environment\getPublicPath
‪static string getPublicPath()
Definition: Environment.php:180
‪TYPO3\CMS\Core\Core\ClassLoadingInformation\AUTOLOAD_CLASSALIASMAP_FILENAME
‪const AUTOLOAD_CLASSALIASMAP_FILENAME
Definition: ClassLoadingInformation.php:59
‪TYPO3\CMS\Core\Core\ClassLoadingInformation\getClassLoader
‪static ClassLoader getClassLoader()
Definition: ClassLoadingInformation.php:230
‪TYPO3\CMS\Core\Core\ClassLoadingInformation\dumpClassLoadingInformation
‪static dumpClassLoadingInformation()
Definition: ClassLoadingInformation.php:90
‪TYPO3\CMS\Core\Core\ClassLoadingInformation\getClassLoadingInformationDirectory
‪static string getClassLoadingInformationDirectory()
Definition: ClassLoadingInformation.php:193
‪TYPO3\CMS\Core\Core\ClassLoadingInformation\isClassLoadingInformationAvailable
‪static bool isClassLoadingInformationAvailable()
Definition: ClassLoadingInformation.php:82
‪TYPO3\CMS\Core\Core\ClassLoadingInformation\ensureAutoloadInfoDirExists
‪static ensureAutoloadInfoDirExists()
Definition: ClassLoadingInformation.php:216
‪TYPO3\CMS\Core\Core\ClassLoadingInformation
Definition: ClassLoadingInformation.php:35
‪TYPO3\CMS\Core\Core\ClassLoadingInformation\updateClassLoadingInformationAfterPackageActivation
‪static updateClassLoadingInformationAfterPackageActivation(AfterPackageActivationEvent $event)
Definition: ClassLoadingInformation.php:122
‪TYPO3\CMS\Core\Core\ClassLoadingInformation\getActiveExtensionPackages
‪static PackageInterface[] getActiveExtensionPackages()
Definition: ClassLoadingInformation.php:251
‪TYPO3\CMS\Core\Package\PackageInterface
Definition: PackageInterface.php:22
‪TYPO3\CMS\Core\Core\Environment\getContext
‪static ApplicationContext getContext()
Definition: Environment.php:133
‪TYPO3\CMS\Core\Core\ClassLoadingInformation\AUTOLOAD_CLASSMAP_FILENAME
‪const AUTOLOAD_CLASSMAP_FILENAME
Definition: ClassLoadingInformation.php:49
‪TYPO3\CMS\Core\Core\ClassLoadingInformation\AUTOLOAD_INFO_DIR
‪const AUTOLOAD_INFO_DIR
Definition: ClassLoadingInformation.php:39
‪TYPO3\CMS\Core\Core\ClassLoadingInformation\getClassNameForAlias
‪static mixed getClassNameForAlias($alias)
Definition: ClassLoadingInformation.php:207
‪TYPO3\CMS\Core\Core\ApplicationContext\isTesting
‪bool isTesting()
Definition: ApplicationContext.php:116
‪TYPO3\CMS\Core\Utility\GeneralUtility\mkdir_deep
‪static mkdir_deep($directory)
Definition: GeneralUtility.php:2022
‪TYPO3\CMS\Core\Package\Event\AfterPackageActivationEvent
Definition: AfterPackageActivationEvent.php:24
‪TYPO3\CMS\Core\Core\ClassLoadingInformation\updateClassLoadingInformationAfterPackageDeactivation
‪static updateClassLoadingInformationAfterPackageDeactivation(AfterPackageDeactivationEvent $event)
Definition: ClassLoadingInformation.php:110
‪TYPO3\CMS\Core\Core\Environment\isComposerMode
‪static bool isComposerMode()
Definition: Environment.php:144
‪TYPO3\CMS\Core\Core\ClassLoadingInformation\AUTOLOAD_INFO_DIR_TESTS
‪const AUTOLOAD_INFO_DIR_TESTS
Definition: ClassLoadingInformation.php:44
‪TYPO3\CMS\Core\Core\ClassLoadingInformation\registerTransientClassLoadingInformationForPackage
‪static registerTransientClassLoadingInformationForPackage(PackageInterface $package)
Definition: ClassLoadingInformation.php:171
‪TYPO3\CMS\Core\Package\Event\AfterPackageDeactivationEvent
Definition: AfterPackageDeactivationEvent.php:24
‪TYPO3\CMS\Core\Core
Definition: ApplicationContext.php:16
‪TYPO3\CMS\Core\Core\ClassLoadingInformation\registerClassLoadingInformation
‪static registerClassLoadingInformation()
Definition: ClassLoadingInformation.php:134
‪TYPO3\CMS\Core\Core\ClassLoadingInformation\$classLoader
‪static ClassLoader $classLoader
Definition: ClassLoadingInformation.php:63
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility\writeFile
‪static bool writeFile($file, $content, $changePermissions=false)
Definition: GeneralUtility.php:1836
‪TYPO3\CMS\Core\Core\Environment\getLegacyConfigPath
‪static string getLegacyConfigPath()
Definition: Environment.php:282
‪TYPO3\CMS\Core\Core\ClassLoadingInformation\setClassLoader
‪static setClassLoader(ClassLoader $classLoader)
Definition: ClassLoadingInformation.php:71
‪TYPO3\CMS\Core\Core\ClassLoadingInformation\isTestingContext
‪static bool isTestingContext()
Definition: ClassLoadingInformation.php:241