FileExistenceResource.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. * @param string $resource The file path to the resource
  25. */
  26. public function __construct($resource)
  27. {
  28. $this->resource = (string) $resource;
  29. $this->exists = file_exists($resource);
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function __toString()
  35. {
  36. return $this->resource;
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function getResource()
  42. {
  43. return $this->resource;
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function isFresh($timestamp)
  49. {
  50. return file_exists($this->resource) === $this->exists;
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function serialize()
  56. {
  57. return serialize(array($this->resource, $this->exists));
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. public function unserialize($serialized)
  63. {
  64. list($this->resource, $this->exists) = unserialize($serialized);
  65. }
  66. }