DirectoryResource.php 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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\Factory\Resource;
  11. /**
  12. * A resource is something formulae can be loaded from.
  13. *
  14. * @author Kris Wallsmith <kris.wallsmith@gmail.com>
  15. */
  16. class DirectoryResource implements IteratorResourceInterface
  17. {
  18. private $path;
  19. private $pattern;
  20. /**
  21. * Constructor.
  22. *
  23. * @param string $path A directory path
  24. * @param string $pattern A filename pattern
  25. */
  26. public function __construct($path, $pattern = null)
  27. {
  28. if (DIRECTORY_SEPARATOR != substr($path, -1)) {
  29. $path .= DIRECTORY_SEPARATOR;
  30. }
  31. $this->path = $path;
  32. $this->pattern = $pattern;
  33. }
  34. public function isFresh($timestamp)
  35. {
  36. if (!is_dir($this->path) || filemtime($this->path) > $timestamp) {
  37. return false;
  38. }
  39. foreach ($this as $resource) {
  40. if (!$resource->isFresh($timestamp)) {
  41. return false;
  42. }
  43. }
  44. return true;
  45. }
  46. /**
  47. * Returns the combined content of all inner resources.
  48. */
  49. public function getContent()
  50. {
  51. $content = array();
  52. foreach ($this as $resource) {
  53. $content[] = $resource->getContent();
  54. }
  55. return implode("\n", $content);
  56. }
  57. public function __toString()
  58. {
  59. return $this->path;
  60. }
  61. public function getIterator()
  62. {
  63. return is_dir($this->path)
  64. ? new DirectoryResourceIterator($this->getInnerIterator())
  65. : new \EmptyIterator();
  66. }
  67. protected function getInnerIterator()
  68. {
  69. return new DirectoryResourceFilterIterator(new \RecursiveDirectoryIterator($this->path, \RecursiveDirectoryIterator::FOLLOW_SYMLINKS), $this->pattern);
  70. }
  71. }
  72. /**
  73. * An iterator that converts file objects into file resources.
  74. *
  75. * @author Kris Wallsmith <kris.wallsmith@gmail.com>
  76. * @access private
  77. */
  78. class DirectoryResourceIterator extends \RecursiveIteratorIterator
  79. {
  80. public function current()
  81. {
  82. return new FileResource(parent::current()->getPathname());
  83. }
  84. }
  85. /**
  86. * Filters files by a basename pattern.
  87. *
  88. * @author Kris Wallsmith <kris.wallsmith@gmail.com>
  89. * @access private
  90. */
  91. class DirectoryResourceFilterIterator extends \RecursiveFilterIterator
  92. {
  93. protected $pattern;
  94. public function __construct(\RecursiveDirectoryIterator $iterator, $pattern = null)
  95. {
  96. parent::__construct($iterator);
  97. $this->pattern = $pattern;
  98. }
  99. public function accept()
  100. {
  101. $file = $this->current();
  102. $name = $file->getBasename();
  103. if ($file->isDir()) {
  104. return '.' != $name[0];
  105. } else {
  106. return null === $this->pattern || 0 < preg_match($this->pattern, $name);
  107. }
  108. }
  109. public function getChildren()
  110. {
  111. return new self(new \RecursiveDirectoryIterator($this->current()->getPathname(), \RecursiveDirectoryIterator::FOLLOW_SYMLINKS), $this->pattern);
  112. }
  113. }