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

[5.5] Filesystem files() to return similar structured arrays as allFiles() #18874

Merged
merged 2 commits into from
Apr 24, 2017
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
3 changes: 3 additions & 0 deletions CHANGELOG-5.5.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@
### Events
- ⚠️ Removed calling queue method on handlers ([0360cb1](https://github.com/laravel/framework/commit/0360cb1c6b71ec89d406517b19d1508511e98fb5), [ec96979](https://github.com/laravel/framework/commit/ec969797878f2c731034455af2397110732d14c4), [d9be4bf](https://github.com/laravel/framework/commit/d9be4bfe0367a8e07eed4931bdabf135292abb1b))

### Filesystem
- ⚠️ Made `Storage::files()` work like `Storage::allFiles()` ([#18874](https://github.com/laravel/framework/pull/18874))

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

love it 👍

### Helpers
- Added `throw_if()` and `throw_unless()` helpers ([18bb4df](https://github.com/laravel/framework/commit/18bb4dfc77c7c289e9b40c4096816ebeff1cd843))
- Added `dispatch_now()` helper function ([#18668](https://github.com/laravel/framework/pull/18668), [61f2e7b](https://github.com/laravel/framework/commit/61f2e7b4106f8eb0b79603d9792426f7c6a6d273))
Expand Down
28 changes: 12 additions & 16 deletions src/Illuminate/Filesystem/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -370,34 +370,30 @@ public function glob($pattern, $flags = 0)
* Get an array of all files in a directory.
*
* @param string $directory
* @param bool $ignoreDotFiles
* @return array
*/
public function files($directory)
public function files($directory, $ignoreDotFiles = false)
{
$glob = glob($directory.'/*');

if ($glob === false) {
return [];
}

// To get the appropriate files, we'll simply glob the directory and filter
// out any "files" that are not truly files so we do not end up with any
// directories in our list, but only true files within the directory.
return array_filter($glob, function ($file) {
return filetype($file) == 'file';
});
return iterator_to_array(
Finder::create()->files()->ignoreDotFiles(! $ignoreDotFiles)->in($directory)->depth(0),
false
);
}

/**
* Get all of the files from the given directory (recursive).
*
* @param string $directory
* @param bool $hidden
* @param bool $ignoreDotFiles
* @return array
*/
public function allFiles($directory, $hidden = false)
public function allFiles($directory, $ignoreDotFiles = false)
{
return iterator_to_array(Finder::create()->files()->ignoreDotFiles(! $hidden)->in($directory), false);
return iterator_to_array(
Finder::create()->files()->ignoreDotFiles(! $ignoreDotFiles)->in($directory),
false
);
}

/**
Expand Down
24 changes: 24 additions & 0 deletions tests/Filesystem/FilesystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -414,4 +414,28 @@ public function testIsFileChecksFilesProperly()
$this->assertTrue($filesystem->isFile($this->tempDir.'/foo/foo.txt'));
$this->assertFalse($filesystem->isFile($this->tempDir.'./foo'));
}

public function testFilesMethodReturnsFileInfoObjects()
{
mkdir($this->tempDir.'/foo');
file_put_contents($this->tempDir.'/foo/1.txt', '1');
file_put_contents($this->tempDir.'/foo/2.txt', '2');
mkdir($this->tempDir.'/foo/bar');
$files = new Filesystem();
foreach ($files->files($this->tempDir.'/foo') as $file) {
$this->assertInstanceOf(\SplFileInfo::class, $file);
}
unset($files);
}

public function testAllFilesReturnsFileInfoObjects()
{
file_put_contents($this->tempDir.'/foo.txt', 'foo');
file_put_contents($this->tempDir.'/bar.txt', 'bar');
$files = new Filesystem();
$allFiles = [];
foreach ($files->allFiles($this->tempDir) as $file) {
$this->assertInstanceOf(\SplFileInfo::class, $file);
}
}
}