Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add $includeDir option to get_filenames() #5862

Merged
merged 3 commits into from
Apr 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions system/Helpers/filesystem_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,14 @@ function delete_files(string $path, bool $delDir = false, bool $htdocs = false,
* @param string $sourceDir Path to source
* @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
* @param bool $hidden Whether to include hidden files (files beginning with a period)
* @param bool $includeDir Whether to include directories
*/
function get_filenames(string $sourceDir, ?bool $includePath = false, bool $hidden = false): array
{
function get_filenames(
string $sourceDir,
?bool $includePath = false,
bool $hidden = false,
bool $includeDir = true
): array {
$files = [];

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

if ($includePath === false) {
$files[] = $basename;
} elseif ($includePath === null) {
$files[] = str_replace($sourceDir, '', $name);
} else {
$files[] = $name;
if ($includeDir || ! $object->isDir()) {
if ($includePath === false) {
$files[] = $basename;
} elseif ($includePath === null) {
$files[] = str_replace($sourceDir, '', $name);
} else {
$files[] = $name;
}
}
}
} catch (Throwable $e) {
Expand Down
16 changes: 16 additions & 0 deletions tests/system/Helpers/FilesystemHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,22 @@ public function testGetFilenames()
$this->assertSame($expected, get_filenames($vfs->url(), false));
}

public function testGetFilenamesWithoutDirectories()
{
$vfs = vfsStream::setup('root', null, $this->structure);

$filenames = get_filenames($vfs->url(), true, false, false);

$expected = [
'vfs://root/boo/far',
'vfs://root/boo/faz',
'vfs://root/foo/bar',
'vfs://root/foo/baz',
'vfs://root/simpleFile',
];
$this->assertSame($expected, $filenames);
}

public function testGetFilenamesWithHidden()
{
$this->assertTrue(function_exists('get_filenames'));
Expand Down
1 change: 1 addition & 0 deletions user_guide_src/source/changelogs/v4.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Enhancements
- 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.
- 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>"
- Added support for webp files to **app/Config/Mimes.php**.
- Added 4th parameter ``$includeDir`` to ``get_filenames()``. See :php:func:`get_filenames`.

Changes
*******
Expand Down
7 changes: 4 additions & 3 deletions user_guide_src/source/helpers/filesystem_helper.rst
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,12 @@ The following functions are available:

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

.. php:function:: get_filenames($source_dir[, $include_path = false])
.. php:function:: get_filenames($sourceDir[, $includePath = false[, $hidden = false[, $includeDir = true]]])

:param string $source_dir: Directory path
: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
:param string $sourceDir: Directory path
: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
:param bool $hidden: Whether to include hidden files (files beginning with a period)
:param bool $includeDir: Whether to include directories in the array output
:returns: An array of file names
:rtype: array

Expand Down