vendor/ibexa/core/src/contracts/Container/Encore/ConfigurationDumper.php line 65

Open in your IDE?
  1. <?php
  2. /**
  3. * @copyright Copyright (C) Ibexa AS. All rights reserved.
  4. * @license For full copyright and license information view LICENSE file distributed with this source code.
  5. */
  6. declare(strict_types=1);
  7. namespace Ibexa\Contracts\Core\Container\Encore;
  8. use Symfony\Component\DependencyInjection\ContainerInterface;
  9. use Symfony\Component\Filesystem\Filesystem;
  10. use Symfony\Component\Finder\Finder;
  11. /**
  12. * Scans project and bundles resources for the given configuration paths.
  13. * To be used only during container building, in Bundle Extension class.
  14. *
  15. * @internal for internal use by Ibexa 1st party packages to provide specific extension points
  16. */
  17. final class ConfigurationDumper
  18. {
  19. public const ENCORE_DIR = 'encore';
  20. public const ENCORE_TARGET_PATH = 'var/encore';
  21. private ContainerInterface $containerBuilder;
  22. public function __construct(ContainerInterface $containerBuilder)
  23. {
  24. $this->containerBuilder = $containerBuilder;
  25. }
  26. /**
  27. * @param array<string, array<string, array{'deprecated'?: bool, 'alternative'?: string}>> $webpackConfigNames
  28. *
  29. * @throws \JsonException
  30. */
  31. public function dumpCustomConfiguration(
  32. array $webpackConfigNames
  33. ): void {
  34. $bundlesMetadata = $this->containerBuilder->getParameter('kernel.bundles_metadata');
  35. $rootPath = $this->containerBuilder->getParameter('kernel.project_dir') . '/';
  36. foreach ($webpackConfigNames as $configName => $configFiles) {
  37. $paths = $this->locateConfigurationFiles($bundlesMetadata, $configFiles, $rootPath);
  38. $this->dumpConfigurationPaths(
  39. $configName,
  40. $rootPath . self::ENCORE_TARGET_PATH,
  41. $paths
  42. );
  43. }
  44. }
  45. private function locateConfigurationFiles(
  46. array $bundlesMetadata,
  47. array $configFiles,
  48. string $rootPath
  49. ): array {
  50. $paths = [];
  51. foreach ($configFiles as $configFile => $options) {
  52. $finder = $this->createFinder($bundlesMetadata, $configFile, $rootPath);
  53. /** @var \Symfony\Component\Finder\SplFileInfo $fileInfo */
  54. foreach ($finder as $fileInfo) {
  55. if ($options['deprecated'] ?? false) {
  56. trigger_deprecation(
  57. 'ibexa/core',
  58. '4.0.0',
  59. 'Support for old configuration files is deprecated, please update name of %s file, to %s',
  60. $fileInfo->getPathname(),
  61. $options['alternative']
  62. );
  63. }
  64. $path = $fileInfo->getRealPath();
  65. if (strpos($path, $rootPath) === 0) {
  66. $path = './' . substr($path, strlen($rootPath));
  67. }
  68. $paths[] = $path;
  69. }
  70. }
  71. return $paths;
  72. }
  73. /**
  74. * @throws \JsonException
  75. */
  76. private function dumpConfigurationPaths(
  77. string $configName,
  78. string $targetPath,
  79. array $paths
  80. ): void {
  81. $filesystem = new Filesystem();
  82. $filesystem->dumpFile(
  83. $targetPath . '/' . $configName,
  84. sprintf('module.exports = %s;', json_encode($paths, JSON_THROW_ON_ERROR))
  85. );
  86. }
  87. private function createFinder(
  88. array $bundlesMetadata,
  89. string $configFile,
  90. string $rootPath
  91. ): Finder {
  92. $finder = new Finder();
  93. $finder
  94. ->in(array_column($bundlesMetadata, 'path'))
  95. ->path('Resources/' . self::ENCORE_DIR)
  96. ->name($configFile)
  97. // include top-level project resources
  98. ->append(
  99. (new Finder())
  100. ->in($rootPath)
  101. ->path(self::ENCORE_DIR)
  102. ->name($configFile)
  103. ->depth(1)
  104. ->files()
  105. )
  106. ->files();
  107. return $finder;
  108. }
  109. }