Navigation Menu

Skip to content

Commit

Permalink
[Config] moved DirectoryResource childs retrieving to the special get…
Browse files Browse the repository at this point in the history
…FilteredChilds method
  • Loading branch information
everzet committed Jun 20, 2012
1 parent 45df2e6 commit 6b39688
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 16 deletions.
57 changes: 41 additions & 16 deletions src/Symfony/Component/Config/Resource/DirectoryResource.php
Expand Up @@ -33,6 +33,46 @@ public function __construct($resource, $pattern = null)
$this->pattern = $pattern;
}

/**
* Returns the list of filtered file and directory childs of directory resource.
*
* @param Boolean $recursive search for files recursive
*
* @return array An array of files
*/
public function getFilteredChilds($recursive = true)
{
$iterator = $recursive
? new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->resource), \RecursiveIteratorIterator::SELF_FIRST)
: new \DirectoryIterator($this->resource);

$childs = array();
foreach ($iterator as $file) {
// if regex filtering is enabled only return matching files
if (isset($this->filterRegexList) && $file->isFile()) {
$regexMatched = false;
foreach ($this->filterRegexList as $regex) {
if (preg_match($regex, (string) $file)) {
$regexMatched = true;
}
}
if (!$regexMatched) {
continue;
}
}

// always monitor directories for changes, except the .. entries
// (otherwise deleted files wouldn't get detected)
if ($file->isDir() && '/..' === substr($file, -3)) {
continue;
}

$childs[] = $file;
}

return $childs;
}

/**
* Returns a string representation of the Resource.
*
Expand Down Expand Up @@ -66,24 +106,9 @@ public function getPattern()
public function getModificationTime()
{
clearstatcache(true, $this->resource);

if (!is_dir($this->resource)) {
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;
}

foreach ($this->getFilteredChilds() as $file) {
clearstatcache(true, (string) $file);
$newestMTime = max($file->getMTime(), $newestMTime);
}
Expand Down
Expand Up @@ -161,6 +161,7 @@ public function testIsFreshModifySubdirectory()

/**
* @covers Symfony\Component\Config\Resource\DirectoryResource::isFresh
* @covers Symfony\Component\Config\Resource\DirectoryResource::getFilteredChilds
*/
public function testFilterRegexListNoMatch()
{
Expand Down

0 comments on commit 6b39688

Please sign in to comment.