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;
15 * DirectoryResource represents a resources stored in a subdirectory tree.
17 * @author Fabien Potencier <fabien@symfony.com>
19 class DirectoryResource implements SelfCheckingResourceInterface, \Serializable
25 * @param string $resource The file path to the resource
26 * @param string|null $pattern A pattern to restrict monitored files
28 * @throws \InvalidArgumentException
30 public function __construct($resource, $pattern = null)
32 $this->resource = realpath($resource) ?: (file_exists($resource) ? $resource : false);
33 $this->pattern = $pattern;
35 if (false === $this->resource || !is_dir($this->resource)) {
36 throw new \InvalidArgumentException(sprintf('The directory "%s" does not exist.', $resource));
43 public function __toString()
45 return md5(serialize(array($this->resource, $this->pattern)));
49 * @return string The file path to the resource
51 public function getResource()
53 return $this->resource;
57 * Returns the pattern to restrict monitored files.
61 public function getPattern()
63 return $this->pattern;
69 public function isFresh($timestamp)
71 if (!is_dir($this->resource)) {
75 if ($timestamp < filemtime($this->resource)) {
79 foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->resource), \RecursiveIteratorIterator::SELF_FIRST) as $file) {
80 // if regex filtering is enabled only check matching files
81 if ($this->pattern && $file->isFile() && !preg_match($this->pattern, $file->getBasename())) {
85 // always monitor directories for changes, except the .. entries
86 // (otherwise deleted files wouldn't get detected)
87 if ($file->isDir() && '/..' === substr($file, -3)) {
93 $fileMTime = $file->getMTime();
94 } catch (\RuntimeException $e) {
98 // early return if a file's mtime exceeds the passed timestamp
99 if ($timestamp < $fileMTime) {
107 public function serialize()
109 return serialize(array($this->resource, $this->pattern));
112 public function unserialize($serialized)
114 list($this->resource, $this->pattern) = unserialize($serialized);