Skip to content

Commit f99fb14

Browse files
committed
Restore flat file return option
1 parent b8d1149 commit f99fb14

3 files changed

Lines changed: 43 additions & 8 deletions

File tree

system/Helpers/filesystem_helper.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,12 +209,12 @@ function delete_files(string $path, bool $del_dir = false, bool $htdocs = false,
209209
* Reads the specified directory and builds an array containing the filenames.
210210
* Any sub-folders contained within the specified path are read as well.
211211
*
212-
* @param string $source_dir Path to source
213-
* @param boolean $include_path Whether to include the path as part of the filename
212+
* @param string $source_dir Path to source
213+
* @param mixed $include_path Whether to include the path as part of the filename; empty for no path, 'relative' for a relative path, not empty for full path
214214
*
215215
* @return array
216216
*/
217-
function get_filenames(string $source_dir, bool $include_path = false): array
217+
function get_filenames(string $source_dir, $include_path = ''): array
218218
{
219219
$files = [];
220220

@@ -228,7 +228,18 @@ function get_filenames(string $source_dir, bool $include_path = false): array
228228
RecursiveIteratorIterator::SELF_FIRST
229229
) as $name => $object)
230230
{
231-
$files[] = $include_path ? $name : str_replace($source_dir, '', $name);
231+
if (empty($include_path))
232+
{
233+
$files[] = pathinfo($name, PATHINFO_BASENAME);
234+
}
235+
elseif ($include_path === 'relative')
236+
{
237+
$files[] = str_replace($source_dir, '', $name);
238+
}
239+
else
240+
{
241+
$files[] = $name;
242+
}
232243
}
233244
}
234245
catch (\Throwable $e)

tests/system/Helpers/FilesystemHelperTest.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,29 @@ public function testDeleteFilesFailure()
178178
//--------------------------------------------------------------------
179179

180180
public function testGetFilenames()
181+
{
182+
$this->assertTrue(function_exists('delete_files'));
183+
184+
// Not sure the directory names should actually show up
185+
// here but this matches v3.x results.
186+
$expected = [
187+
'.hidden',
188+
'AnEmptyFolder',
189+
'bar',
190+
'baz',
191+
'boo',
192+
'far',
193+
'faz',
194+
'foo',
195+
'simpleFile',
196+
];
197+
198+
$vfs = vfsStream::setup('root', null, $this->structure);
199+
200+
$this->assertEquals($expected, get_filenames($vfs->url(), false));
201+
}
202+
203+
public function testGetFilenamesWithRelativeSource()
181204
{
182205
$this->assertTrue(function_exists('get_filenames'));
183206

@@ -195,10 +218,10 @@ public function testGetFilenames()
195218

196219
$vfs = vfsStream::setup('root', null, $this->structure);
197220

198-
$this->assertEquals($expected, get_filenames($vfs->url(), false));
221+
$this->assertEquals($expected, get_filenames($vfs->url(), 'relative'));
199222
}
200223

201-
public function testGetFilenamesWithSource()
224+
public function testGetFilenamesWithFullSource()
202225
{
203226
$this->assertTrue(function_exists('get_filenames'));
204227

user_guide_src/source/helpers/filesystem_helper.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,14 @@ The following functions are available:
148148
.. php:function:: get_filenames($source_dir[, $include_path = FALSE])
149149
150150
:param string $source_dir: Directory path
151-
:param bool $include_path: Whether to include the path as part of the filenames
151+
:param string $include_path: Whether to include the path as part of the filename; empty for no path, 'relative' for a relative path, not empty for full path
152152
:returns: An array of file names
153153
:rtype: array
154154

155155
Takes a server path as input and returns an array containing the names of all files
156156
contained within it. The file path can optionally be added to the file names by setting
157-
the second parameter to TRUE, otherwise file names will be relative to the source.
157+
the second parameter to 'relative' for relative paths or any other non-empty value for
158+
a full file path.
158159

159160
Example::
160161

0 commit comments

Comments
 (0)