FileExistenceResource.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. * @final
  20. */
  21. class FileExistenceResource implements SelfCheckingResourceInterface
  22. {
  23. private $resource;
  24. private $exists;
  25. /**
  26. * @param string $resource The file path to the resource
  27. */
  28. public function __construct(string $resource)
  29. {
  30. $this->resource = $resource;
  31. $this->exists = file_exists($resource);
  32. }
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public function __toString(): string
  37. {
  38. return $this->resource;
  39. }
  40. /**
  41. * @return string The file path to the resource
  42. */
  43. public function getResource(): string
  44. {
  45. return $this->resource;
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function isFresh(int $timestamp): bool
  51. {
  52. return file_exists($this->resource) === $this->exists;
  53. }
  54. }