nozavroni / csvelte

🕺🏻 CSV and Tabular Data library for PHP
http://phpcsv.com/
Other
6 stars 0 forks source link

Implement lazy open stream #121

Closed nozavroni closed 7 years ago

nozavroni commented 7 years ago

Implement a lazy open stream class (or maybe just make it the default). What this means is that the stream resource isn't opened until an I/O operation is performed on it.

$stream = IO\LazyOpenStream('file:///var/www/data/reports.csv', 'r+b');
echo $stream->getResource(); // null
$stream->read(100);
echo $stream->getResource(); // stream resource 
nozavroni commented 7 years ago

I came up with an alternative solution for this... instead of creating an entirely new stream class, I abstracted the concept of a "stream resource" into a class of its own. the Stream class will now use it internally in place of simply using a php stream resource handle. Then the resource object will lazy-open instead of the stream. Let me demonstrate:

$res = (new CSVelte\IO\Resource('./data/products.csv'))
    ->setBaseMode('w')
    ->setIsPlus(true)
    ->setIsBinary(true);
echo $res->isConnected(); // false 
$stream = Stream::open($res);
$stream->getResource()->isConnected(); // false
$stream->write('helloworld');
echo $stream->getResource()->isConnected(); true
nozavroni commented 7 years ago

Finished with the exception of documentation

nozavroni commented 7 years ago

Closing all issues that only need documentation.