vendor/symfony/webpack-encore-bundle/src/Asset/EntrypointLookup.php line 36

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