DirectoryResource.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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.potencier@symfony-project.com>
  15. */
  16. class DirectoryResource implements ResourceInterface
  17. {
  18. protected $resource;
  19. /**
  20. * Constructor.
  21. *
  22. * @param string $resource The file path to the resource
  23. */
  24. public function __construct($resource)
  25. {
  26. $this->resource = realpath($resource);
  27. }
  28. /**
  29. * Returns a string representation of the Resource.
  30. *
  31. * @return string A string representation of the Resource
  32. */
  33. public function __toString()
  34. {
  35. return (string) $this->resource;
  36. }
  37. /**
  38. * Returns the resource tied to this Resource.
  39. *
  40. * @return mixed The resource
  41. */
  42. public function getResource()
  43. {
  44. return $this->resource;
  45. }
  46. /**
  47. * Returns true if the resource has not been updated since the given timestamp.
  48. *
  49. * @param integer $timestamp The last time the resource was loaded
  50. *
  51. * @return Boolean true if the resource has not been updated, false otherwise
  52. */
  53. public function isFresh($timestamp)
  54. {
  55. if (!file_exists($this->resource)) {
  56. return false;
  57. }
  58. $newestMTime = 0;
  59. foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->resource), \RecursiveIteratorIterator::LEAVES_ONLY) as $file) {
  60. $newestMTime = max(filemtime($file), $newestMTime);
  61. }
  62. return $newestMTime < $timestamp;
  63. }
  64. }