‪TYPO3CMS  ‪main
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 (!str_ends_with(‪$packagePath, '/')) {
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 
136  protected function createPackageMetaData(PackageManager $packageManager)
137  {
138  $this->packageMetaData = new MetaData($this->getPackageKey());
139  $description = (string)$this->getValueFromComposerManifest('description');
140  $this->packageMetaData->setDescription($description);
141  $this->packageMetaData->setTitle($this->getValueFromComposerManifest('title') ?? $description);
142  $this->packageMetaData->setVersion((string)$this->getValueFromComposerManifest('version'));
143  $this->packageMetaData->setPackageType((string)$this->getValueFromComposerManifest('type'));
144  $requirements = $this->getValueFromComposerManifest('require');
145  if ($requirements !== null) {
146  foreach ($requirements as $requirement => $version) {
147  $packageKey = $packageManager->getPackageKeyFromComposerName($requirement);
148  $constraint = new PackageConstraint(MetaData::CONSTRAINT_TYPE_DEPENDS, $packageKey);
149  $this->packageMetaData->addConstraint($constraint);
150  }
151  }
152  $suggestions = $this->getValueFromComposerManifest('suggest');
153  if ($suggestions !== null) {
154  foreach ($suggestions as $suggestion => $version) {
155  $packageKey = $packageManager->getPackageKeyFromComposerName($suggestion);
156  $constraint = new PackageConstraint(MetaData::CONSTRAINT_TYPE_SUGGESTS, $packageKey);
157  $this->packageMetaData->addConstraint($constraint);
158  }
159  }
160  }
161 
167  public function getServiceProvider(): string
168  {
169  return $this->serviceProvider ?? PseudoServiceProvider::class;
170  }
171 
176  public function isPartOfFactoryDefault()
177  {
178  return $this->partOfFactoryDefault;
179  }
180 
185  public function isPartOfMinimalUsableSystem()
186  {
187  return $this->partOfMinimalUsableSystem;
188  }
189 
195  public function getPackageKey()
196  {
197  return $this->packageKey;
198  }
199 
205  public function isProtected()
206  {
207  return $this->protected;
208  }
209 
215  public function setProtected($protected)
216  {
217  $this->protected = (bool)$protected;
218  }
219 
225  public function getPackagePath()
226  {
227  if (!$this->isRelativePackagePath) {
228  return $this->packagePath;
229  }
230  $this->isRelativePackagePath = false;
231 
232  return $this->packagePath = Environment::getComposerRootPath() . '/' . $this->packagePath;
233  }
234 
240  public function makePathRelative(Filesystem $filesystem, string $composerRootPath): void
241  {
242  $this->isRelativePackagePath = true;
243  $this->packagePath = ($composerRootPath . '/') === $this->packagePath ? '' : $filesystem->findShortestPath($composerRootPath, $this->packagePath, true) . '/';
244  }
245 
252  public function getPackageMetaData()
253  {
254  return $this->packageMetaData;
255  }
256 
263  public function getPackageReplacementKeys()
264  {
265  // The cast to array is required since the manifest returns data with type mixed
266  return (array)$this->getValueFromComposerManifest('replace') ?: [];
267  }
268 
277  public function getValueFromComposerManifest($key = null)
278  {
279  if ($key === null) {
280  return $this->composerManifest;
281  }
282 
283  if (isset($this->composerManifest->{$key})) {
284  $value = $this->composerManifest->{$key};
285  } else {
286  $value = null;
287  }
288  return $value;
289  }
290 }
‪TYPO3\CMS\Core\Package\Package\getPackageMetaData
‪MetaData getPackageMetaData()
Definition: Package.php:243
‪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\getServiceProvider
‪getServiceProvider()
Definition: Package.php:158
‪TYPO3\CMS\Core\Package\Package\$serviceProvider
‪string null $serviceProvider
Definition: Package.php:49
‪TYPO3\CMS\Core\Package\Package\isProtected
‪bool isProtected()
Definition: Package.php:196
‪TYPO3\CMS\Core\Package\Package\getPackagePath
‪string getPackagePath()
Definition: Package.php:216
‪TYPO3\CMS\Core\Package\Package\setProtected
‪setProtected($protected)
Definition: Package.php:206
‪TYPO3\CMS\Core\Package\Exception\InvalidPackagePathException
Definition: InvalidPackagePathException.php:23
‪TYPO3\CMS\Core\Package\Package\makePathRelative
‪makePathRelative(Filesystem $filesystem, string $composerRootPath)
Definition: Package.php:231
‪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:186
‪TYPO3\CMS\Core\Package\PackageInterface
Definition: PackageInterface.php:22
‪TYPO3\CMS\Core\Package\Package\getValueFromComposerManifest
‪mixed null getValueFromComposerManifest($key=null)
Definition: Package.php:268
‪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:176
‪TYPO3\CMS\Core\Package\Package\createPackageMetaData
‪createPackageMetaData(PackageManager $packageManager)
Definition: Package.php:127
‪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:167
‪TYPO3\CMS\Core\Package\Package\getPackageReplacementKeys
‪array getPackageReplacementKeys()
Definition: Package.php:254
‪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:41
‪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