Reading a stream instead of file_get_contents

An import that read a 900 MB CSV into a string, on a worker with a 512 MB memory limit, and had been working because the file used to be small.

// what it was
$rows = array_map('str_getcsv', explode("n", file_get_contents($path)));

// what it is
$handle = fopen($path, 'rb');

try {
    while (($row = fgetcsv($handle)) !== false) {
        yield $row;
    }
} finally {
    fclose($handle);
}

Three copies of the file existed simultaneously in the original — the string, the exploded array and the mapped array — so the real requirement was closer to three gigabytes than to nine hundred megabytes. The generator holds one row. The finally matters more than it looks: a consumer that breaks out of the loop early leaves the generator suspended, and without it the handle stays open until garbage collection.