DirectoryResource.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 ResourceInterface, \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 $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. * Returns a string representation of the Resource.
  33. *
  34. * @return string A string representation of the Resource
  35. */
  36. public function __toString()
  37. {
  38. return (string) $this->resource;
  39. }
  40. /**
  41. * Returns the resource tied to this Resource.
  42. *
  43. * @return mixed The resource
  44. */
  45. public function getResource()
  46. {
  47. return $this->resource;
  48. }
  49. public function getPattern()
  50. {
  51. return $this->pattern;
  52. }
  53. /**
  54. * Returns true if the resource has not been updated since the given timestamp.
  55. *
  56. * @param integer $timestamp The last time the resource was loaded
  57. *
  58. * @return Boolean true if the resource has not been updated, false otherwise
  59. */
  60. public function isFresh($timestamp)
  61. {
  62. if (!is_dir($this->resource)) {
  63. return false;
  64. }
  65. $newestMTime = filemtime($this->resource);
  66. foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->resource), \RecursiveIteratorIterator::SELF_FIRST) as $file) {
  67. // if regex filtering is enabled only check matching files
  68. if ($this->pattern && $file->isFile() && !preg_match($this->pattern, $file->getBasename())) {
  69. continue;
  70. }
  71. // always monitor directories for changes, except the .. entries
  72. // (otherwise deleted files wouldn't get detected)
  73. if ($file->isDir() && '/..' === substr($file, -3)) {
  74. continue;
  75. }
  76. $newestMTime = max($file->getMTime(), $newestMTime);
  77. }
  78. return $newestMTime < $timestamp;
  79. }
  80. public function serialize()
  81. {
  82. return serialize(array($this->resource, $this->pattern));
  83. }
  84. public function unserialize($serialized)
  85. {
  86. list($this->resource, $this->pattern) = unserialize($serialized);
  87. }
  88. }