Merge pull request #5862 from kenjis/feat-get_filenames-includeDir

feat: add `$includeDir` option to `get_filenames()`
This commit is contained in:
kenjis 2022-04-07 07:50:15 +09:00 committed by GitHub
commit ad9d1ba04f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 11 deletions

View File

@ -194,9 +194,14 @@ if (! function_exists('get_filenames')) {
* @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;
@ -212,12 +217,14 @@ if (! function_exists('get_filenames')) {
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) {

View File

@ -305,6 +305,22 @@ final class FilesystemHelperTest extends CIUnitTestCase
$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'));

View File

@ -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
*******

View File

@ -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