nette / utils

🛠 Lightweight utilities for string & array manipulation, image handling, safe JSON encoding/decoding, validation, slug or strong password generating etc.
https://doc.nette.org/utils
Other
1.98k stars 147 forks source link

Missing `file` support in `Utils/Filesystem` #220

Closed zeleznypa closed 1 year ago

zeleznypa commented 4 years ago

Sometimes it is useful to read the file content "line by line".

It is also handy to have all file manipulation methods in one place.

Unfortunately, the Filesystem class is final, so I can't extend it just for my projects.

But I miss the code like:

    /**
     * Reads file content "line by line".
     *
     * @return array<int,string>
     * @throws Nette\IOException
     */
    public static function readLines(string $file): array
    {
        $content = @file($file); // @ is escalated to exception
        if ($content === false) {
            throw new Nette\IOException("Unable to read file '$file'. " . Helpers::getLastError());
        }
        return $content;
    }

Will it be possible please, to add it or change the Filesystem class as non-final?

zeleznypa commented 1 year ago

@dg Thank you very much David. The idea of using yield exceeded my expectations. Great job