GlobAsset.php 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /*
  3. * This file is part of the Assetic package, an OpenSky project.
  4. *
  5. * (c) 2010-2012 OpenSky Project Inc
  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 Assetic\Asset;
  11. use Assetic\Filter\FilterInterface;
  12. /**
  13. * A collection of assets loaded by glob.
  14. *
  15. * @author Kris Wallsmith <kris.wallsmith@gmail.com>
  16. */
  17. class GlobAsset extends AssetCollection
  18. {
  19. private $globs;
  20. private $initialized;
  21. /**
  22. * Constructor.
  23. *
  24. * @param string|array $globs A single glob path or array of paths
  25. * @param array $filters An array of filters
  26. * @param string $root The root directory
  27. */
  28. public function __construct($globs, $filters = array(), $root = null)
  29. {
  30. $this->globs = (array) $globs;
  31. $this->initialized = false;
  32. parent::__construct(array(), $filters, $root);
  33. }
  34. public function all()
  35. {
  36. if (!$this->initialized) {
  37. $this->initialize();
  38. }
  39. return parent::all();
  40. }
  41. public function load(FilterInterface $additionalFilter = null)
  42. {
  43. if (!$this->initialized) {
  44. $this->initialize();
  45. }
  46. parent::load($additionalFilter);
  47. }
  48. public function dump(FilterInterface $additionalFilter = null)
  49. {
  50. if (!$this->initialized) {
  51. $this->initialize();
  52. }
  53. return parent::dump($additionalFilter);
  54. }
  55. public function getLastModified()
  56. {
  57. if (!$this->initialized) {
  58. $this->initialize();
  59. }
  60. return parent::getLastModified();
  61. }
  62. public function getIterator()
  63. {
  64. if (!$this->initialized) {
  65. $this->initialize();
  66. }
  67. return parent::getIterator();
  68. }
  69. /**
  70. * Initializes the collection based on the glob(s) passed in.
  71. */
  72. private function initialize()
  73. {
  74. foreach ($this->globs as $glob) {
  75. if (false !== $paths = glob($glob)) {
  76. foreach ($paths as $path) {
  77. $this->add(new FileAsset($path, array(), $this->getSourceRoot()));
  78. }
  79. }
  80. }
  81. $this->initialized = true;
  82. }
  83. }