‪TYPO3CMS  11.5
ComposerPackageArtifact.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;
19 
29 class ComposerPackageArtifact implements ‪PackageCacheInterface
30 {
34  private const ARTIFACTS_FILE = '/PackageArtifact.php';
35 
41  private string $packageArtifactsFile;
42 
48  private ‪PackageCacheEntry $cacheEntry;
49 
55  private ?Filesystem $filesystem;
56 
63  private ?string $cacheIdentifier;
64 
65  public function __construct(string $packageArtifactsPath, ?Filesystem $filesystem = null, string $cacheIdentifier = null)
66  {
67  $this->packageArtifactsFile = $packageArtifactsPath . self::ARTIFACTS_FILE;
68  $this->filesystem = $filesystem;
69  $this->cacheIdentifier = $cacheIdentifier;
70  }
71 
72  public function fetch(): ‪PackageCacheEntry
73  {
74  if ($this->isComposerBuildContext()) {
75  throw new \RuntimeException('Can not load package states during generation', 1629820498);
76  }
77  if (isset($this->cacheEntry)) {
78  return $this->cacheEntry;
79  }
80  $packageData = @include $this->packageArtifactsFile;
81  if (!$packageData) {
82  throw new \RuntimeException('Package artifact not found. Run "composer install" to create it.', 1629819799);
83  }
84 
85  return $this->cacheEntry = ‪PackageCacheEntry::fromCache($packageData);
86  }
87 
88  public function store(PackageCacheEntry $cacheEntry): void
89  {
90  if (!$this->isComposerBuildContext()) {
91  throw new \RuntimeException('Can not modify package states in Composer mode', 1629819858);
92  }
93  $this->filesystem->ensureDirectoryExists(dirname($this->packageArtifactsFile));
94 
95  file_put_contents($this->packageArtifactsFile, '<?php' . PHP_EOL . 'return ' . PHP_EOL . $cacheEntry->withIdentifier($this->cacheIdentifier)->serialize() . ';');
96  }
97 
98  public function invalidate(): void
99  {
100  throw new \RuntimeException('Can not modify package states in Composer mode', 1629824596);
101  }
102 
103  public function getIdentifier(): string
104  {
105  if (!isset($this->cacheEntry)) {
106  $this->fetch();
107  }
108 
109  return $this->cacheEntry->getIdentifier();
110  }
111 
112  private function isComposerBuildContext(): bool
113  {
114  return isset($this->filesystem, $this->cacheIdentifier);
115  }
116 }
‪TYPO3\CMS\Core\Package\Cache\PackageCacheEntry\fromCache
‪static fromCache(array $packageData)
Definition: PackageCacheEntry.php:107
‪TYPO3\CMS\Core\Package\Cache\PackageCacheEntry
Definition: PackageCacheEntry.php:33
‪TYPO3\CMS\Core\Package\Cache\PackageCacheInterface
Definition: PackageCacheInterface.php:31
‪TYPO3\CMS\Core\Package\Cache
Definition: ComposerPackageArtifact.php:16