FileResource.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. * FileResource represents a resource stored on the filesystem.
  13. *
  14. * The resource can be a file or a directory.
  15. *
  16. * @author Fabien Potencier <fabien@symfony.com>
  17. *
  18. * @final
  19. */
  20. class FileResource implements SelfCheckingResourceInterface
  21. {
  22. /**
  23. * @var string|false
  24. */
  25. private $resource;
  26. /**
  27. * @param string $resource The file path to the resource
  28. *
  29. * @throws \InvalidArgumentException
  30. */
  31. public function __construct(string $resource)
  32. {
  33. $this->resource = realpath($resource) ?: (file_exists($resource) ? $resource : false);
  34. if (false === $this->resource) {
  35. throw new \InvalidArgumentException(sprintf('The file "%s" does not exist.', $resource));
  36. }
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function __toString(): string
  42. {
  43. return $this->resource;
  44. }
  45. /**
  46. * @return string The canonicalized, absolute path to the resource
  47. */
  48. public function getResource(): string
  49. {
  50. return $this->resource;
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function isFresh(int $timestamp): bool
  56. {
  57. return false !== ($filemtime = @filemtime($this->resource)) && $filemtime <= $timestamp;
  58. }
  59. }