FileResource.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. * Constructor.
  26. *
  27. * @param string $resource The file path to the resource
  28. */
  29. public function __construct($resource)
  30. {
  31. $this->resource = realpath($resource) ?: (file_exists($resource) ? $resource : false);
  32. }
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public function __toString()
  37. {
  38. return (string) $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. if (false === $this->resource || !file_exists($this->resource)) {
  53. return false;
  54. }
  55. return filemtime($this->resource) <= $timestamp;
  56. }
  57. public function serialize()
  58. {
  59. return serialize($this->resource);
  60. }
  61. public function unserialize($serialized)
  62. {
  63. $this->resource = unserialize($serialized);
  64. }
  65. }