Skip to content

Commit

Permalink
fixup! Implemented ConfigurationDumper for Ibexa Encore files
Browse files Browse the repository at this point in the history
  • Loading branch information
alongosz committed Jun 20, 2022
1 parent 9443086 commit fe690df
Showing 1 changed file with 34 additions and 24 deletions.
58 changes: 34 additions & 24 deletions src/contracts/Container/Encore/ConfigurationDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
final class ConfigurationDumper
{
public const ENCORE_DIR = 'encore';
public const ENCORE_TARGET_PATH = 'var/encore';

private ContainerInterface $containerBuilder;

Expand All @@ -39,11 +40,13 @@ public function dumpCustomConfiguration(
): void {
$bundlesMetadata = $this->containerBuilder->getParameter('kernel.bundles_metadata');
$rootPath = $this->containerBuilder->getParameter('kernel.project_dir') . '/';
$targetPath = 'var/encore';

foreach ($webpackConfigNames as $configName => $configFiles) {
$paths = $this->locateConfigurationFiles($bundlesMetadata, $configFiles, $rootPath);
$this->dumpConfigurationPaths($configName, $rootPath . $targetPath, $paths);
$this->dumpConfigurationPaths(
$configName,
$rootPath . self::ENCORE_TARGET_PATH,
$paths
);
}
}

Expand All @@ -54,21 +57,7 @@ private function locateConfigurationFiles(
): array {
$paths = [];
foreach ($configFiles as $configFile => $options) {
$finder = new Finder();
$finder
->in(array_column($bundlesMetadata, 'path'))
->path('Resources/' . self::ENCORE_DIR)
->name($configFile)
// include top-level project resources
->append(
(new Finder())
->in($rootPath)
->path(self::ENCORE_DIR)
->name($configFile)
->depth(1)
->files()
)
->files();
$finder = $this->createFinder($bundlesMetadata, $configFile, $rootPath);

/** @var \Symfony\Component\Finder\SplFileInfo $fileInfo */
foreach ($finder as $fileInfo) {
Expand All @@ -82,11 +71,12 @@ private function locateConfigurationFiles(
);
}

$paths[] = preg_replace(
'/^' . preg_quote($rootPath, '/') . '/',
'./',
$fileInfo->getRealPath()
);
$path = $fileInfo->getRealPath();
if (strpos($path, $rootPath) === 0) {
$path = './' . substr($path, strlen($rootPath));
}

$paths[] = $path;
}
}

Expand All @@ -102,10 +92,30 @@ private function dumpConfigurationPaths(
array $paths
): void {
$filesystem = new Filesystem();
$filesystem->mkdir($targetPath);
$filesystem->dumpFile(
$targetPath . '/' . $configName,
sprintf('module.exports = %s;', json_encode($paths, JSON_THROW_ON_ERROR))
);
}

private function createFinder(array $bundlesMetadata, $configFile, string $rootPath): Finder
{
$finder = new Finder();
$finder
->in(array_column($bundlesMetadata, 'path'))
->path('Resources/' . self::ENCORE_DIR)
->name($configFile)
// include top-level project resources
->append(
(new Finder())
->in($rootPath)
->path(self::ENCORE_DIR)
->name($configFile)
->depth(1)
->files()
)
->files();

return $finder;
}
}

0 comments on commit fe690df

Please sign in to comment.