07280b4b877f2b4222a6e7f697280bb76c4ac760
[yaffs-website] / vendor / symfony / config / Resource / DirectoryResource.php
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
12 namespace Symfony\Component\Config\Resource;
13
14 /**
15  * DirectoryResource represents a resources stored in a subdirectory tree.
16  *
17  * @author Fabien Potencier <fabien@symfony.com>
18  */
19 class DirectoryResource implements SelfCheckingResourceInterface, \Serializable
20 {
21     private $resource;
22     private $pattern;
23
24     /**
25      * Constructor.
26      *
27      * @param string      $resource The file path to the resource
28      * @param string|null $pattern  A pattern to restrict monitored files
29      *
30      * @throws \InvalidArgumentException
31      */
32     public function __construct($resource, $pattern = null)
33     {
34         $this->resource = realpath($resource) ?: (file_exists($resource) ? $resource : false);
35         $this->pattern = $pattern;
36
37         if (false === $this->resource || !is_dir($this->resource)) {
38             throw new \InvalidArgumentException(sprintf('The directory "%s" does not exist.', $resource));
39         }
40     }
41
42     /**
43      * {@inheritdoc}
44      */
45     public function __toString()
46     {
47         return md5(serialize(array($this->resource, $this->pattern)));
48     }
49
50     /**
51      * @return string The file path to the resource
52      */
53     public function getResource()
54     {
55         return $this->resource;
56     }
57
58     /**
59      * Returns the pattern to restrict monitored files.
60      *
61      * @return string|null
62      */
63     public function getPattern()
64     {
65         return $this->pattern;
66     }
67
68     /**
69      * {@inheritdoc}
70      */
71     public function isFresh($timestamp)
72     {
73         if (!is_dir($this->resource)) {
74             return false;
75         }
76
77         if ($timestamp < filemtime($this->resource)) {
78             return false;
79         }
80
81         foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->resource), \RecursiveIteratorIterator::SELF_FIRST) as $file) {
82             // if regex filtering is enabled only check matching files
83             if ($this->pattern && $file->isFile() && !preg_match($this->pattern, $file->getBasename())) {
84                 continue;
85             }
86
87             // always monitor directories for changes, except the .. entries
88             // (otherwise deleted files wouldn't get detected)
89             if ($file->isDir() && '/..' === substr($file, -3)) {
90                 continue;
91             }
92
93             // for broken links
94             try {
95                 $fileMTime = $file->getMTime();
96             } catch (\RuntimeException $e) {
97                 continue;
98             }
99
100             // early return if a file's mtime exceeds the passed timestamp
101             if ($timestamp < $fileMTime) {
102                 return false;
103             }
104         }
105
106         return true;
107     }
108
109     public function serialize()
110     {
111         return serialize(array($this->resource, $this->pattern));
112     }
113
114     public function unserialize($serialized)
115     {
116         list($this->resource, $this->pattern) = unserialize($serialized);
117     }
118 }