FileResource.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. public function __construct($resource)
  28. {
  29. $this->resource = realpath($resource) ?: (file_exists($resource) ? $resource : false);
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function __toString()
  35. {
  36. return (string) $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. if (false === $this->resource || !file_exists($this->resource)) {
  51. return false;
  52. }
  53. return filemtime($this->resource) <= $timestamp;
  54. }
  55. public function serialize()
  56. {
  57. return serialize($this->resource);
  58. }
  59. public function unserialize($serialized)
  60. {
  61. $this->resource = unserialize($serialized);
  62. }
  63. }