AssetManagerPass.php 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. /*
  3. * This file is part of the Symfony framework.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace Symfony\Bundle\AsseticBundle\DependencyInjection\Compiler;
  11. use Symfony\Component\DependencyInjection\Reference;
  12. use Symfony\Component\DependencyInjection\ContainerBuilder;
  13. use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  14. /**
  15. * Adds services tagged as assets to the asset manager.
  16. *
  17. * @author Kris Wallsmith <kris@symfony.com>
  18. */
  19. class AssetManagerPass implements CompilerPassInterface
  20. {
  21. public function process(ContainerBuilder $container)
  22. {
  23. if (!$container->hasDefinition('assetic.asset_manager')) {
  24. return;
  25. }
  26. $am = $container->getDefinition('assetic.asset_manager');
  27. // add assets
  28. foreach ($container->findTaggedServiceIds('assetic.asset') as $id => $attributes) {
  29. foreach ($attributes as $attr) {
  30. if (isset($attr['alias'])) {
  31. $am->addMethodCall('set', array($attr['alias'], new Reference($id)));
  32. }
  33. }
  34. }
  35. // add loaders
  36. $loaders = array();
  37. foreach ($container->findTaggedServiceIds('assetic.formula_loader') as $id => $attributes) {
  38. foreach ($attributes as $attr) {
  39. if (isset($attr['alias'])) {
  40. $loaders[$attr['alias']] = new Reference($id);
  41. }
  42. }
  43. }
  44. $am->replaceArgument(1, $loaders);
  45. // add resources
  46. foreach ($container->findTaggedServiceIds('assetic.formula_resource') as $id => $attributes) {
  47. foreach ($attributes as $attr) {
  48. if (isset($attr['loader'])) {
  49. $am->addMethodCall('addResource', array(new Reference($id), $attr['loader']));
  50. }
  51. }
  52. }
  53. }
  54. }