8000 [Config] getFilteredChildResources() method added to DirectoryResource · lauris/symfony@1f9ba38 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1f9ba38

Browse files
committed
[Config] getFilteredChildResources() method added to DirectoryResource
1 parent 6b39688 commit 1f9ba38

File tree

1 file changed

+58
-18
lines changed

1 file changed

+58
-18
lines changed

src/Symfony/Component/Config/Resource/DirectoryResource.php

Lines changed: 58 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,29 +36,15 @@ public function __construct($resource, $pattern = null)
3636
/**
3737
* Returns the list of filtered file and directory childs of directory resource.
3838
*
39-
* @param Boolean $recursive search for files recursive
40-
*
4139
* @return array An array of files
4240
*/
43-
public function getFilteredChilds($recursive = true)
41+
public function getFilteredChilds()
4442
{
45-
$iterator = $recursive
46-
? new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->resource), \RecursiveIteratorIterator::SELF_FIRST)
47-
: new \DirectoryIterator($this->resource);
48-
4943
$childs = array();
50-
foreach ($iterator as $file) {
44+
foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->resource), \RecursiveIteratorIterator::SELF_FIRST) as $file) {
5145
// if regex filtering is enabled only return matching files
52-
if (isset($this->filterRegexList) && $file->isFile()) {
53-
$regexMatched = false;
54-
foreach ($this->filterRegexList as $regex) {
55-
if (preg_match($regex, (string) $file)) {
56-
$regexMatched = true;
57-
}
58-
}
59-
if (!$regexMatched) {
60-
continue;
61-
}
46+
if (!$this->isFileMatchesFilters($file)) {
47+
continue;
6248
}
6349

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

62+
/**
63+
* Returns child resources that matches directory filters.
64+
*
65+
* @return array
66+
*/
67+
public function getFilteredChildResources()
68+
{
69+
$iterator = new \DirectoryIterator($this->resource);
70+
71+
$resources = array();
72+
foreach ($iterator as $file) {
73+
// if regex filtering is enabled only return matching files
74+
if (!$this->isFileMatchesFilters($file)) {
75+
continue;
76+
}
77+
78+
// always monitor directories for changes, except the .. entries
79+
// (otherwise deleted files wouldn't get detected)
80+
if ($file->isDir() && '/..' === substr($file, -3)) {
81+
continue;
82+
}
83+
84+
$resources[] = $file->isDir()
85+
? new DirectoryResource((string) $file)
86+
: new FileResource((string) $file);
87+
}
88+
89+
return $resources;
90+
}
91+
7692
/**
7793
* Returns a string representation of the Resource.
7894
*
@@ -151,4 +167,28 @@ public function unserialize($serialized)
151167
{
152168
list($this->resource, $this->pattern) = unserialize($serialized);
153169
}
170+
171+
/**
172+
* Checks that passed file matches specified in resource filters.
173+
*
174+
* @param \SplFileInfo $file
175+
*
176+
* @return Boolean
177+
*/
178+
private function isFileMatchesFilters(\SplFileInfo $file)
179+
{
180+
if (isset($this->filterRegexList) && $file->isFile()) {
181+
$regexMatched = false;
182+
foreach ($this->filterRegexList as $regex) {
183+
if (preg_match($regex, (string) $file)) {
184+
$regexMatched = true;
185+
}
186+
}
187+
if (!$regexMatched) {
188+
return false;
189+
}
190+
}
191+
192+
return true;
193+
}
154194
}

0 commit comments

Comments
 (0)
0