‪TYPO3CMS  11.5
Package.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
18 use Composer\Util\Filesystem;
23 
28 {
35  protected ‪$partOfFactoryDefault = false;
36 
43  protected ‪$partOfMinimalUsableSystem = false;
44 
52  protected ‪$serviceProvider;
53 
58  protected ‪$packageKey;
59 
64  protected ‪$packagePath;
65 
69  protected ‪$isRelativePackagePath = false;
70 
75  protected ‪$protected = false;
76 
80  protected ‪$composerManifest;
81 
86  protected ‪$packageMetaData;
87 
99  public function ‪__construct(PackageManager $packageManager, string ‪$packageKey, string ‪$packagePath, bool $ignoreExtEmConf = false)
100  {
101  if (!$packageManager->isPackageKeyValid(‪$packageKey)) {
102  throw new ‪InvalidPackageKeyException('"' . ‪$packageKey . '" is not a valid package key.', 1217959511);
103  }
104  if (!(@is_dir(‪$packagePath) || (is_link(‪$packagePath) && is_dir(‪$packagePath)))) {
105  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);
106  }
107  if (substr(‪$packagePath, -1, 1) !== '/') {
108  throw new ‪InvalidPackagePathException(sprintf('The package path "%s" provided for package "%s" has no trailing forward slash.', ‪$packagePath, ‪$packageKey), 1166633722);
109  }
110  $this->packageKey = ‪$packageKey;
111  $this->packagePath = ‪$packagePath;
112  $this->composerManifest = $packageManager->getComposerManifest($this->packagePath, $ignoreExtEmConf);
114  $this->‪createPackageMetaData($packageManager);
115  }
116 
121  protected function ‪loadFlagsFromComposerManifest()
122  {
123  $extraFlags = $this->‪getValueFromComposerManifest('extra');
124  if ($extraFlags !== null && isset($extraFlags->{'typo3/cms'}->{'Package'})) {
125  foreach ($extraFlags->{'typo3/cms'}->{'Package'} as $flagName => $flagValue) {
126  if (property_exists($this, $flagName)) {
127  $this->{$flagName} = $flagValue;
128  }
129  }
130  }
131  }
132 
138  protected function createPackageMetaData(PackageManager $packageManager)
139  {
140  $this->packageMetaData = new MetaData($this->getPackageKey());
141  $description = (string)$this->getValueFromComposerManifest('description');
142  $this->packageMetaData->setDescription($description);
143  $this->packageMetaData->setTitle($this->getValueFromComposerManifest('title') ?? $description);
144  $this->packageMetaData->setVersion((string)$this->getValueFromComposerManifest('version'));
145  $this->packageMetaData->setPackageType((string)$this->getValueFromComposerManifest('type'));
146  $requirements = $this->getValueFromComposerManifest('require');
147  if ($requirements !== null) {
148  foreach ($requirements as $requirement => $version) {
149  $packageKey = $packageManager->getPackageKeyFromComposerName($requirement);
150  $constraint = new PackageConstraint(MetaData::CONSTRAINT_TYPE_DEPENDS, $packageKey);
151  $this->packageMetaData->addConstraint($constraint);
152  }
153  }
154  $suggestions = $this->getValueFromComposerManifest('suggest');
155  if ($suggestions !== null) {
156  foreach ($suggestions as $suggestion => $version) {
157  $packageKey = $packageManager->getPackageKeyFromComposerName($suggestion);
158  $constraint = new PackageConstraint(MetaData::CONSTRAINT_TYPE_SUGGESTS, $packageKey);
159  $this->packageMetaData->addConstraint($constraint);
160  }
161  }
162  }
163 
170  public function getServiceProvider(): string
171  {
172  return $this->serviceProvider ?? PseudoServiceProvider::class;
173  }
174 
179  public function isPartOfFactoryDefault()
180  {
181  return $this->partOfFactoryDefault;
182  }
183 
188  public function isPartOfMinimalUsableSystem()
189  {
190  return $this->partOfMinimalUsableSystem;
191  }
192 
198  public function getPackageKey()
199  {
200  return $this->packageKey;
201  }
202 
208  public function isProtected()
209  {
210  return $this->protected;
211  }
212 
218  public function setProtected($protected)
219  {
220  $this->protected = (bool)$protected;
221  }
222 
228  public function getPackagePath()
229  {
230  if (!$this->isRelativePackagePath) {
231  return $this->packagePath;
232  }
233  $this->isRelativePackagePath = false;
234 
235  return $this->packagePath = Environment::getComposerRootPath() . '/' . $this->packagePath;
236  }
237 
245  public function makePathRelative(Filesystem $filesystem, string $composerRootPath): void
246  {
247  $this->isRelativePackagePath = true;
248  $this->packagePath = ($composerRootPath . '/') === $this->packagePath ? '' : $filesystem->findShortestPath($composerRootPath, $this->packagePath, true) . '/';
249  }
250 
257  public function getPackageMetaData()
258  {
259  return $this->packageMetaData;
260  }
261 
268  public function getPackageReplacementKeys()
269  {
270  // The cast to array is required since the manifest returns data with type mixed
271  return (array)$this->getValueFromComposerManifest('replace') ?: [];
272  }
273 
282  public function getValueFromComposerManifest($key = null)
283  {
284  if ($key === null) {
285  return $this->composerManifest;
286  }
287 
288  if (isset($this->composerManifest->{$key})) {
289  $value = $this->composerManifest->{$key};
290  } else {
291  $value = null;
292  }
293  return $value;
294  }
295 }
‪TYPO3\CMS\Core\Package\Package\getPackageMetaData
‪MetaData getPackageMetaData()
Definition: Package.php:248
‪TYPO3\CMS\Core\Package\Package\__construct
‪__construct(PackageManager $packageManager, string $packageKey, string $packagePath, bool $ignoreExtEmConf=false)
Definition: Package.php:90
‪TYPO3\CMS\Core\Package\Package\$serviceProvider
‪string null $serviceProvider
Definition: Package.php:49
‪TYPO3\CMS\Core\Package\Package\isProtected
‪bool isProtected()
Definition: Package.php:199
‪TYPO3\CMS\Core\Package\Package\getPackagePath
‪string getPackagePath()
Definition: Package.php:219
‪TYPO3\CMS\Core\Package\Package\setProtected
‪setProtected($protected)
Definition: Package.php:209
‪TYPO3\CMS\Core\Package\Exception\InvalidPackagePathException
Definition: InvalidPackagePathException.php:23
‪TYPO3\CMS\Core\Package\Package\makePathRelative
‪makePathRelative(Filesystem $filesystem, string $composerRootPath)
Definition: Package.php:236
‪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:63
‪TYPO3\CMS\Core\Package\Package\getPackageKey
‪string getPackageKey()
Definition: Package.php:189
‪TYPO3\CMS\Core\Package\PackageInterface
Definition: PackageInterface.php:22
‪TYPO3\CMS\Core\Package\Package\getValueFromComposerManifest
‪mixed null getValueFromComposerManifest($key=null)
Definition: Package.php:273
‪TYPO3\CMS\Core\Package\Package\$packageMetaData
‪MetaData $packageMetaData
Definition: Package.php:77
‪TYPO3\CMS\Core\Package\Package
Definition: Package.php:28
‪TYPO3\CMS\Core\Package\Package\isPartOfMinimalUsableSystem
‪bool isPartOfMinimalUsableSystem()
Definition: Package.php:179
‪TYPO3\CMS\Core\Package\Package\createPackageMetaData
‪createPackageMetaData(PackageManager $packageManager)
Definition: Package.php:129
‪TYPO3\CMS\Core\Package\MetaData\PackageConstraint
Definition: PackageConstraint.php:22
‪TYPO3\CMS\Core\Package\Package\$partOfFactoryDefault
‪bool $partOfFactoryDefault
Definition: Package.php:34
‪TYPO3\CMS\Core\Package\Package\isPartOfFactoryDefault
‪bool isPartOfFactoryDefault()
Definition: Package.php:170
‪TYPO3\CMS\Core\Package\Package\getServiceProvider
‪string getServiceProvider()
Definition: Package.php:161
‪TYPO3\CMS\Core\Package\Package\getPackageReplacementKeys
‪array getPackageReplacementKeys()
Definition: Package.php:259
‪TYPO3\CMS\Core\Package\Package\$partOfMinimalUsableSystem
‪bool $partOfMinimalUsableSystem
Definition: Package.php:41
‪TYPO3\CMS\Core\Package\Package\loadFlagsFromComposerManifest
‪loadFlagsFromComposerManifest()
Definition: Package.php:112
‪TYPO3\CMS\Core\Package\Package\$protected
‪bool $protected
Definition: Package.php:68
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:43
‪TYPO3\CMS\Core\Package
Definition: AbstractServiceProvider.php:18
‪TYPO3\CMS\Core\Package\MetaData
Definition: MetaData.php:24
‪TYPO3\CMS\Core\Package\Package\$composerManifest
‪stdClass $composerManifest
Definition: Package.php:72