• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

68publishers / webpack-encore-bundle / 13510931892

25 Feb 2025 12:37AM UTC coverage: 93.452% (+2.1%) from 91.379%
13510931892

push

github

tg666
PHP-Cs-Fixer should be run on PHP 8.3 because the package does not support PHP 8.4

314 of 336 relevant lines covered (93.45%)

0.93 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.43
/src/Asset/EntryPointLookup.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace SixtyEightPublishers\WebpackEncoreBundle\Asset;
6

7
use InvalidArgumentException;
8
use JsonException;
9
use Psr\Cache\CacheItemPoolInterface;
10
use Psr\Cache\InvalidArgumentException as PsrCacheInvalidArgumentException;
11
use SixtyEightPublishers\WebpackEncoreBundle\Exception\EntryPointNotFoundException;
12
use Throwable;
13
use function array_diff;
14
use function array_key_exists;
15
use function array_merge;
16
use function array_values;
17
use function file_exists;
18
use function file_get_contents;
19
use function json_decode;
20
use function sprintf;
21
use function strrpos;
22
use function substr;
23

24
/**
25
 * @phpstan-type EntriesData = array{
26
 *      entrypoints?: array<string, array{
27
 *          js?: list<string>,
28
 *          css?: list<string>,
29
 *      }>,
30
 *     integrity?: array<string, string>,
31
 *  }
32
 */
33
final class EntryPointLookup implements EntryPointLookupInterface, IntegrityDataProviderInterface
34
{
35
    private string $entrypointJsonPath;
36

37
    private ?CacheItemPoolInterface $cacheItemPool;
38

39
    private ?string $cacheKey;
40

41
    private bool $strictMode;
42

43
    /** @var EntriesData|null  */
44
    private ?array $entriesData = null;
45

46
    /** @var list<string> */
47
    private array $returnedFiles = [];
48

49
    public function __construct(string $entrypointJsonPath, ?CacheItemPoolInterface $cacheItemPool = null, ?string $cacheKey = null, bool $strictMode = true)
50
    {
51
        $this->entrypointJsonPath = $entrypointJsonPath;
1✔
52
        $this->cacheItemPool = $cacheItemPool;
1✔
53
        $this->cacheKey = $cacheKey;
1✔
54
        $this->strictMode = $strictMode;
1✔
55
    }
1✔
56

57
    /**
58
     * @throws Throwable|PsrCacheInvalidArgumentException
59
     */
60
    public function getJavaScriptFiles(string $entryName): array
61
    {
62
        return $this->getEntryFiles($entryName, 'js');
1✔
63
    }
64

65
    /**
66
     * @throws Throwable|PsrCacheInvalidArgumentException
67
     */
68
    public function getCssFiles(string $entryName): array
69
    {
70
        return $this->getEntryFiles($entryName, 'css');
1✔
71
    }
72

73
    /**
74
     * @throws Throwable|PsrCacheInvalidArgumentException
75
     */
76
    public function entryExists(string $entryName): bool
77
    {
78
        $entriesData = $this->getEntriesData();
1✔
79

80
        return isset($entriesData['entrypoints'][$entryName]);
1✔
81
    }
82

83
    public function reset(): void
84
    {
85
        $this->returnedFiles = [];
1✔
86
    }
1✔
87

88
    /**
89
     * @throws Throwable|PsrCacheInvalidArgumentException
90
     */
91
    public function getIntegrityData(): array
92
    {
93
        $entriesData = $this->getEntriesData();
1✔
94

95
        if (!array_key_exists('integrity', $entriesData)) {
1✔
96
            return [];
1✔
97
        }
98

99
        return $entriesData['integrity'];
1✔
100
    }
101

102
    /**
103
     * @return list<string>
104
     * @throws Throwable|PsrCacheInvalidArgumentException
105
     */
106
    private function getEntryFiles(string $entryName, string $key): array
107
    {
108
        $this->validateEntryName($entryName);
1✔
109

110
        $entryData = $this->getEntriesData()['entrypoints'][$entryName] ?? [];
1✔
111

112
        if (!isset($entryData[$key])) {
1✔
113
            return [];
×
114
        }
115

116
        $entryFiles = $entryData[$key];
1✔
117
        $newFiles = array_values(array_diff($entryFiles, $this->returnedFiles));
1✔
118
        $this->returnedFiles = array_merge($this->returnedFiles, $newFiles);
1✔
119

120
        return $newFiles;
1✔
121
    }
122

123
    /**
124
     * @throws Throwable|PsrCacheInvalidArgumentException
125
     */
126
    private function validateEntryName(string $entryName): void
127
    {
128
        $entriesData = $this->getEntriesData();
1✔
129

130
        if (isset($entriesData['entrypoints'][$entryName]) || !$this->strictMode) {
1✔
131
            return;
1✔
132
        }
133

134
        $dotPosition = strrpos($entryName, '.');
1✔
135
        $withoutExtension = false !== $dotPosition ? substr($entryName, 0, $dotPosition) : null;
1✔
136

137
        throw null !== $withoutExtension && isset($entriesData['entrypoints'][$withoutExtension])
1✔
138
            ? EntryPointNotFoundException::missingEntryWithSuggestion($entryName, $withoutExtension)
1✔
139
            : EntryPointNotFoundException::missingEntry($entryName, $this->entrypointJsonPath, array_keys($entriesData['entrypoints'] ?? []));
1✔
140
    }
141

142
    /**
143
     * @return EntriesData
144
     * @throws Throwable|PsrCacheInvalidArgumentException
145
     */
146
    private function getEntriesData(): array
147
    {
148
        if (null !== $this->entriesData) {
1✔
149
            return $this->entriesData;
1✔
150
        }
151

152
        if ($this->cacheItemPool && $this->cacheKey) {
1✔
153
            $cached = $this->cacheItemPool->getItem($this->cacheKey);
1✔
154

155
            if ($cached->isHit()) {
1✔
156
                return $this->entriesData = $cached->get();
1✔
157
            }
158
        }
159

160
        if (!file_exists($this->entrypointJsonPath)) {
1✔
161
            if (!$this->strictMode) {
1✔
162
                return [];
×
163
            }
164

165
            throw new InvalidArgumentException(sprintf(
1✔
166
                'Could not find the entrypoints file from Webpack: the file "%s" does not exist.',
1✔
167
                $this->entrypointJsonPath,
1✔
168
            ));
169
        }
170

171
        try {
172
            $this->entriesData = json_decode((string) file_get_contents($this->entrypointJsonPath), true, 512, JSON_THROW_ON_ERROR);
1✔
173
        } catch (JsonException $e) {
1✔
174
        }
175

176
        if (isset($e) || null === $this->entriesData) {
1✔
177
            throw new InvalidArgumentException(sprintf(
1✔
178
                'There was a problem JSON decoding the "%s" file.',
1✔
179
                $this->entrypointJsonPath,
1✔
180
            ));
181
        }
182

183
        if (!isset($this->entriesData['entrypoints'])) {
1✔
184
            throw new InvalidArgumentException(sprintf(
1✔
185
                'Could not find an "entrypoints" key in the "%s" file.',
1✔
186
                $this->entrypointJsonPath,
1✔
187
            ));
188
        }
189

190
        if (isset($cached) && $this->cacheItemPool) {
1✔
191
            $this->cacheItemPool->save($cached->set($this->entriesData));
1✔
192
        }
193

194
        return $this->entriesData;
1✔
195
    }
196
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc