‪TYPO3CMS  ‪main
ClassLoadingInformation.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
18 namespace ‪TYPO3\CMS\Core\Core;
19 
20 use Composer\Autoload\ClassLoader;
21 use TYPO3\ClassAliasLoader\ClassAliasMap;
23 use TYPO3\CMS\Core\Package\PackageManager;
25 
35 {
39  public const ‪AUTOLOAD_INFO_DIR = 'autoload/';
40 
44  public const ‪AUTOLOAD_INFO_DIR_TESTS = 'autoload-tests/';
45 
49  public const ‪AUTOLOAD_CLASSMAP_FILENAME = 'autoload_classmap.php';
50 
54  public const ‪AUTOLOAD_PSR4_FILENAME = 'autoload_psr4.php';
55 
59  public const ‪AUTOLOAD_CLASSALIASMAP_FILENAME = 'autoload_classaliasmap.php';
60 
64  protected static ‪$classLoader;
65 
71  public static function ‪setClassLoader(ClassLoader ‪$classLoader)
72  {
73  static::$classLoader = ‪$classLoader;
74  }
75 
82  public static function ‪isClassLoadingInformationAvailable()
83  {
84  return file_exists(self::getClassLoadingInformationDirectory() . self::AUTOLOAD_CLASSMAP_FILENAME);
85  }
86 
90  public static function ‪dumpClassLoadingInformation()
91  {
93  $composerClassLoader = static::getClassLoader();
94  $activeExtensionPackages = static::getActiveExtensionPackages();
95 
96  $generator = GeneralUtility::makeInstance(ClassLoadingInformationGenerator::class, $composerClassLoader, $activeExtensionPackages, ‪Environment::getPublicPath() . '/', self::isTestingContext());
97  $classInfoFiles = $generator->buildAutoloadInformationFiles();
98  ‪GeneralUtility::writeFile(self::getClassLoadingInformationDirectory() . self::AUTOLOAD_CLASSMAP_FILENAME, $classInfoFiles['classMapFile']);
99  ‪GeneralUtility::writeFile(self::getClassLoadingInformationDirectory() . self::AUTOLOAD_PSR4_FILENAME, $classInfoFiles['psr-4File']);
100 
101  $classAliasMapFile = $generator->buildClassAliasMapFile();
102  ‪GeneralUtility::writeFile(self::getClassLoadingInformationDirectory() . self::AUTOLOAD_CLASSALIASMAP_FILENAME, $classAliasMapFile);
103  }
104 
109  public static function ‪registerClassLoadingInformation()
110  {
111  $composerClassLoader = static::getClassLoader();
112 
114  if (file_exists($dynamicClassAliasMapFile)) {
115  $classAliasMap = require $dynamicClassAliasMapFile;
116  if (is_array($classAliasMap) && !empty($classAliasMap['aliasToClassNameMapping']) && !empty($classAliasMap['classNameToAliasMapping'])) {
117  ClassAliasMap::addAliasMap($classAliasMap);
118  }
119  }
120 
122  if (file_exists($dynamicClassMapFile)) {
123  $classMap = require $dynamicClassMapFile;
124  if (!empty($classMap) && is_array($classMap)) {
125  $composerClassLoader->addClassMap($classMap);
126  }
127  }
128 
130  if (file_exists($dynamicPsr4File)) {
131  $psr4 = require $dynamicPsr4File;
132  if (is_array($psr4)) {
133  foreach ($psr4 as $prefix => $paths) {
134  $composerClassLoader->setPsr4($prefix, $paths);
135  }
136  }
137  }
138  }
139 
145  public static function ‪registerTransientClassLoadingInformationForPackage(PackageInterface $package)
146  {
147  $composerClassLoader = static::getClassLoader();
148  $activeExtensionPackages = static::getActiveExtensionPackages();
149 
150  $generator = GeneralUtility::makeInstance(ClassLoadingInformationGenerator::class, $composerClassLoader, $activeExtensionPackages, ‪Environment::getPublicPath() . '/', self::isTestingContext());
151 
152  $classInformation = $generator->buildClassLoadingInformationForPackage($package);
153  $composerClassLoader->addClassMap($classInformation['classMap']);
154  foreach ($classInformation['psr-4'] as $prefix => $paths) {
155  $composerClassLoader->setPsr4($prefix, $paths);
156  }
157  $classAliasMap = $generator->buildClassAliasMapForPackage($package);
158  if (is_array($classAliasMap) && !empty($classAliasMap['aliasToClassNameMapping']) && !empty($classAliasMap['classNameToAliasMapping'])) {
159  ClassAliasMap::addAliasMap($classAliasMap);
160  }
161  }
162 
166  protected static function ‪getClassLoadingInformationDirectory()
167  {
168  if (self::isTestingContext()) {
170  }
172  }
173 
180  public static function ‪getClassNameForAlias($alias)
181  {
182  return ClassAliasMap::getClassNameForAlias($alias);
183  }
184 
189  protected static function ‪ensureAutoloadInfoDirExists()
190  {
192  if (!file_exists($autoloadInfoDir)) {
193  ‪GeneralUtility::mkdir_deep($autoloadInfoDir);
194  }
195  }
196 
203  public static function ‪getClassLoader()
204  {
205  return static::$classLoader;
206  }
207 
214  protected static function ‪isTestingContext()
215  {
216  return ‪Environment::getContext()->isTesting();
217  }
218 
224  protected static function ‪getActiveExtensionPackages()
225  {
226  $activeExtensionPackages = [];
227  $packageManager = GeneralUtility::makeInstance(PackageManager::class);
228  foreach ($packageManager->getActivePackages() as $package) {
229  if ($package->getPackageMetaData()->isFrameworkType()) {
230  // Skip all core packages as the class loading info is prepared for them already
231  continue;
232  }
233  $activeExtensionPackages[] = $package;
234  }
235  return $activeExtensionPackages;
236  }
237 }
‪TYPO3\CMS\Core\Core\ClassLoadingInformation\AUTOLOAD_PSR4_FILENAME
‪const AUTOLOAD_PSR4_FILENAME
Definition: ClassLoadingInformation.php:54
‪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:202
‪TYPO3\CMS\Core\Core\Environment\getPublicPath
‪static getPublicPath()
Definition: Environment.php:187
‪TYPO3\CMS\Core\Core\ClassLoadingInformation\dumpClassLoadingInformation
‪static dumpClassLoadingInformation()
Definition: ClassLoadingInformation.php:89
‪TYPO3\CMS\Core\Core\ClassLoadingInformation\getClassLoadingInformationDirectory
‪static string getClassLoadingInformationDirectory()
Definition: ClassLoadingInformation.php:165
‪TYPO3\CMS\Core\Core\ClassLoadingInformation\isClassLoadingInformationAvailable
‪static bool isClassLoadingInformationAvailable()
Definition: ClassLoadingInformation.php:81
‪TYPO3\CMS\Core\Core\ClassLoadingInformation\ensureAutoloadInfoDirExists
‪static ensureAutoloadInfoDirExists()
Definition: ClassLoadingInformation.php:188
‪TYPO3\CMS\Core\Core\ClassLoadingInformation
Definition: ClassLoadingInformation.php:35
‪TYPO3\CMS\Core\Utility\GeneralUtility\mkdir_deep
‪static mkdir_deep(string $directory)
Definition: GeneralUtility.php:1652
‪TYPO3\CMS\Core\Core\ClassLoadingInformation\getActiveExtensionPackages
‪static PackageInterface[] getActiveExtensionPackages()
Definition: ClassLoadingInformation.php:223
‪TYPO3\CMS\Core\Core\Environment\getLegacyConfigPath
‪static getLegacyConfigPath()
Definition: Environment.php:268
‪TYPO3\CMS\Core\Package\PackageInterface
Definition: PackageInterface.php:24
‪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\Utility\GeneralUtility\writeFile
‪static bool writeFile(string $file, string $content, bool $changePermissions=false)
Definition: GeneralUtility.php:1467
‪TYPO3\CMS\Core\Core\ClassLoadingInformation\getClassNameForAlias
‪static class string getClassNameForAlias($alias)
Definition: ClassLoadingInformation.php:179
‪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:144
‪TYPO3\CMS\Core\Core
Definition: ApplicationContext.php:16
‪TYPO3\CMS\Core\Core\ClassLoadingInformation\registerClassLoadingInformation
‪static registerClassLoadingInformation()
Definition: ClassLoadingInformation.php:108
‪TYPO3\CMS\Core\Core\ClassLoadingInformation\$classLoader
‪static ClassLoader $classLoader
Definition: ClassLoadingInformation.php:63
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Core\Environment\getContext
‪static getContext()
Definition: Environment.php:128
‪TYPO3\CMS\Core\Core\ClassLoadingInformation\setClassLoader
‪static setClassLoader(ClassLoader $classLoader)
Definition: ClassLoadingInformation.php:70
‪TYPO3\CMS\Core\Core\ClassLoadingInformation\isTestingContext
‪static bool isTestingContext()
Definition: ClassLoadingInformation.php:213