/
PackageArtifactBuilder.php
281 lines (260 loc) · 13 KB
/
PackageArtifactBuilder.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
<?php
declare(strict_types=1);
/*
* 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\Composer;
use Composer\Package\PackageInterface;
use Composer\Repository\PlatformRepository;
use Composer\Script\Event;
use Composer\Util\Filesystem;
use Composer\Util\Platform;
use TYPO3\CMS\Composer\Plugin\Config;
use TYPO3\CMS\Composer\Plugin\Core\InstallerScript;
use TYPO3\CMS\Composer\Plugin\Util\ExtensionKeyResolver;
use TYPO3\CMS\Core\Package\Cache\ComposerPackageArtifact;
use TYPO3\CMS\Core\Package\Exception\InvalidPackageKeyException;
use TYPO3\CMS\Core\Package\Exception\InvalidPackageManifestException;
use TYPO3\CMS\Core\Package\Exception\InvalidPackagePathException;
use TYPO3\CMS\Core\Package\Exception\InvalidPackageStateException;
use TYPO3\CMS\Core\Package\Package;
use TYPO3\CMS\Core\Package\PackageManager;
use TYPO3\CMS\Core\Service\DependencyOrderingService;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\PathUtility;
/**
* The builder is a subclass of PackageManager as it shares much of its functionality.
* It evaluates the installed Composer packages for applicable TYPO3 extensions.
* All Composer packages will be discovered, that have an extra.typo3/cms definition in their composer.json.
* All ext_emconf.php files will be completely ignored in this context, which means all extensions
* are required to have a composer.json file, which works out naturally with a Composer setup.
*
* @internal This class is an implementation detail and does not represent public API
*/
class PackageArtifactBuilder extends PackageManager implements InstallerScript
{
private const LEGACY_EXTENSION_INSTALL_PATH = '/typo3conf/ext';
/**
* @var Event $event
*/
private $event;
/**
* @var Config $config
*/
private $config;
/**
* Array of Composer package names (as array key) that are installed by Composer but have no relation to TYPO3 extension API
* @var array
*/
private $availableComposerPackageKeys = [];
public function __construct()
{
// Disable path determination with Environment class, which is not initialized here
parent::__construct(new DependencyOrderingService(), '', '');
}
protected function isComposerDependency(string $packageName): bool
{
return PlatformRepository::isPlatformPackage($packageName) || ($this->availableComposerPackageKeys[$packageName] ?? false);
}
/**
* Entry method called in Composer post-dump-autoload hook
*
* @throws InvalidPackageKeyException
* @throws InvalidPackageManifestException
* @throws InvalidPackagePathException
* @throws InvalidPackageStateException
*/
public function run(Event $event): bool
{
$this->event = $event;
$this->config = Config::load($this->event->getComposer(), $this->event->getIO());
$composer = $this->event->getComposer();
$basePath = $this->config->get('base-dir');
$this->packagesBasePath = $basePath . '/';
foreach ($this->extractPackageMapFromComposer() as [$composerPackage, $path, $extensionKey]) {
$packagePath = PathUtility::sanitizeTrailingSeparator($path);
$package = new Package($this, $extensionKey, $packagePath, true);
$this->setTitleFromExtEmConf($package);
$package->makePathRelative(new Filesystem(), $basePath);
$package->getPackageMetaData()->setVersion($composerPackage->getPrettyVersion());
$this->registerPackage($package);
}
$this->sortPackagesAndConfiguration();
$cacheIdentifier = md5(serialize($composer->getLocker()->getLockData()) . $this->event->isDevMode());
$this->setPackageCache(new ComposerPackageArtifact($composer->getConfig()->get('vendor-dir') . '/typo3', new Filesystem(), $cacheIdentifier));
$this->saveToPackageCache();
return true;
}
/**
* Sets a title for the package from ext_emconf.php in case this file exists
* @todo deprecate or remove in TYPO3 v12
*/
private function setTitleFromExtEmConf(Package $package): void
{
$emConfPath = $package->getPackagePath() . '/ext_emconf.php';
if (file_exists($emConfPath)) {
$_EXTKEY = $package->getPackageKey();
$EM_CONF = null;
include $emConfPath;
if (!empty($EM_CONF[$_EXTKEY]['title'])) {
$package->getPackageMetaData()->setTitle($EM_CONF[$_EXTKEY]['title']);
}
}
}
/**
* Sorts all TYPO3 extension packages by dependency defined in composer.json file
*/
private function sortPackagesAndConfiguration(): void
{
$packagesWithDependencies = $this->resolvePackageDependencies($this->packages);
// Sort the packages by key at first, so we get a stable sorting of "equivalent" packages afterwards
ksort($packagesWithDependencies);
$sortedPackageKeys = $this->sortPackageStatesConfigurationByDependency($packagesWithDependencies);
$this->packageStatesConfiguration = [];
$sortedPackages = [];
foreach ($sortedPackageKeys as $packageKey) {
$sortedPackages[$packageKey] = $this->packages[$packageKey];
// The artifact does not need path information, so it is kept empty
// The keys must be present, though because the PackageManager implies than a
// package is active by this configuration array
$this->packageStatesConfiguration['packages'][$packageKey] = [];
}
$this->packages = $sortedPackages;
$this->packageStatesConfiguration['version'] = 5;
}
/**
* Fetch a map of all installed packages and filter them, when they apply
* for TYPO3.
*/
private function extractPackageMapFromComposer(): array
{
$composer = $this->event->getComposer();
$rootPackage = $composer->getPackage();
$autoLoadGenerator = $composer->getAutoloadGenerator();
$localRepo = $composer->getRepositoryManager()->getLocalRepository();
$usedExtensionKeys = [];
$installedTypo3Packages = array_map(
function (array $packageAndPath) use ($rootPackage, &$usedExtensionKeys) {
[$composerPackage, $packagePath] = $packageAndPath;
$packageName = $composerPackage->getName();
$packagePath = GeneralUtility::fixWindowsFilePath($packagePath);
try {
$extensionKey = ExtensionKeyResolver::resolve($composerPackage);
} catch (\Throwable $e) {
if (str_starts_with($composerPackage->getType(), 'typo3-cms-')) {
// This means we have a package of type extension, and it does not have the extension key set
// This only happens since version > 4.0 of the installer and must be propagated to become user facing
throw $e;
}
// In case we can not otherwise determine the extension key, we take the composer name
$extensionKey = $packageName;
}
if (isset($usedExtensionKeys[$extensionKey])) {
throw new \UnexpectedValueException(
sprintf(
'Package with the name "%s" registered extension key "%s", but this key was already set by package with the name "%s"',
$packageName,
$extensionKey,
$usedExtensionKeys[$extensionKey]
),
1638880941
);
}
$usedExtensionKeys[$extensionKey] = $packageName;
unset($this->availableComposerPackageKeys[$packageName]);
$this->composerNameToPackageKeyMap[$packageName] = $extensionKey;
if ($composerPackage === $rootPackage) {
return $this->handleRootPackage($rootPackage, $extensionKey);
}
// Add extension key to the package map for later reference
return [$composerPackage, $packagePath, $extensionKey];
},
array_filter(
$autoLoadGenerator->buildPackageMap($composer->getInstallationManager(), $rootPackage, $localRepo->getCanonicalPackages()),
function (array $packageAndPath) {
/** @var PackageInterface $composerPackage */
[$composerPackage] = $packageAndPath;
// Filter all Composer packages without typo3/cms definition, but keep all
// package names, to be able to ignore Composer only dependencies when ordering the packages
$this->availableComposerPackageKeys[$composerPackage->getName()] = true;
foreach ($composerPackage->getReplaces() as $link) {
$this->availableComposerPackageKeys[$link->getTarget()] = true;
}
return isset($composerPackage->getExtra()['typo3/cms']);
}
)
);
$this->publishResources($installedTypo3Packages);
return $installedTypo3Packages;
}
/**
* TYPO3 can not handle public resources of extensions, that do not reside in typo3conf/ext
* Therefore, if the root package is of type typo3-cms-extension and has the folder Resources/Public,
* we fake the path of this extension to be in typo3conf/ext
*
* For root packages of other types or extensions without public resources, no symlink is created
* and the package path stays to be the composer root path.
*
* If extensions are installed into vendor folder, linking is skipped, because public resources
* are published anyway.
* Linking could be skipped altogether, but is kept to stay consistent:
* extensions in typo3conf/ext: root package is linked
* extensions in vendor: public resources of all packages are published
* @todo: remove this method in TYPO3 v12
*
* @param PackageInterface $rootPackage
* @param string $extensionKey
*/
private function handleRootPackage(PackageInterface $rootPackage, string $extensionKey): array
{
$baseDir = $this->config->get('base-dir');
$composer = $this->event->getComposer();
if ($rootPackage->getType() !== 'typo3-cms-extension'
|| !file_exists($baseDir . '/Resources/Public/')
) {
return [$rootPackage, $baseDir, $extensionKey];
}
$typo3ExtensionInstallPath = $composer->getInstallationManager()->getInstaller('typo3-cms-extension')->getInstallPath($rootPackage);
if (!str_contains($typo3ExtensionInstallPath, self::LEGACY_EXTENSION_INSTALL_PATH)) {
return [$rootPackage, $baseDir, $extensionKey];
}
$filesystem = new Filesystem();
if (!file_exists($typo3ExtensionInstallPath) && !$filesystem->isSymlinkedDirectory($typo3ExtensionInstallPath)) {
$filesystem->ensureDirectoryExists(dirname($typo3ExtensionInstallPath));
$filesystem->relativeSymlink($baseDir, $typo3ExtensionInstallPath);
}
if (realpath($baseDir) !== realpath($typo3ExtensionInstallPath)) {
$this->event->getIO()->warning('The root package is of type "typo3-cms-extension" and has public resources, but could not be linked to "' . self::LEGACY_EXTENSION_INSTALL_PATH . '" directory, because target directory already exits.');
}
return [$rootPackage, $typo3ExtensionInstallPath, $extensionKey];
}
private function publishResources(array $installedTypo3Packages): void
{
$fileSystem = new Filesystem();
$baseDir = $this->config->get('base-dir');
foreach ($installedTypo3Packages as [$composerPackage, $path, $extensionKey]) {
$fileSystemResourcesPath = $path . '/Resources/Public';
if (str_contains($path, 'ext/' . $extensionKey) || !file_exists($fileSystemResourcesPath)) {
continue;
}
$relativePath = substr($fileSystemResourcesPath, strlen($baseDir));
[$relativePrefix] = explode('Resources/Public', $relativePath);
$publicResourcesPath = $fileSystem->normalizePath($this->config->get('web-dir') . '/_assets/' . md5($relativePrefix));
$fileSystem->ensureDirectoryExists(dirname($publicResourcesPath));
if (Platform::isWindows() && !$fileSystem->isJunction($publicResourcesPath)) {
$fileSystem->junction($fileSystemResourcesPath, $publicResourcesPath);
} elseif (!$fileSystem->isSymlinkedDirectory($publicResourcesPath)) {
$fileSystem->relativeSymlink($fileSystemResourcesPath, $publicResourcesPath);
}
}
}
}