Skip to content

Commit

Permalink
[Config] getFilteredChildResources() method added to DirectoryResource
Browse files Browse the repository at this point in the history
  • Loading branch information
everzet committed Jun 20, 2012
1 parent 6b39688 commit 1f9ba38
Showing 1 changed file with 58 additions and 18 deletions.
76 changes: 58 additions & 18 deletions src/Symfony/Component/Config/Resource/DirectoryResource.php
Expand Up @@ -36,29 +36,15 @@ public function __construct($resource, $pattern = null)
/**
* 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)
public function getFilteredChilds()
{
$iterator = $recursive
? new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->resource), \RecursiveIteratorIterator::SELF_FIRST)
: new \DirectoryIterator($this->resource);

$childs = array();
foreach ($iterator as $file) {
foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->resource), \RecursiveIteratorIterator::SELF_FIRST) 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;
}
if (!$this->isFileMatchesFilters($file)) {
continue;
}

// always monitor directories for changes, except the .. entries
Expand All @@ -73,6 +59,36 @@ public function getFilteredChilds($recursive = true)
return $childs;
}

/**
* Returns child resources that matches directory filters.
*
* @return array
*/
public function getFilteredChildResources()
{
$iterator = new \DirectoryIterator($this->resource);

$resources = array();
foreach ($iterator as $file) {
// if regex filtering is enabled only return matching files
if (!$this->isFileMatchesFilters($file)) {
continue;
}

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

$resources[] = $file->isDir()
? new DirectoryResource((string) $file)
: new FileResource((string) $file);
}

return $resources;
}

/**
* Returns a string representation of the Resource.
*
Expand Down Expand Up @@ -151,4 +167,28 @@ public function unserialize($serialized)
{
list($this->resource, $this->pattern) = unserialize($serialized);
}

/**
* Checks that passed file matches specified in resource filters.
*
* @param \SplFileInfo $file
*
* @return Boolean
*/
private function isFileMatchesFilters(\SplFileInfo $file)
{
if (isset($this->filterRegexList) && $file->isFile()) {
$regexMatched = false;
foreach ($this->filterRegexList as $regex) {
if (preg_match($regex, (string) $file)) {
$regexMatched = true;
}
}
if (!$regexMatched) {
return false;
}
}

return true;
}
}

0 comments on commit 1f9ba38

Please sign in to comment.