Skip to content

Commit ad9d1ba

Browse files
authored
Merge pull request #5862 from kenjis/feat-get_filenames-includeDir
feat: add `$includeDir` option to `get_filenames()`
2 parents 64308d4 + 3fe7b8a commit ad9d1ba

4 files changed

Lines changed: 36 additions & 11 deletions

File tree

system/Helpers/filesystem_helper.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,14 @@ function delete_files(string $path, bool $delDir = false, bool $htdocs = false,
194194
* @param string $sourceDir Path to source
195195
* @param bool|null $includePath Whether to include the path as part of the filename; false for no path, null for a relative path, true for full path
196196
* @param bool $hidden Whether to include hidden files (files beginning with a period)
197+
* @param bool $includeDir Whether to include directories
197198
*/
198-
function get_filenames(string $sourceDir, ?bool $includePath = false, bool $hidden = false): array
199-
{
199+
function get_filenames(
200+
string $sourceDir,
201+
?bool $includePath = false,
202+
bool $hidden = false,
203+
bool $includeDir = true
204+
): array {
200205
$files = [];
201206

202207
$sourceDir = realpath($sourceDir) ?: $sourceDir;
@@ -212,12 +217,14 @@ function get_filenames(string $sourceDir, ?bool $includePath = false, bool $hidd
212217
continue;
213218
}
214219

215-
if ($includePath === false) {
216-
$files[] = $basename;
217-
} elseif ($includePath === null) {
218-
$files[] = str_replace($sourceDir, '', $name);
219-
} else {
220-
$files[] = $name;
220+
if ($includeDir || ! $object->isDir()) {
221+
if ($includePath === false) {
222+
$files[] = $basename;
223+
} elseif ($includePath === null) {
224+
$files[] = str_replace($sourceDir, '', $name);
225+
} else {
226+
$files[] = $name;
227+
}
221228
}
222229
}
223230
} catch (Throwable $e) {

tests/system/Helpers/FilesystemHelperTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,22 @@ public function testGetFilenames()
305305
$this->assertSame($expected, get_filenames($vfs->url(), false));
306306
}
307307

308+
public function testGetFilenamesWithoutDirectories()
309+
{
310+
$vfs = vfsStream::setup('root', null, $this->structure);
311+
312+
$filenames = get_filenames($vfs->url(), true, false, false);
313+
314+
$expected = [
315+
'vfs://root/boo/far',
316+
'vfs://root/boo/faz',
317+
'vfs://root/foo/bar',
318+
'vfs://root/foo/baz',
319+
'vfs://root/simpleFile',
320+
];
321+
$this->assertSame($expected, $filenames);
322+
}
323+
308324
public function testGetFilenamesWithHidden()
309325
{
310326
$this->assertTrue(function_exists('get_filenames'));

user_guide_src/source/changelogs/v4.2.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Enhancements
3737
- Exception information logged through ``log_message()`` has now improved. It now includes the file and line where the exception originated. It also does not truncate the message anymore.
3838
- The log format has also changed. If users are depending on the log format in their apps, the new log format is "<1-based count> <cleaned filepath>(<line>): <class><function><args>"
3939
- Added support for webp files to **app/Config/Mimes.php**.
40+
- Added 4th parameter ``$includeDir`` to ``get_filenames()``. See :php:func:`get_filenames`.
4041

4142
Changes
4243
*******

user_guide_src/source/helpers/filesystem_helper.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,12 @@ The following functions are available:
150150

151151
.. note:: The files must be writable or owned by the system in order to be deleted.
152152

153-
.. php:function:: get_filenames($source_dir[, $include_path = false])
153+
.. php:function:: get_filenames($sourceDir[, $includePath = false[, $hidden = false[, $includeDir = true]]])
154154
155-
:param string $source_dir: Directory path
156-
:param bool|null $include_path: Whether to include the path as part of the filename; false for no path, null for the path relative to $source_dir, true for the full path
155+
:param string $sourceDir: Directory path
156+
:param bool|null $includePath: Whether to include the path as part of the filename; false for no path, null for the path relative to ``$sourceDir``, true for the full path
157157
:param bool $hidden: Whether to include hidden files (files beginning with a period)
158+
:param bool $includeDir: Whether to include directories in the array output
158159
:returns: An array of file names
159160
:rtype: array
160161

0 commit comments

Comments
 (0)