Skip to content

Commit 387bc5a

Browse files
committed
Refactor get_filenames output
1 parent fa225ab commit 387bc5a

2 files changed

Lines changed: 34 additions & 38 deletions

File tree

system/Helpers/filesystem_helper.php

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -211,43 +211,33 @@ function delete_files(string $path, bool $del_dir = false, bool $htdocs = false,
211211
*
212212
* @param string $source_dir Path to source
213213
* @param boolean $include_path Whether to include the path as part of the filename
214-
* @param boolean $recursion Internal variable to determine recursion status - do not use in calls
215214
*
216215
* @return array
217216
*/
218-
function get_filenames(string $source_dir, bool $include_path = false, bool $recursion = false): array
217+
function get_filenames(string $source_dir, bool $include_path = false): array
219218
{
220-
static $fileData = [];
219+
$files = [];
220+
221+
$source_dir = rtrim($source_dir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
221222

222223
try
223224
{
224-
$fp = opendir($source_dir);
225-
// reset the array and make sure $source_dir has a trailing slash on the initial call
226-
if ($recursion === false)
225+
foreach (new RecursiveIteratorIterator(
226+
new RecursiveDirectoryIterator($source_dir, RecursiveDirectoryIterator::SKIP_DOTS),
227+
RecursiveIteratorIterator::SELF_FIRST
228+
) as $name => $object)
227229
{
228-
$fileData = [];
229-
$source_dir = rtrim(realpath($source_dir), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
230+
$files[] = $include_path ? $name : str_replace($source_dir, '', $name);
230231
}
231-
232-
while (false !== ($file = readdir($fp)))
233-
{
234-
if (is_dir($source_dir . $file) && $file[0] !== '.')
235-
{
236-
get_filenames($source_dir . $file . DIRECTORY_SEPARATOR, $include_path, true);
237-
}
238-
elseif ($file[0] !== '.')
239-
{
240-
$fileData[] = ($include_path === true) ? $source_dir . $file : $file;
241-
}
242-
}
243-
244-
closedir($fp);
245-
return $fileData;
246232
}
247-
catch (\Exception $fe)
233+
catch (\Throwable $e)
248234
{
249235
return [];
250236
}
237+
238+
sort($files);
239+
240+
return $files;
251241
}
252242
}
253243

tests/system/Helpers/FilesystemHelperTest.php

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -179,14 +179,17 @@ public function testDeleteFilesFailure()
179179

180180
public function testGetFilenames()
181181
{
182-
$this->assertTrue(function_exists('delete_files'));
182+
$this->assertTrue(function_exists('get_filenames'));
183183

184-
// Not sure the directory names should actually show up
185-
// here but this matches v3.x results.
186184
$expected = [
187-
'foo',
188-
'boo',
185+
'.hidden',
189186
'AnEmptyFolder',
187+
'boo',
188+
'boo/far',
189+
'boo/faz',
190+
'foo',
191+
'foo/bar',
192+
'foo/baz',
190193
'simpleFile',
191194
];
192195

@@ -197,19 +200,22 @@ public function testGetFilenames()
197200

198201
public function testGetFilenamesWithSource()
199202
{
200-
$this->assertTrue(function_exists('delete_files'));
203+
$this->assertTrue(function_exists('get_filenames'));
204+
205+
$vfs = vfsStream::setup('root', null, $this->structure);
201206

202-
// Not sure the directory names should actually show up
203-
// here but this matches v3.x results.
204207
$expected = [
205-
DIRECTORY_SEPARATOR . 'foo',
206-
DIRECTORY_SEPARATOR . 'boo',
207-
DIRECTORY_SEPARATOR . 'AnEmptyFolder',
208-
DIRECTORY_SEPARATOR . 'simpleFile',
208+
$vfs->url() . DIRECTORY_SEPARATOR . '.hidden',
209+
$vfs->url() . DIRECTORY_SEPARATOR . 'AnEmptyFolder',
210+
$vfs->url() . DIRECTORY_SEPARATOR . 'boo',
211+
$vfs->url() . DIRECTORY_SEPARATOR . 'boo/far',
212+
$vfs->url() . DIRECTORY_SEPARATOR . 'boo/faz',
213+
$vfs->url() . DIRECTORY_SEPARATOR . 'foo',
214+
$vfs->url() . DIRECTORY_SEPARATOR . 'foo/bar',
215+
$vfs->url() . DIRECTORY_SEPARATOR . 'foo/baz',
216+
$vfs->url() . DIRECTORY_SEPARATOR . 'simpleFile',
209217
];
210218

211-
$vfs = vfsStream::setup('root', null, $this->structure);
212-
213219
$this->assertEquals($expected, get_filenames($vfs->url(), true));
214220
}
215221

0 commit comments

Comments
 (0)