‪TYPO3CMS  ‪main
Package.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 
19 
20 use Composer\Util\Filesystem;
25 
30 {
35  protected bool ‪$partOfFactoryDefault = false;
36 
41  protected bool ‪$partOfMinimalUsableSystem = false;
42 
49  protected ?string ‪$serviceProvider;
50 
54  protected string ‪$packageKey;
55 
59  protected string ‪$packagePath;
60 
61  protected bool ‪$isRelativePackagePath = false;
62 
66  protected bool ‪$protected = false;
67 
68  protected ?\stdClass ‪$composerManifest;
69 
74 
84  public function ‪__construct(PackageManager $packageManager, string ‪$packageKey, string ‪$packagePath, bool $ignoreExtEmConf = false)
85  {
86  if (!$packageManager->isPackageKeyValid(‪$packageKey)) {
87  throw new ‪InvalidPackageKeyException('"' . ‪$packageKey . '" is not a valid package key.', 1217959511);
88  }
89  if (!(@is_dir(‪$packagePath) || (is_link(‪$packagePath) && is_dir(‪$packagePath)))) {
90  throw new ‪InvalidPackagePathException(sprintf('Tried to instantiate a package object for package "%s" with a non-existing package path "%s". Either the package does not exist anymore, or the code creating this object contains an error.', ‪$packageKey, ‪$packagePath), 1166631890);
91  }
92  if (!str_ends_with(‪$packagePath, '/')) {
93  throw new ‪InvalidPackagePathException(sprintf('The package path "%s" provided for package "%s" has no trailing forward slash.', ‪$packagePath, ‪$packageKey), 1166633722);
94  }
95  $this->packageKey = ‪$packageKey;
96  $this->packagePath = ‪$packagePath;
97  $this->composerManifest = $packageManager->getComposerManifest($this->packagePath, $ignoreExtEmConf);
99  $this->‪createPackageMetaData($packageManager);
100  }
101 
106  protected function ‪loadFlagsFromComposerManifest(): void
107  {
108  $extraFlags = $this->‪getValueFromComposerManifest('extra');
109  if ($extraFlags !== null && isset($extraFlags->{'typo3/cms'}->{'Package'})) {
110  foreach ($extraFlags->{'typo3/cms'}->{'Package'} as $flagName => $flagValue) {
111  if (property_exists($this, $flagName)) {
112  $this->{$flagName} = $flagValue;
113  }
114  }
115  }
116  }
117 
121  protected function ‪createPackageMetaData(PackageManager $packageManager): void
122  {
123  $this->packageMetaData = new ‪MetaData($this->getPackageKey());
124  $description = (string)$this->getValueFromComposerManifest('description');
125  $this->packageMetaData->setDescription($description);
126  $this->packageMetaData->setTitle($this->getValueFromComposerManifest('title') ?? $description);
127  $this->packageMetaData->setVersion((string)$this->getValueFromComposerManifest('version'));
128  $this->packageMetaData->setPackageType((string)$this->getValueFromComposerManifest('type'));
129  $requirements = $this->getValueFromComposerManifest('require');
130  if ($requirements !== null) {
131  foreach ($requirements as $requirement => $version) {
132  $packageKey = $packageManager->getPackageKeyFromComposerName($requirement);
133  $constraint = new ‪PackageConstraint(MetaData::CONSTRAINT_TYPE_DEPENDS, $packageKey);
134  $this->packageMetaData->addConstraint($constraint);
135  }
136  }
137  $suggestions = $this->getValueFromComposerManifest('suggest');
138  if ($suggestions !== null) {
139  foreach ($suggestions as $suggestion => $version) {
140  $packageKey = $packageManager->getPackageKeyFromComposerName($suggestion);
141  $constraint = new ‪PackageConstraint(MetaData::CONSTRAINT_TYPE_SUGGESTS, $packageKey);
142  $this->packageMetaData->addConstraint($constraint);
143  }
144  }
145  }
146 
152  public function ‪getServiceProvider(): string
153  {
154  return $this->serviceProvider ?? PseudoServiceProvider::class;
155  }
156 
160  public function ‪isPartOfFactoryDefault(): bool
161  {
162  return $this->partOfFactoryDefault;
163  }
164 
168  public function ‪isPartOfMinimalUsableSystem(): bool
169  {
170  return $this->partOfMinimalUsableSystem;
171  }
172 
176  public function ‪getPackageKey(): string
177  {
178  return $this->packageKey;
179  }
180 
184  public function ‪isProtected(): bool
185  {
186  return $this->protected;
187  }
188 
194  public function ‪setProtected(bool $protected): void
195  {
196  $this->protected = (bool)$protected;
197  }
198 
204  public function ‪getPackagePath(): string
205  {
206  if (!$this->isRelativePackagePath) {
207  return $this->packagePath;
208  }
209  $this->isRelativePackagePath = false;
210 
211  return $this->packagePath = Environment::getComposerRootPath() . '/' . $this->packagePath;
212  }
213 
219  public function ‪makePathRelative(Filesystem $filesystem, string $composerRootPath): void
220  {
221  $this->isRelativePackagePath = true;
222  $this->packagePath = ($composerRootPath . '/') === $this->packagePath ? '' : $filesystem->findShortestPath($composerRootPath, $this->packagePath, true) . '/';
223  }
224 
231  {
232  return $this->packageMetaData;
233  }
234 
239  public function ‪getPackageReplacementKeys(): array
240  {
241  // The cast to array is required since the manifest returns data with type mixed
242  return (array)$this->getValueFromComposerManifest('replace') ?: [];
243  }
244 
252  public function ‪getValueFromComposerManifest($key = null): mixed
253  {
254  if ($key === null) {
255  return $this->composerManifest;
256  }
257 
258  if (isset($this->composerManifest->{$key})) {
259  $value = $this->composerManifest->{$key};
260  } else {
261  $value = null;
262  }
263  return $value;
264  }
265 
269  public function ‪getPackageIcon(): ?string
270  {
271  $resourcePath = 'Resources/Public/Icons/Extension.';
272  foreach (['svg', 'png', 'gif'] as $fileExtension) {
273  if (file_exists($this->getPackagePath() . $resourcePath . $fileExtension)) {
274  return $resourcePath . $fileExtension;
275  }
276  }
277  return null;
278  }
279 }
‪TYPO3\CMS\Core\Package\Package\__construct
‪__construct(PackageManager $packageManager, string $packageKey, string $packagePath, bool $ignoreExtEmConf=false)
Definition: Package.php:84
‪TYPO3\CMS\Core\Package\Package\getServiceProvider
‪getServiceProvider()
Definition: Package.php:152
‪TYPO3\CMS\Core\Package\Package\getPackageMetaData
‪getPackageMetaData()
Definition: Package.php:230
‪TYPO3\CMS\Core\Package\Package\getPackagePath
‪string getPackagePath()
Definition: Package.php:204
‪TYPO3\CMS\Core\Package\Exception\InvalidPackagePathException
Definition: InvalidPackagePathException.php:23
‪TYPO3\CMS\Core\Package\Package\makePathRelative
‪makePathRelative(Filesystem $filesystem, string $composerRootPath)
Definition: Package.php:219
‪TYPO3\CMS\Core\Package\Package\isPartOfMinimalUsableSystem
‪isPartOfMinimalUsableSystem()
Definition: Package.php:168
‪TYPO3\CMS\Core\Package\Package\$packageKey
‪string $packageKey
Definition: Package.php:54
‪TYPO3\CMS\Core\Package\Package\$packagePath
‪string $packagePath
Definition: Package.php:59
‪TYPO3\CMS\Core\Package\Exception\InvalidPackageKeyException
Definition: InvalidPackageKeyException.php:23
‪TYPO3\CMS\Core\Package\Package\$isRelativePackagePath
‪bool $isRelativePackagePath
Definition: Package.php:61
‪TYPO3\CMS\Core\Package\PackageInterface
Definition: PackageInterface.php:24
‪TYPO3\CMS\Core\Package\Package\getValueFromComposerManifest
‪getValueFromComposerManifest($key=null)
Definition: Package.php:252
‪TYPO3\CMS\Core\Package\Package\$packageMetaData
‪MetaData $packageMetaData
Definition: Package.php:73
‪TYPO3\CMS\Core\Package\Package
Definition: Package.php:30
‪TYPO3\CMS\Core\Package\Package\createPackageMetaData
‪createPackageMetaData(PackageManager $packageManager)
Definition: Package.php:121
‪TYPO3\CMS\Core\Package\MetaData\PackageConstraint
Definition: PackageConstraint.php:22
‪TYPO3\CMS\Core\Package\Package\$partOfFactoryDefault
‪bool $partOfFactoryDefault
Definition: Package.php:35
‪TYPO3\CMS\Core\Package\Package\getPackageReplacementKeys
‪getPackageReplacementKeys()
Definition: Package.php:239
‪TYPO3\CMS\Core\Package\Package\$partOfMinimalUsableSystem
‪bool $partOfMinimalUsableSystem
Definition: Package.php:41
‪TYPO3\CMS\Core\Package\Package\loadFlagsFromComposerManifest
‪loadFlagsFromComposerManifest()
Definition: Package.php:106
‪TYPO3\CMS\Core\Package\Package\$protected
‪bool $protected
Definition: Package.php:66
‪TYPO3\CMS\Core\Package\Package\isProtected
‪isProtected()
Definition: Package.php:184
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:41
‪TYPO3\CMS\Core\Package\Package\getPackageIcon
‪getPackageIcon()
Definition: Package.php:269
‪TYPO3\CMS\Core\Package\Package\isPartOfFactoryDefault
‪isPartOfFactoryDefault()
Definition: Package.php:160
‪TYPO3\CMS\Core\Package\Package\$serviceProvider
‪string $serviceProvider
Definition: Package.php:49
‪TYPO3\CMS\Core\Package
Definition: AbstractServiceProvider.php:18
‪TYPO3\CMS\Core\Package\Package\getPackageKey
‪getPackageKey()
Definition: Package.php:176
‪TYPO3\CMS\Core\Package\MetaData
Definition: MetaData.php:24
‪TYPO3\CMS\Core\Package\Package\$composerManifest
‪stdClass $composerManifest
Definition: Package.php:68
‪TYPO3\CMS\Core\Package\Package\setProtected
‪setProtected(bool $protected)
Definition: Package.php:194