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

[8.x] Add File::lines($path) method #34187

Merged
merged 1 commit into from
Sep 8, 2020
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
29 changes: 29 additions & 0 deletions src/Illuminate/Filesystem/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
use ErrorException;
use FilesystemIterator;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Illuminate\Support\LazyCollection;
use Illuminate\Support\Traits\Macroable;
use RuntimeException;
use SplFileObject;
use Symfony\Component\Filesystem\Filesystem as SymfonyFilesystem;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Mime\MimeTypes;
Expand Down Expand Up @@ -84,6 +86,33 @@ public function sharedGet($path)
return $contents;
}

/**
* Get the contents of a file, one line at a time.
*
* @param string $path
* @return \Illuminate\Support\LazyCollection
*
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
*/
public function lines($path)
JosephSilber marked this conversation as resolved.
Show resolved Hide resolved
{
if (! $this->isFile($path)) {
throw new FileNotFoundException(
"File does not exist at path {$path}."
);
}

return LazyCollection::make(function () use ($path) {
$file = new SplFileObject($path);

$file->setFlags(SplFileObject::DROP_NEW_LINE);

while (! $file->eof()) {
yield $file->fgets();
}
});
}

/**
* Get the returned value of a file.
*
Expand Down
22 changes: 22 additions & 0 deletions tests/Filesystem/FilesystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Filesystem\Filesystem;
use Illuminate\Filesystem\FilesystemManager;
use Illuminate\Foundation\Application;
use Illuminate\Support\LazyCollection;
use Illuminate\Testing\Assert;
use Mockery as m;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -56,6 +57,27 @@ public function testPutStoresFiles()
$this->assertStringEqualsFile(self::$tempDir.'/file.txt', 'Hello World');
}

public function testLines()
{
$path = self::$tempDir.'/file.txt';

$contents = LazyCollection::times(3)
->map(function ($number) {
return "line-{$number}";
})
->join("\n");

file_put_contents($path, $contents);

$files = new Filesystem;
$this->assertInstanceOf(LazyCollection::class, $files->lines($path));

$this->assertSame(
['line-1', 'line-2', 'line-3'],
$files->lines($path)->all()
);
}

public function testReplaceCreatesFile()
{
$tempFile = self::$tempDir.'/file.txt';
Expand Down