DirectoryResource.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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\Component\Config\Resource;
  11. /**
  12. * DirectoryResource represents a resources stored in a subdirectory tree.
  13. *
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. */
  16. class DirectoryResource implements SelfCheckingResourceInterface, \Serializable
  17. {
  18. private $resource;
  19. private $pattern;
  20. /**
  21. * @param string $resource The file path to the resource
  22. * @param string|null $pattern A pattern to restrict monitored files
  23. */
  24. public function __construct($resource, $pattern = null)
  25. {
  26. $this->resource = $resource;
  27. $this->pattern = $pattern;
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function __toString()
  33. {
  34. return md5(serialize(array($this->resource, $this->pattern)));
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function getResource()
  40. {
  41. return $this->resource;
  42. }
  43. /**
  44. * Returns the pattern to restrict monitored files.
  45. *
  46. * @return string|null
  47. */
  48. public function getPattern()
  49. {
  50. return $this->pattern;
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function isFresh($timestamp)
  56. {
  57. if (!is_dir($this->resource)) {
  58. return false;
  59. }
  60. if ($timestamp < filemtime($this->resource)) {
  61. return false;
  62. }
  63. foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->resource), \RecursiveIteratorIterator::SELF_FIRST) as $file) {
  64. // if regex filtering is enabled only check matching files
  65. if ($this->pattern && $file->isFile() && !preg_match($this->pattern, $file->getBasename())) {
  66. continue;
  67. }
  68. // always monitor directories for changes, except the .. entries
  69. // (otherwise deleted files wouldn't get detected)
  70. if ($file->isDir() && '/..' === substr($file, -3)) {
  71. continue;
  72. }
  73. // for broken links
  74. try {
  75. $fileMTime = $file->getMTime();
  76. } catch (\RuntimeException $e) {
  77. continue;
  78. }
  79. // early return if a file's mtime exceeds the passed timestamp
  80. if ($timestamp < $fileMTime) {
  81. return false;
  82. }
  83. }
  84. return true;
  85. }
  86. public function serialize()
  87. {
  88. return serialize(array($this->resource, $this->pattern));
  89. }
  90. public function unserialize($serialized)
  91. {
  92. list($this->resource, $this->pattern) = unserialize($serialized);
  93. }
  94. }