DirectoryResource.php 2.8 KB

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