DirectoryResourceDefinition.php 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bundle\AsseticBundle\DependencyInjection;
  11. use Symfony\Component\DependencyInjection\Definition;
  12. use Symfony\Component\DependencyInjection\Reference;
  13. /**
  14. * Encapsulates logic for creating a directory resource.
  15. *
  16. * @author Kris Wallsmith <kris@symfony.com>
  17. */
  18. class DirectoryResourceDefinition extends Definition
  19. {
  20. /**
  21. * Constructor.
  22. *
  23. * @param string $bundle A bundle name or empty string
  24. * @param string $engine The templating engine
  25. * @param array $dirs An array of directories to merge
  26. */
  27. public function __construct($bundle, $engine, array $dirs)
  28. {
  29. if (!count($dirs)) {
  30. throw new \InvalidArgumentException('You must provide at least one directory.');
  31. }
  32. parent::__construct();
  33. $this
  34. ->addTag('assetic.templating.'.$engine)
  35. ->addTag('assetic.formula_resource', array('loader' => $engine));
  36. ;
  37. if (1 == count($dirs)) {
  38. // no need to coalesce
  39. self::configureDefinition($this, $bundle, $engine, reset($dirs));
  40. return;
  41. }
  42. // gather the wrapped resource definitions
  43. $resources = array();
  44. foreach ($dirs as $dir) {
  45. $resources[] = $resource = new Definition();
  46. self::configureDefinition($resource, $bundle, $engine, $dir);
  47. }
  48. $this
  49. ->setClass('%assetic.coalescing_directory_resource.class%')
  50. ->addArgument($resources)
  51. ->setPublic(false)
  52. ;
  53. }
  54. static private function configureDefinition(Definition $definition, $bundle, $engine, $dir)
  55. {
  56. $definition
  57. ->setClass('%assetic.directory_resource.class%')
  58. ->addArgument(new Reference('templating.loader'))
  59. ->addArgument($bundle)
  60. ->addArgument($dir)
  61. ->addArgument('/^[^.]+\.[^.]+\.'.$engine.'$/')
  62. ->setPublic(false)
  63. ;
  64. }
  65. }