/
ClassLoadingInformationGenerator.php
309 lines (281 loc) · 11.9 KB
/
ClassLoadingInformationGenerator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
<?php
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
namespace TYPO3\CMS\Core\Core;
use Composer\Autoload\ClassLoader;
use Composer\Autoload\ClassMapGenerator;
use TYPO3\CMS\Core\Error\Exception;
use TYPO3\CMS\Core\Package\PackageInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
* Generates class loading information (class maps, class aliases etc.) and writes it to files
* for further inclusion in the bootstrap
* @internal
*/
class ClassLoadingInformationGenerator
{
/**
* @var PackageInterface[]
*/
protected $activeExtensionPackages;
/**
* @var ClassLoader
*/
protected $classLoader;
/**
* @var string
*/
protected $installationRoot;
/**
* @var bool
*/
protected $isDevMode;
/**
* @param ClassLoader $classLoader
* @param array $activeExtensionPackages
* @param string $installationRoot
* @param bool $isDevMode
*/
public function __construct(ClassLoader $classLoader, array $activeExtensionPackages, $installationRoot, $isDevMode = false)
{
$this->classLoader = $classLoader;
$this->activeExtensionPackages = $activeExtensionPackages;
$this->installationRoot = $installationRoot;
$this->isDevMode = $isDevMode;
}
/**
* Returns class loading information for a single package
*
* @param PackageInterface $package The package to generate the class loading info for
* @param bool $useRelativePaths If set to TRUE, make the path relative to the current TYPO3 public web path
* @return array
*/
public function buildClassLoadingInformationForPackage(PackageInterface $package, $useRelativePaths = false)
{
$classMap = [];
$psr4 = [];
$packagePath = $package->getPackagePath();
$manifest = $package->getValueFromComposerManifest();
if (empty($manifest->autoload)) {
// Legacy mode: Scan the complete extension directory for class files
$classMap = $this->createClassMap($packagePath, $useRelativePaths, !$this->isDevMode);
} else {
$autoloadPsr4 = $this->getAutoloadSectionFromManifest($manifest, 'psr-4');
if (!empty($autoloadPsr4)) {
foreach ($autoloadPsr4 as $namespacePrefix => $paths) {
foreach ((array)$paths as $path) {
$namespacePath = $packagePath . $path;
$namespaceRealPath = realpath($namespacePath);
if ($useRelativePaths) {
$psr4[$namespacePrefix][] = $this->makePathRelative($namespacePath, $namespaceRealPath);
} else {
$psr4[$namespacePrefix][] = $namespacePath;
}
if (!empty($namespaceRealPath) && is_dir($namespaceRealPath)) {
// Add all prs-4 classes to the class map for improved class loading performance
$classMap = array_merge($classMap, $this->createClassMap($namespacePath, $useRelativePaths, false, $namespacePrefix));
}
}
}
}
$autoloadClassmap = $this->getAutoloadSectionFromManifest($manifest, 'classmap');
if (!empty($autoloadClassmap)) {
foreach ($autoloadClassmap as $path) {
$classMap = array_merge($classMap, $this->createClassMap($packagePath . $path, $useRelativePaths));
}
}
}
return ['classMap' => $classMap, 'psr-4' => $psr4];
}
/**
* Fetches class loading info from the according section from the manifest file.
* Development information will be extracted and merged as well.
*
* @param \stdClass $manifest
* @param string $section
* @return array
*/
protected function getAutoloadSectionFromManifest($manifest, $section)
{
$finalAutoloadSection = [];
$autoloadDefinition = json_decode(json_encode($manifest->autoload), true);
if (!empty($autoloadDefinition[$section]) && is_array($autoloadDefinition[$section])) {
$finalAutoloadSection = $autoloadDefinition[$section];
}
if ($this->isDevMode) {
if (isset($manifest->{'autoload-dev'})) {
$autoloadDefinitionDev = json_decode(json_encode($manifest->{'autoload-dev'}), true);
if (!empty($autoloadDefinitionDev[$section]) && is_array($autoloadDefinitionDev[$section])) {
$finalAutoloadSection = array_merge($finalAutoloadSection, $autoloadDefinitionDev[$section]);
}
}
}
return $finalAutoloadSection;
}
/**
* Creates a class map for a given (absolute) path
*
* @param string $classesPath
* @param bool $useRelativePaths
* @param bool $ignorePotentialTestClasses
* @param string $namespace
* @return array
*/
protected function createClassMap($classesPath, $useRelativePaths = false, $ignorePotentialTestClasses = false, $namespace = null)
{
$classMap = [];
$blacklistExpression = null;
if ($ignorePotentialTestClasses) {
$blacklistPathPrefix = realpath($classesPath);
$blacklistPathPrefix = str_replace('\\', '/', $blacklistPathPrefix);
$blacklistExpression = "{($blacklistPathPrefix/tests/|$blacklistPathPrefix/Tests/|$blacklistPathPrefix/Resources/|$blacklistPathPrefix/res/|$blacklistPathPrefix/class.ext_update.php)}";
}
foreach (ClassMapGenerator::createMap($classesPath, $blacklistExpression, null, $namespace) as $class => $path) {
if ($useRelativePaths) {
$classMap[$class] = $this->makePathRelative($classesPath, $path);
} else {
$classMap[$class] = $path;
}
}
return $classMap;
}
/**
* Returns class alias map for given package
*
* @param PackageInterface $package The package to generate the class alias info for
* @throws \TYPO3\CMS\Core\Error\Exception
* @return array
*/
public function buildClassAliasMapForPackage(PackageInterface $package)
{
$aliasToClassNameMapping = [];
$classNameToAliasMapping = [];
$possibleClassAliasFiles = [];
$manifest = $package->getValueFromComposerManifest();
if (!empty($manifest->extra->{'typo3/class-alias-loader'}->{'class-alias-maps'})) {
$possibleClassAliasFiles = $manifest->extra->{'typo3/class-alias-loader'}->{'class-alias-maps'};
if (!is_array($possibleClassAliasFiles)) {
throw new Exception('"typo3/class-alias-loader"/"class-alias-maps" must return an array!', 1444142481);
}
} else {
$possibleClassAliasFiles[] = 'Migrations/Code/ClassAliasMap.php';
}
$packagePath = $package->getPackagePath();
foreach ($possibleClassAliasFiles as $possibleClassAliasFile) {
$possiblePathToClassAliasFile = $packagePath . $possibleClassAliasFile;
if (file_exists($possiblePathToClassAliasFile)) {
$packageAliasMap = require $possiblePathToClassAliasFile;
if (!is_array($packageAliasMap)) {
throw new Exception('"class alias maps" must return an array', 1422625075);
}
foreach ($packageAliasMap as $aliasClassName => $className) {
$lowerCasedAliasClassName = strtolower($aliasClassName);
$aliasToClassNameMapping[$lowerCasedAliasClassName] = $className;
$classNameToAliasMapping[$className][$lowerCasedAliasClassName] = $lowerCasedAliasClassName;
}
}
}
return ['aliasToClassNameMapping' => $aliasToClassNameMapping, 'classNameToAliasMapping' => $classNameToAliasMapping];
}
/**
* Generate the class map file
* @return string[]
* @internal
*/
public function buildAutoloadInformationFiles()
{
$psr4File = $classMapFile = <<<EOF
<?php
// autoload_classmap.php @generated by TYPO3
\$typo3InstallDir = \TYPO3\CMS\Core\Core\Environment::getPublicPath() . '/';
return array(
EOF;
$classMap = [];
$psr4 = [];
foreach ($this->activeExtensionPackages as $package) {
$classLoadingInformation = $this->buildClassLoadingInformationForPackage($package, true);
$classMap = array_merge($classMap, $classLoadingInformation['classMap']);
$psr4 = array_merge($psr4, $classLoadingInformation['psr-4']);
}
ksort($classMap);
ksort($psr4);
foreach ($classMap as $class => $relativePath) {
$classMapFile .= sprintf(' %s => %s,', var_export($class, true), $this->getPathCode($relativePath)) . "\n";
}
$classMapFile .= ");\n";
foreach ($psr4 as $prefix => $relativePaths) {
$psr4File .= sprintf(' %s => array(%s),', var_export($prefix, true), implode(',', array_map([$this, 'getPathCode'], $relativePaths))) . "\n";
}
$psr4File .= ");\n";
return ['classMapFile' => $classMapFile, 'psr-4File' => $psr4File];
}
/**
* Generate a relative path string from an absolute path within a give package path
*
* @param string $packagePath
* @param string $realPathOfClassFile
* @param bool $relativeToRoot
* @return string
*/
protected function makePathRelative($packagePath, $realPathOfClassFile, $relativeToRoot = true)
{
$realPathOfClassFile = GeneralUtility::fixWindowsFilePath($realPathOfClassFile);
$packageRealPath = GeneralUtility::fixWindowsFilePath(realpath($packagePath));
$relativePackagePath = rtrim(substr($packagePath, strlen($this->installationRoot)), '/');
if ($relativeToRoot) {
if ($realPathOfClassFile === $packageRealPath) {
$relativePathToClassFile = $relativePackagePath;
} else {
$relativePathToClassFile = $relativePackagePath . '/' . ltrim(substr($realPathOfClassFile, strlen($packageRealPath)), '/');
}
} else {
$relativePathToClassFile = ltrim(substr($realPathOfClassFile, strlen($packageRealPath)), '/');
}
return $relativePathToClassFile;
}
/**
* Generate a relative path string from a relative path
*
* @param string $relativePathToClassFile
* @return string
*/
protected function getPathCode($relativePathToClassFile)
{
return '$typo3InstallDir . ' . var_export($relativePathToClassFile, true);
}
/**
* Build class alias mapping file
*
* @return string
* @throws \Exception
* @internal
*/
public function buildClassAliasMapFile()
{
$aliasToClassNameMapping = [];
$classNameToAliasMapping = [];
foreach ($this->activeExtensionPackages as $package) {
$aliasMappingForPackage = $this->buildClassAliasMapForPackage($package);
$aliasToClassNameMapping = array_merge($aliasToClassNameMapping, $aliasMappingForPackage['aliasToClassNameMapping']);
$classNameToAliasMapping = array_merge($classNameToAliasMapping, $aliasMappingForPackage['classNameToAliasMapping']);
}
$exportArray = [
'aliasToClassNameMapping' => $aliasToClassNameMapping,
'classNameToAliasMapping' => $classNameToAliasMapping
];
$fileContent = "<?php\nreturn ";
$fileContent .= var_export($exportArray, true);
$fileContent .= ";\n";
return $fileContent;
}
}