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
27 * @param string $resource The file path to the resource
28 * @param string|null $pattern A pattern to restrict monitored files
30 * @throws \InvalidArgumentException
32 public function __construct($resource, $pattern = null)
34 $this->resource = realpath($resource) ?: (file_exists($resource) ? $resource : false);
35 $this->pattern = $pattern;
37 if (false === $this->resource || !is_dir($this->resource)) {
38 throw new \InvalidArgumentException(sprintf('The directory "%s" does not exist.', $resource));
45 public function __toString()
47 return md5(serialize(array($this->resource, $this->pattern)));
51 * @return string The file path to the resource
53 public function getResource()
55 return $this->resource;
59 * Returns the pattern to restrict monitored files.
63 public function getPattern()
65 return $this->pattern;
71 public function isFresh($timestamp)
73 if (!is_dir($this->resource)) {
77 if ($timestamp < filemtime($this->resource)) {
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())) {
87 // always monitor directories for changes, except the .. entries
88 // (otherwise deleted files wouldn't get detected)
89 if ($file->isDir() && '/..' === substr($file, -3)) {
95 $fileMTime = $file->getMTime();
96 } catch (\RuntimeException $e) {
100 // early return if a file's mtime exceeds the passed timestamp
101 if ($timestamp < $fileMTime) {
109 public function serialize()
111 return serialize(array($this->resource, $this->pattern));
114 public function unserialize($serialized)
116 list($this->resource, $this->pattern) = unserialize($serialized);