-
-
Notifications
You must be signed in to change notification settings - Fork 84
/
EntrypointLookup.php
116 lines (91 loc) · 3.74 KB
/
EntrypointLookup.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
/*
* This file is part of the Symfony WebpackEncoreBundle package.
* (c) Fabien Potencier <fabien@symfony.com>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\WebpackEncoreBundle\Asset;
use Symfony\WebpackEncoreBundle\Exception\EntrypointNotFoundException;
/**
* Returns the CSS or JavaScript files needed for a Webpack entry.
*
* This reads a JSON file with the format of Webpack Encore's entrypoints.json file.
*
* @final
*/
class EntrypointLookup implements EntrypointLookupInterface, IntegrityDataProviderInterface
{
private $entrypointJsonPath;
private $entriesData;
private $returnedFiles = [];
public function __construct(string $entrypointJsonPath)
{
$this->entrypointJsonPath = $entrypointJsonPath;
}
public function getJavaScriptFiles(string $entryName): array
{
return $this->getEntryFiles($entryName, 'js');
}
public function getCssFiles(string $entryName): array
{
return $this->getEntryFiles($entryName, 'css');
}
public function getIntegrityData(): array
{
$entriesData = $this->getEntriesData();
if (!array_key_exists('integrity', $entriesData)) {
return [];
}
return $entriesData['integrity'];
}
/**
* Resets the state of this service.
*/
public function reset()
{
$this->returnedFiles = [];
}
private function getEntryFiles(string $entryName, string $key): array
{
$this->validateEntryName($entryName);
$entriesData = $this->getEntriesData();
$entryData = $entriesData['entrypoints'][$entryName];
if (!isset($entryData[$key])) {
// If we don't find the file type then just send back nothing.
return [];
}
// make sure to not return the same file multiple times
$entryFiles = $entryData[$key];
$newFiles = array_values(array_diff($entryFiles, $this->returnedFiles));
$this->returnedFiles = array_merge($this->returnedFiles, $newFiles);
return $newFiles;
}
private function validateEntryName(string $entryName)
{
$entriesData = $this->getEntriesData();
if (!isset($entriesData['entrypoints'][$entryName])) {
$withoutExtension = substr($entryName, 0, strrpos($entryName, '.'));
if (isset($entriesData['entrypoints'][$withoutExtension])) {
throw new EntrypointNotFoundException(sprintf('Could not find the entry "%s". Try "%s" instead (without the extension).', $entryName, $withoutExtension));
}
throw new EntrypointNotFoundException(sprintf('Could not find the entry "%s" in "%s". Found: %s.', $entryName, $this->entrypointJsonPath, implode(', ', array_keys($entriesData))));
}
}
private function getEntriesData(): array
{
if (null === $this->entriesData) {
if (!file_exists($this->entrypointJsonPath)) {
throw new \InvalidArgumentException(sprintf('Could not find the entrypoints file from Webpack: the file "%s" does not exist.', $this->entrypointJsonPath));
}
$this->entriesData = json_decode(file_get_contents($this->entrypointJsonPath), true);
if (null === $this->entriesData) {
throw new \InvalidArgumentException(sprintf('There was a problem JSON decoding the "%s" file', $this->entrypointJsonPath));
}
if (!isset($this->entriesData['entrypoints'])) {
throw new \InvalidArgumentException(sprintf('Could not find an "entrypoints" key in the "%s" file', $this->entrypointJsonPath));
}
}
return $this->entriesData;
}
}