|
31 | 31 | use Symfony\Bundle\FullStack;
|
32 | 32 | use Symfony\Bundle\MercureBundle\MercureBundle;
|
33 | 33 | use Symfony\Component\Asset\PackageInterface;
|
| 34 | +use Symfony\Component\AssetMapper\AssetMapper; |
| 35 | +use Symfony\Component\AssetMapper\ImportMap\ImportMapManager; |
34 | 36 | use Symfony\Component\BrowserKit\AbstractBrowser;
|
35 | 37 | use Symfony\Component\Cache\Adapter\AdapterInterface;
|
36 | 38 | use Symfony\Component\Cache\Adapter\ArrayAdapter;
|
|
45 | 47 | use Symfony\Component\Config\FileLocator;
|
46 | 48 | use Symfony\Component\Config\Loader\LoaderInterface;
|
47 | 49 | use Symfony\Component\Config\Resource\DirectoryResource;
|
| 50 | +use Symfony\Component\Config\Resource\FileResource; |
48 | 51 | use Symfony\Component\Config\ResourceCheckerInterface;
|
49 | 52 | use Symfony\Component\Console\Application;
|
50 | 53 | use Symfony\Component\Console\Command\Command;
|
@@ -330,6 +333,14 @@ public function load(array $configs, ContainerBuilder $container)
|
330 | 333 | $this->registerAssetsConfiguration($config['assets'], $container, $loader);
|
331 | 334 | }
|
332 | 335 |
|
| 336 | + if ($this->readConfigEnabled('asset_mapper', $container, $config['asset_mapper'])) { |
| 337 | + if (!class_exists(AssetMapper::class)) { |
| 338 | + throw new LogicException('AssetMapper support cannot be enabled as the AssetMapper component is not installed. Try running "composer require symfony/asset-mapper".'); |
| 339 | + } |
| 340 | + |
| 341 | + $this->registerAssetMapperConfiguration($config['asset_mapper'], $container, $loader, $this->readConfigEnabled('assets', $container, $config['assets'])); |
| 342 | + } |
| 343 | + |
333 | 344 | if ($this->readConfigEnabled('http_client', $container, $config['http_client'])) {
|
334 | 345 | $this->registerHttpClientConfiguration($config['http_client'], $container, $loader);
|
335 | 346 | }
|
@@ -1231,6 +1242,57 @@ private function registerAssetsConfiguration(array $config, ContainerBuilder $co
|
1231 | 1242 | }
|
1232 | 1243 | }
|
1233 | 1244 |
|
| 1245 | + private function registerAssetMapperConfiguration(array $config, ContainerBuilder $container, PhpFileLoader $loader, bool $assetEnabled): void |
| 1246 | + { |
| 1247 | + $loader->load('asset_mapper.php'); |
| 1248 | + |
| 1249 | + if (!$assetEnabled) { |
| 1250 | + $container->removeDefinition('asset_mapper.asset_package'); |
| 1251 | + } |
| 1252 | + |
| 1253 | + $publicDirName = $this->getPublicDirectoryName($container); |
| 1254 | + $container->getDefinition('asset_mapper') |
| 1255 | + ->setArgument(3, $config['public_prefix']) |
| 1256 | + ->setArgument(4, $publicDirName) |
| 1257 | + ->setArgument(5, $config['extensions']) |
| 1258 | + ; |
| 1259 | + |
| 1260 | + $paths = $config['paths']; |
| 1261 | + foreach ($container->getParameter('kernel.bundles_metadata') as $name => $bundle) { |
| 1262 | + if ($container->fileExists($dir = $bundle['path'].'/Resources/public') || $container->fileExists($dir = $bundle['path'].'/public')) { |
| 1263 | + $paths[$dir] = sprintf('bundles/%s', preg_replace('/bundle$/', '', strtolower($name))); |
| 1264 | + } |
| 1265 | + } |
| 1266 | + $container->getDefinition('asset_mapper.repository') |
| 1267 | + ->setArgument(0, $paths); |
| 1268 | + |
| 1269 | + $container->getDefinition('asset_mapper.command.compile') |
| 1270 | + ->setArgument(4, $publicDirName); |
| 1271 | + |
| 1272 | + if (!$config['server']) { |
| 1273 | + $container->removeDefinition('asset_mapper.dev_server_subscriber'); |
| 1274 | + } |
| 1275 | + |
| 1276 | + $container->getDefinition('asset_mapper.compiler.css_asset_url_compiler') |
| 1277 | + ->setArgument(0, $config['strict_mode']); |
| 1278 | + |
| 1279 | + $container->getDefinition('asset_mapper.compiler.javascript_import_path_compiler') |
| 1280 | + ->setArgument(0, $config['strict_mode']); |
| 1281 | + |
| 1282 | + $container |
| 1283 | + ->getDefinition('asset_mapper.importmap.manager') |
| 1284 | + ->replaceArgument(1, $config['importmap_path']) |
| 1285 | + ->replaceArgument(2, $config['vendor_dir']) |
| 1286 | + ->replaceArgument(3, $config['provider']) |
| 1287 | + ; |
| 1288 | + |
| 1289 | + $container |
| 1290 | + ->getDefinition('asset_mapper.importmap.renderer') |
| 1291 | + ->replaceArgument(2, $config['importmap_polyfill'] ?? ImportMapManager::POLYFILL_URL) |
| 1292 | + ->replaceArgument(3, $config['importmap_script_attributes']) |
| 1293 | + ; |
| 1294 | + } |
| 1295 | + |
1234 | 1296 | /**
|
1235 | 1297 | * Returns a definition for an asset package.
|
1236 | 1298 | */
|
@@ -2994,4 +3056,20 @@ private function writeConfigEnabled(string $path, bool $value, array &$config):
|
2994 | 3056 | $this->configsEnabled[$path] = $value;
|
2995 | 3057 | $config['enabled'] = $value;
|
2996 | 3058 | }
|
| 3059 | + |
| 3060 | + private function getPublicDirectoryName(ContainerBuilder $container): string |
| 3061 | + { |
| 3062 | + $defaultPublicDir = 'public'; |
| 3063 | + |
| 3064 | + $composerFilePath = $container->getParameter('kernel.project_dir').'/composer.json'; |
| 3065 | + |
| 3066 | + if (!file_exists($composerFilePath)) { |
| 3067 | + return $defaultPublicDir; |
| 3068 | + } |
| 3069 | + |
| 3070 | + $container->addResource(new FileResource($composerFilePath)); |
| 3071 | + $composerConfig = json_decode(file_get_contents($composerFilePath), true); |
| 3072 | + |
| 3073 | + return $composerConfig['extra']['public-dir'] ?? $defaultPublicDir; |
| 3074 | + } |
2997 | 3075 | }
|
0 commit comments