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