-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Config] additions from ResourceWatcher #5744
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
51f4be2
1e5e3c1
fb1d244
073ca68
7ea86f9
0c4e7cb
49a12d2
0155e26
c3fdbc0
e43bff8
81980d7
1ad8e0e
c8efa9e
cca8598
0fc4282
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,7 @@ | |
* | ||
* @author Fabien Potencier <fabien@symfony.com> | ||
*/ | ||
class DirectoryResource implements ResourceInterface, \Serializable | ||
class DirectoryResource implements ResourceInterface | ||
{ | ||
private $resource; | ||
private $pattern; | ||
|
@@ -33,6 +33,82 @@ public function __construct($resource, $pattern = null) | |
$this->pattern = $pattern; | ||
} | ||
|
||
/** | ||
* Returns the list of filtered file and directory children of directory resource. | ||
* | ||
* @return array An array of files | ||
*/ | ||
public function getFilteredChildren() | ||
{ | ||
if (!$this->exists()) { | ||
return array(); | ||
} | ||
|
||
$iterator = new \RecursiveIteratorIterator( | ||
new \RecursiveDirectoryIterator($this->resource, \FilesystemIterator::SKIP_DOTS), | ||
\RecursiveIteratorIterator::SELF_FIRST | ||
); | ||
|
||
$children = array(); | ||
foreach ($iterator as $file) { | ||
// if regex filtering is enabled only return matching files | ||
if ($file->isFile() && !$this->hasFile($file)) { | ||
continue; | ||
} | ||
|
||
// always monitor directories for changes, except the .. entries | ||
// (otherwise deleted files wouldn't get detected) | ||
if ($file->isDir() && '/..' === substr($file, -3)) { | ||
continue; | ||
} | ||
|
||
$children[] = $file; | ||
} | ||
|
||
return $children; | ||
} | ||
|
||
/** | ||
* Returns child resources that matches directory filters. | ||
* | ||
* @return array | ||
*/ | ||
public function getFilteredResources() | ||
{ | ||
if (!$this->exists()) { | ||
return array(); | ||
} | ||
|
||
$iterator = new \DirectoryIterator($this->resource); | ||
|
||
$resources = array(); | ||
foreach ($iterator as $file) { | ||
// if regex filtering is enabled only return matching files | ||
if ($file->isFile() && !$this->hasFile($file)) { | ||
continue; | ||
} | ||
|
||
// always monitor directories for changes, except the .. entries | ||
// (otherwise deleted files wouldn't get detected) | ||
if ($file->isDir() && '/..' === substr($file, -3)) { | ||
continue; | ||
} | ||
|
||
// if file is dot - continue | ||
if ($file->isDot()) { | ||
continue; | ||
} | ||
|
||
if ($file->isFile()) { | ||
$resources[] = new FileResource($file->getRealPath()); | ||
} elseif ($file->isDir()) { | ||
$resources[] = new DirectoryResource($file->getRealPath()); | ||
} | ||
} | ||
|
||
return $resources; | ||
} | ||
|
||
/** | ||
* Returns a string representation of the Resource. | ||
* | ||
|
@@ -53,11 +129,62 @@ public function getResource() | |
return $this->resource; | ||
} | ||
|
||
/** | ||
* Returns check pattern. | ||
* | ||
* @return mixed | ||
*/ | ||
public function getPattern() | ||
{ | ||
return $this->pattern; | ||
} | ||
|
||
/** | ||
* Checks that passed file exists in resource and matches resource filters. | ||
* | ||
* @param SplFileInfo|string $file | ||
* | ||
* @return Boolean | ||
*/ | ||
public function hasFile($file) | ||
{ | ||
if (!$file instanceof \SplFileInfo) { | ||
$file = new \SplFileInfo($file); | ||
} | ||
|
||
if (0 !== strpos($file->getRealPath(), realpath($this->resource))) { | ||
return false; | ||
} | ||
|
||
if ($this->pattern) { | ||
return (bool) preg_match($this->pattern, $file->getBasename()); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Returns resource mtime. | ||
* | ||
* @return integer | ||
*/ | ||
public function getModificationTime() | ||
{ | ||
if (!$this->exists()) { | ||
return -1; | ||
} | ||
|
||
clearstatcache(true, $this->resource); | ||
$newestMTime = filemtime($this->resource); | ||
|
||
foreach ($this->getFilteredChildren() as $file) { | ||
clearstatcache(true, (string) $file); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
$newestMTime = max($file->getMTime(), $newestMTime); | ||
} | ||
|
||
return $newestMTime; | ||
} | ||
|
||
/** | ||
* Returns true if the resource has not been updated since the given timestamp. | ||
* | ||
|
@@ -67,27 +194,31 @@ public function getPattern() | |
*/ | ||
public function isFresh($timestamp) | ||
{ | ||
if (!is_dir($this->resource)) { | ||
if (!$this->exists()) { | ||
return false; | ||
} | ||
|
||
$newestMTime = filemtime($this->resource); | ||
foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->resource), \RecursiveIteratorIterator::SELF_FIRST) as $file) { | ||
// if regex filtering is enabled only check matching files | ||
if ($this->pattern && $file->isFile() && !preg_match($this->pattern, $file->getBasename())) { | ||
continue; | ||
} | ||
|
||
// always monitor directories for changes, except the .. entries | ||
// (otherwise deleted files wouldn't get detected) | ||
if ($file->isDir() && '/..' === substr($file, -3)) { | ||
continue; | ||
} | ||
return $this->getModificationTime() < $timestamp; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Baachi The existing logic is also using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's true, but the |
||
} | ||
|
||
$newestMTime = max($file->getMTime(), $newestMTime); | ||
} | ||
/** | ||
* Returns true if the resource exists in the filesystem. | ||
* | ||
* @return Boolean | ||
*/ | ||
public function exists() | ||
{ | ||
return is_dir($this->resource); | ||
} | ||
|
||
return $newestMTime < $timestamp; | ||
/** | ||
* Returns unique resource ID. | ||
* | ||
* @return string | ||
*/ | ||
public function getId() | ||
{ | ||
return md5('d'.$this->resource.$this->pattern); | ||
} | ||
|
||
public function serialize() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@everzet is the check for
..
really needed as you are using\FilesystemIterator::SKIP_DOTS
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI, removing this
continue
does not break the testsuite