‪TYPO3CMS  9.5
ClassLoadingInformation.php
Go to the documentation of this file.
1 <?php
2 namespace ‪TYPO3\CMS\Core\Core;
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
17 use Composer\Autoload\ClassLoader;
18 use TYPO3\ClassAliasLoader\ClassAliasMap;
20 use TYPO3\CMS\Core\Package\PackageManager;
22 
32 {
36  const ‪AUTOLOAD_INFO_DIR = 'autoload/';
37 
41  const ‪AUTOLOAD_INFO_DIR_TESTS = 'autoload-tests/';
42 
46  const ‪AUTOLOAD_CLASSMAP_FILENAME = 'autoload_classmap.php';
47 
51  const ‪AUTOLOAD_PSR4_FILENAME = 'autoload_psr4.php';
52 
56  const ‪AUTOLOAD_CLASSALIASMAP_FILENAME = 'autoload_classaliasmap.php';
57 
61  protected static ‪$classLoader;
62 
69  public static function ‪setClassLoader(ClassLoader ‪$classLoader)
70  {
71  static::$classLoader = ‪$classLoader;
72  }
73 
80  public static function ‪isClassLoadingInformationAvailable()
81  {
82  return file_exists(self::getClassLoadingInformationDirectory() . self::AUTOLOAD_CLASSMAP_FILENAME);
83  }
84 
88  public static function ‪dumpClassLoadingInformation()
89  {
91  $composerClassLoader = static::getClassLoader();
92  $activeExtensionPackages = static::getActiveExtensionPackages();
93 
95  $generator = GeneralUtility::makeInstance(ClassLoadingInformationGenerator::class, $composerClassLoader, $activeExtensionPackages, ‪Environment::getPublicPath() . '/', self::isTestingContext());
96  $classInfoFiles = $generator->buildAutoloadInformationFiles();
97  GeneralUtility::writeFile(self::getClassLoadingInformationDirectory() . self::AUTOLOAD_CLASSMAP_FILENAME, $classInfoFiles['classMapFile']);
98  GeneralUtility::writeFile(self::getClassLoadingInformationDirectory() . self::AUTOLOAD_PSR4_FILENAME, $classInfoFiles['psr-4File']);
99 
100  $classAliasMapFile = $generator->buildClassAliasMapFile();
101  GeneralUtility::writeFile(self::getClassLoadingInformationDirectory() . self::AUTOLOAD_CLASSALIASMAP_FILENAME, $classAliasMapFile);
102  }
103 
108  public static function ‪registerClassLoadingInformation()
109  {
110  $composerClassLoader = static::getClassLoader();
111 
113  if (file_exists($dynamicClassAliasMapFile)) {
114  $classAliasMap = require $dynamicClassAliasMapFile;
115  if (is_array($classAliasMap) && !empty($classAliasMap['aliasToClassNameMapping']) && !empty($classAliasMap['classNameToAliasMapping'])) {
116  ClassAliasMap::addAliasMap($classAliasMap);
117  }
118  }
119 
121  if (file_exists($dynamicClassMapFile)) {
122  $classMap = require $dynamicClassMapFile;
123  if (!empty($classMap) && is_array($classMap)) {
124  $composerClassLoader->addClassMap($classMap);
125  }
126  }
127 
129  if (file_exists($dynamicPsr4File)) {
130  $psr4 = require $dynamicPsr4File;
131  if (is_array($psr4)) {
132  foreach ($psr4 as $prefix => $paths) {
133  $composerClassLoader->setPsr4($prefix, $paths);
134  }
135  }
136  }
137  }
138 
145  public static function ‪registerTransientClassLoadingInformationForPackage(PackageInterface $package)
146  {
147  $composerClassLoader = static::getClassLoader();
148  $activeExtensionPackages = static::getActiveExtensionPackages();
149 
151  $generator = GeneralUtility::makeInstance(ClassLoadingInformationGenerator::class, $composerClassLoader, $activeExtensionPackages, ‪Environment::getPublicPath() . '/', self::isTestingContext());
152 
153  $classInformation = $generator->buildClassLoadingInformationForPackage($package);
154  $composerClassLoader->addClassMap($classInformation['classMap']);
155  foreach ($classInformation['psr-4'] as $prefix => $paths) {
156  $composerClassLoader->setPsr4($prefix, $paths);
157  }
158  $classAliasMap = $generator->buildClassAliasMapForPackage($package);
159  if (is_array($classAliasMap) && !empty($classAliasMap['aliasToClassNameMapping']) && !empty($classAliasMap['classNameToAliasMapping'])) {
160  ClassAliasMap::addAliasMap($classAliasMap);
161  }
162  }
163 
167  protected static function ‪getClassLoadingInformationDirectory()
168  {
169  if (self::isTestingContext()) {
171  }
173  }
174 
181  public static function ‪getClassNameForAlias($alias)
182  {
183  return ClassAliasMap::getClassNameForAlias($alias);
184  }
185 
190  protected static function ‪ensureAutoloadInfoDirExists()
191  {
193  if (!file_exists($autoloadInfoDir)) {
194  GeneralUtility::mkdir_deep($autoloadInfoDir);
195  }
196  }
197 
204  protected static function ‪getClassLoader()
205  {
206  return static::$classLoader;
207  }
208 
215  protected static function ‪isTestingContext()
216  {
217  return GeneralUtility::getApplicationContext()->isTesting();
218  }
219 
225  protected static function ‪getActiveExtensionPackages()
226  {
227  $activeExtensionPackages = [];
228  $packageManager = GeneralUtility::makeInstance(PackageManager::class);
229  foreach ($packageManager->getActivePackages() as $package) {
230  if ($package->getValueFromComposerManifest('type') === 'typo3-cms-framework') {
231  // Skip all core packages as the class loading info is prepared for them already
232  continue;
233  }
234  $activeExtensionPackages[] = $package;
235  }
236  return $activeExtensionPackages;
237  }
238 }
‪TYPO3\CMS\Core\Core\ClassLoadingInformation\AUTOLOAD_PSR4_FILENAME
‪const AUTOLOAD_PSR4_FILENAME
Definition: ClassLoadingInformation.php:51
‪TYPO3\CMS\Core\Core\Environment\getPublicPath
‪static string getPublicPath()
Definition: Environment.php:153
‪TYPO3\CMS\Core\Core\ClassLoadingInformation\AUTOLOAD_CLASSALIASMAP_FILENAME
‪const AUTOLOAD_CLASSALIASMAP_FILENAME
Definition: ClassLoadingInformation.php:56
‪TYPO3\CMS\Core\Core\ClassLoadingInformation\getClassLoader
‪static ClassLoader getClassLoader()
Definition: ClassLoadingInformation.php:203
‪TYPO3\CMS\Core\Core\ClassLoadingInformation\dumpClassLoadingInformation
‪static dumpClassLoadingInformation()
Definition: ClassLoadingInformation.php:87
‪TYPO3\CMS\Core\Core\ClassLoadingInformation\getClassLoadingInformationDirectory
‪static string getClassLoadingInformationDirectory()
Definition: ClassLoadingInformation.php:166
‪TYPO3\CMS\Core\Core\ClassLoadingInformation\isClassLoadingInformationAvailable
‪static bool isClassLoadingInformationAvailable()
Definition: ClassLoadingInformation.php:79
‪TYPO3\CMS\Core\Core\ClassLoadingInformation\ensureAutoloadInfoDirExists
‪static ensureAutoloadInfoDirExists()
Definition: ClassLoadingInformation.php:189
‪TYPO3\CMS\Core\Core\ClassLoadingInformation
Definition: ClassLoadingInformation.php:32
‪TYPO3\CMS\Core\Core\ClassLoadingInformation\getActiveExtensionPackages
‪static PackageInterface[] getActiveExtensionPackages()
Definition: ClassLoadingInformation.php:224
‪TYPO3\CMS\Core\Package\PackageInterface
Definition: PackageInterface.php:21
‪TYPO3\CMS\Core\Core\ClassLoadingInformation\AUTOLOAD_CLASSMAP_FILENAME
‪const AUTOLOAD_CLASSMAP_FILENAME
Definition: ClassLoadingInformation.php:46
‪TYPO3\CMS\Core\Core\ClassLoadingInformation\AUTOLOAD_INFO_DIR
‪const AUTOLOAD_INFO_DIR
Definition: ClassLoadingInformation.php:36
‪TYPO3\CMS\Core\Core\ClassLoadingInformation\getClassNameForAlias
‪static mixed getClassNameForAlias($alias)
Definition: ClassLoadingInformation.php:180
‪TYPO3\CMS\Core\Core\ClassLoadingInformation\AUTOLOAD_INFO_DIR_TESTS
‪const AUTOLOAD_INFO_DIR_TESTS
Definition: ClassLoadingInformation.php:41
‪TYPO3\CMS\Core\Core\ClassLoadingInformation\registerTransientClassLoadingInformationForPackage
‪static registerTransientClassLoadingInformationForPackage(PackageInterface $package)
Definition: ClassLoadingInformation.php:144
‪TYPO3\CMS\Core\Core
Definition: ApplicationContext.php:2
‪TYPO3\CMS\Core\Core\ClassLoadingInformation\registerClassLoadingInformation
‪static registerClassLoadingInformation()
Definition: ClassLoadingInformation.php:107
‪TYPO3\CMS\Core\Core\ClassLoadingInformation\$classLoader
‪static ClassLoader $classLoader
Definition: ClassLoadingInformation.php:60
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Core\Environment\getLegacyConfigPath
‪static string getLegacyConfigPath()
Definition: Environment.php:256
‪TYPO3\CMS\Core\Core\ClassLoadingInformation\setClassLoader
‪static setClassLoader(ClassLoader $classLoader)
Definition: ClassLoadingInformation.php:68
‪TYPO3\CMS\Core\Core\ClassLoadingInformation\isTestingContext
‪static bool isTestingContext()
Definition: ClassLoadingInformation.php:214