Skip to content

Commit

Permalink
[TASK] Include ext_emconf.php data in PackageInfo
Browse files Browse the repository at this point in the history
TYPO3 extensions may have either one or both of
`composer.json` and `ext_emconf.php` available.

This change now includes both information in the
`PackageInfo` structure as further preparation to
ensure backwards compatibility for test fixture
extensions without a proper `composer.json` file.

Releases: main, 7
  • Loading branch information
sbuerk committed May 11, 2023
1 parent 44d7d20 commit 91e010f
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 9 deletions.
79 changes: 73 additions & 6 deletions Classes/Composer/ComposerPackageManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,20 @@ private function processRootPackage(): void
$packageName = $package['name'];
$packagePath = $this->getPackageInstallPath($packageName);
$packageRealPath = $this->realPath($packagePath);
$info = $this->getPackageComposerJson($packagePath) ?? [];
$info = $this->getPackageComposerJson($packagePath);
$packageType = $info['type'] ?? '';
$extEmConf = $this->getExtEmConf($packagePath);
$extensionKey = $this->determineExtensionKey($packagePath, $info, $extEmConf);

$packageInfo = new PackageInfo(
$packageName,
$packageType,
$packagePath,
$packageRealPath,
$package['pretty_version'],
$info
$extensionKey,
$info,
$extEmConf
);
self::$rootPackage = $packageInfo;
$this->addPackageInfo($packageInfo);
Expand Down Expand Up @@ -159,14 +163,18 @@ private function processMonoRepository(): void
$info = $this->getPackageComposerJson($packageRealPath);
$packageName = $info['name'] ?? '';
$packageType = $info['type'] ?? '';
$extEmConf = $this->getExtEmConf($packageRealPath);
$extensionKey = $this->determineExtensionKey($packageRealPath, $info, $extEmConf);
$packageInfo = new PackageInfo(
$packageName,
$packageType,
$packagePath,
$packageRealPath,
// System extensions in mono-repository are exactly the same version as the root package. Use it.
$this->rootPackage()->getVersion(),
$info
$extensionKey,
$info,
$extEmConf,
);
if (!$packageInfo->isSystemExtension()) {
continue;
Expand All @@ -184,15 +192,19 @@ private function processPackages(): void
foreach ($loader['versions'] as $packageName => $version) {
$packagePath = $this->getPackageInstallPath($packageName);
$packageRealPath = $this->realPath($packagePath);
$info = $this->getPackageComposerJson($packagePath) ?? [];
$info = $this->getPackageComposerJson($packagePath);
$extEmConf = $this->getExtEmConf($packagePath);
$packageType = $info['type'] ?? '';
$extensionKey = $this->determineExtensionKey($packagePath, $info, $extEmConf);
$this->addPackageInfo(new PackageInfo(
$packageName,
$packageType,
$packagePath,
$packageRealPath,
(string)($version['pretty_version'] ?? ''),
$info
(string)($version['pretty_version'] ?? $this->prettifyVersion($extEmConf['version'] ?? '')),
$extensionKey,
$info,
$extEmConf
));
}
}
Expand Down Expand Up @@ -222,6 +234,9 @@ private function rootPackage(): ?PackageInfo

private function getPackageComposerJson(string $path): ?array
{
if ($path === '') {
return null;
}
$composerFile = rtrim($path, '/') . '/composer.json';
if (!file_exists($composerFile) || !is_readable($composerFile)) {
return null;
Expand All @@ -234,6 +249,28 @@ private function getPackageComposerJson(string $path): ?array
return null;
}

private function getExtEmConf(string $path): ?array
{
if ($path === '') {
return null;
}
$extEmConfFile = rtrim($path, '/') . 'ext_emconf.php';
if (!file_exists($extEmConfFile) || !is_readable($extEmConfFile)) {
return null;
}

try {
/** @var array<non-empty-string, array> $EM_CONF */
$EM_CONF = [];
$_EXTKEY = '__EXTKEY__';
@include $extEmConfFile;
return $EM_CONF[$_EXTKEY] ?? null;
} catch (\Throwable $t) {
}

return null;
}

private function resolvePackageName(string $name): string
{
if (str_starts_with($name, 'typo3conf/ext/')
Expand Down Expand Up @@ -375,4 +412,34 @@ private function findCanonicalParts(string $root, string $pathWithoutRoot): arra

return $canonicalParts;
}

private function determineExtensionKey(
string $packagePath,
?array $info = null,
?array $extEmConf = null
): string {
$isExtension = in_array($info['type'] ?? '', ['typo3-cms-framework', 'typo3-cms-extension'], true)
|| ($extEmConf !== null);
if (!$isExtension) {
return '';
}
$baseName = basename($packagePath);
return $info['extra']['typo3/cms']['extension-key'] ?? $baseName;
}

private function prettifyVersion(string $version): string
{
if ($version === '') {
return '';
}
$parts = array_pad(explode('.', $version), 3, '0');
return implode(
'.',
[
$parts[0] ?? '0',
$parts[1] ?? '0',
$parts[2] ?? '0',
],
);
}
}
27 changes: 24 additions & 3 deletions Classes/Composer/PackageInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,28 @@ final class PackageInfo
private string $path;
private string $realPath;
private string $version;
private array $info;
private string $extensionKey;
private ?array $info = null;
private ?array $extEmConf = null;

public function __construct(
string $name,
string $type,
string $path,
string $realPath,
string $version,
array $info
string $extensionKey,
?array $info = null,
?array $extEmConf = null
) {
$this->name = $name;
$this->type = $type;
$this->path = $path;
$this->realPath = $realPath;
$this->version = $version;
$this->extensionKey = $extensionKey;
$this->info = $info;
$this->extEmConf = $extEmConf;
}

public function getName(): string
Expand Down Expand Up @@ -70,6 +76,16 @@ public function getVersion(): string
return $this->version;
}

public function getInfo(): ?array
{
return $this->info;
}

public function getExtEmConf(): ?array
{
return $this->extEmConf;
}

public function isSystemExtension(): bool
{
return $this->type === 'typo3-cms-framework';
Expand All @@ -85,9 +101,14 @@ public function isMonoRepository(): bool
return $this->type === 'typo3-cms-core';
}

public function isComposerPackage(): bool
{
return $this->info !== null;
}

public function getExtensionKey(): string
{
return (string)($this->info['extra']['typo3/cms']['extension-key'] ?? '');
return $this->extensionKey;
}

public function getVendorDir(): string
Expand Down

0 comments on commit 91e010f

Please sign in to comment.