composer package updates
[openemr.git] / vendor / symfony / config / Resource / DirectoryResource.php
blobb65d40ae4400ed735c11096e4c385569e49c7959
1 <?php
3 /*
4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Config\Resource;
14 /**
15 * DirectoryResource represents a resources stored in a subdirectory tree.
17 * @author Fabien Potencier <fabien@symfony.com>
19 class DirectoryResource implements SelfCheckingResourceInterface, \Serializable
21 private $resource;
22 private $pattern;
24 /**
25 * @param string $resource The file path to the resource
26 * @param string|null $pattern A pattern to restrict monitored files
28 public function __construct($resource, $pattern = null)
30 $this->resource = $resource;
31 $this->pattern = $pattern;
34 /**
35 * {@inheritdoc}
37 public function __toString()
39 return md5(serialize(array($this->resource, $this->pattern)));
42 /**
43 * {@inheritdoc}
45 public function getResource()
47 return $this->resource;
50 /**
51 * Returns the pattern to restrict monitored files.
53 * @return string|null
55 public function getPattern()
57 return $this->pattern;
60 /**
61 * {@inheritdoc}
63 public function isFresh($timestamp)
65 if (!is_dir($this->resource)) {
66 return false;
69 if ($timestamp < filemtime($this->resource)) {
70 return false;
73 foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->resource), \RecursiveIteratorIterator::SELF_FIRST) as $file) {
74 // if regex filtering is enabled only check matching files
75 if ($this->pattern && $file->isFile() && !preg_match($this->pattern, $file->getBasename())) {
76 continue;
79 // always monitor directories for changes, except the .. entries
80 // (otherwise deleted files wouldn't get detected)
81 if ($file->isDir() && '/..' === substr($file, -3)) {
82 continue;
85 // for broken links
86 try {
87 $fileMTime = $file->getMTime();
88 } catch (\RuntimeException $e) {
89 continue;
92 // early return if a file's mtime exceeds the passed timestamp
93 if ($timestamp < $fileMTime) {
94 return false;
98 return true;
101 public function serialize()
103 return serialize(array($this->resource, $this->pattern));
106 public function unserialize($serialized)
108 list($this->resource, $this->pattern) = unserialize($serialized);