FileExistenceResource.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. * FileExistenceResource represents a resource stored on the filesystem.
  13. * Freshness is only evaluated against resource creation or deletion.
  14. *
  15. * The resource can be a file or a directory.
  16. *
  17. * @author Charles-Henri Bruyand <charleshenri.bruyand@gmail.com>
  18. */
  19. class FileExistenceResource implements SelfCheckingResourceInterface, \Serializable
  20. {
  21. private $resource;
  22. private $exists;
  23. /**
  24. * Constructor.
  25. *
  26. * @param string $resource The file path to the resource
  27. */
  28. public function __construct($resource)
  29. {
  30. $this->resource = (string) $resource;
  31. $this->exists = file_exists($resource);
  32. }
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public function __toString()
  37. {
  38. return $this->resource;
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function getResource()
  44. {
  45. return $this->resource;
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function isFresh($timestamp)
  51. {
  52. return file_exists($this->resource) === $this->exists;
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. public function serialize()
  58. {
  59. return serialize(array($this->resource, $this->exists));
  60. }
  61. /**
  62. * {@inheritdoc}
  63. */
  64. public function unserialize($serialized)
  65. {
  66. list($this->resource, $this->exists) = unserialize($serialized);
  67. }
  68. }