1<?php
2
3/*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Symfony\Component\Config\Resource;
13
14/**
15 * FileResource represents a resource stored on the filesystem.
16 *
17 * The resource can be a file or a directory.
18 *
19 * @author Fabien Potencier <fabien@symfony.com>
20 */
21class FileResource implements SelfCheckingResourceInterface, \Serializable
22{
23    /**
24     * @var string|false
25     */
26    private $resource;
27
28    /**
29     * @param string $resource The file path to the resource
30     *
31     * @throws \InvalidArgumentException
32     */
33    public function __construct($resource)
34    {
35        $this->resource = realpath($resource) ?: (file_exists($resource) ? $resource : false);
36
37        if (false === $this->resource) {
38            throw new \InvalidArgumentException(sprintf('The file "%s" does not exist.', $resource));
39        }
40    }
41
42    /**
43     * {@inheritdoc}
44     */
45    public function __toString()
46    {
47        return $this->resource;
48    }
49
50    /**
51     * @return string The canonicalized, absolute path to the resource
52     */
53    public function getResource()
54    {
55        return $this->resource;
56    }
57
58    /**
59     * {@inheritdoc}
60     */
61    public function isFresh($timestamp)
62    {
63        return false !== ($filemtime = @filemtime($this->resource)) && $filemtime <= $timestamp;
64    }
65
66    /**
67     * @internal
68     */
69    public function serialize()
70    {
71        return serialize($this->resource);
72    }
73
74    /**
75     * @internal
76     */
77    public function unserialize($serialized)
78    {
79        $this->resource = unserialize($serialized);
80    }
81}
82