FileResource.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. class FileResource implements SelfCheckingResourceInterface, \Serializable
  19. {
  20. /**
  21. * @var string|false
  22. */
  23. private $resource;
  24. /**
  25. * @param string $resource The file path to the resource
  26. *
  27. * @throws \InvalidArgumentException
  28. */
  29. public function __construct(string $resource)
  30. {
  31. $this->resource = realpath($resource) ?: (file_exists($resource) ? $resource : false);
  32. if (false === $this->resource) {
  33. throw new \InvalidArgumentException(sprintf('The file "%s" does not exist.', $resource));
  34. }
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function __toString()
  40. {
  41. return $this->resource;
  42. }
  43. /**
  44. * @return string The canonicalized, absolute path to the resource
  45. */
  46. public function getResource()
  47. {
  48. return $this->resource;
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function isFresh($timestamp)
  54. {
  55. return false !== ($filemtime = @filemtime($this->resource)) && $filemtime <= $timestamp;
  56. }
  57. /**
  58. * @internal
  59. */
  60. public function serialize()
  61. {
  62. return serialize($this->resource);
  63. }
  64. /**
  65. * @internal
  66. */
  67. public function unserialize($serialized)
  68. {
  69. $this->resource = unserialize($serialized);
  70. }
  71. }