Custom stream wrapper support
Added support for custom stream wrappers, such as "fly" or "s3". Support is determined per reader.
This commit is contained in:
1 parent
0c90d102ef
commit
d2ac54c578
10 files changed
+333
-5
No files matched your search
@@ -423,4 +423,50 @@ class ReaderTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertEquals($expectedRows, $allRows);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testReadCustomStreamWrapper()
|
||||
{
|
||||
$allRows = [];
|
||||
$resourcePath = 'spout://csv_standard';
|
||||
|
||||
// register stream wrapper
|
||||
stream_wrapper_register('spout', SpoutTestStream::CLASS_NAME);
|
||||
|
||||
/** @var \Box\Spout\Reader\CSV\Reader $reader */
|
||||
$reader = ReaderFactory::create(Type::CSV);
|
||||
$reader->open($resourcePath);
|
||||
|
||||
foreach ($reader->getSheetIterator() as $sheet) {
|
||||
foreach ($sheet->getRowIterator() as $row) {
|
||||
$allRows[] = $row;
|
||||
}
|
||||
}
|
||||
|
||||
$reader->close();
|
||||
|
||||
$expectedRows = [
|
||||
['csv--11', 'csv--12', 'csv--13'],
|
||||
['csv--21', 'csv--22', 'csv--23'],
|
||||
['csv--31', 'csv--32', 'csv--33'],
|
||||
];
|
||||
$this->assertEquals($expectedRows, $allRows);
|
||||
|
||||
// cleanup
|
||||
stream_wrapper_unregister('spout');
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Box\Spout\Common\Exception\IOException
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testReadWithUnsupportedCustomStreamWrapper()
|
||||
{
|
||||
/** @var \Box\Spout\Reader\CSV\Reader $reader */
|
||||
$reader = ReaderFactory::create(Type::CSV);
|
||||
$reader->open('unsupported://foobar');
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
<?php
|
||||
|
||||
namespace Box\Spout\Reader\CSV;
|
||||
|
||||
/**
|
||||
* Class SpoutTestStream
|
||||
* Custom stream that reads CSV files located in the tests/resources/csv folder.
|
||||
* For example: spout://foobar will point to tests/resources/csv/foobar.csv
|
||||
*
|
||||
* @package Box\Spout\Reader\CSV
|
||||
*/
|
||||
class SpoutTestStream
|
||||
{
|
||||
const CLASS_NAME = __CLASS__;
|
||||
|
||||
const PATH_TO_CSV_RESOURCES = 'tests/resources/csv/';
|
||||
const CSV_EXTENSION = '.csv';
|
||||
|
||||
/** @var int $position */
|
||||
private $position;
|
||||
|
||||
/** @var resource $fileHandle */
|
||||
private $fileHandle;
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
* @param int $flag
|
||||
* @return array
|
||||
*/
|
||||
public function url_stat($path, $flag)
|
||||
{
|
||||
$filePath = $this->getFilePathFromStreamPath($path);
|
||||
return stat($filePath);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $streamPath
|
||||
* @return string
|
||||
*/
|
||||
private function getFilePathFromStreamPath($streamPath)
|
||||
{
|
||||
$fileName = parse_url($streamPath, PHP_URL_HOST);
|
||||
return self::PATH_TO_CSV_RESOURCES . $fileName . self::CSV_EXTENSION;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
* @param string $mode
|
||||
* @param int $options
|
||||
* @param string $opened_path
|
||||
* @return bool
|
||||
*/
|
||||
public function stream_open($path, $mode, $options, &$opened_path)
|
||||
{
|
||||
$this->position = 0;
|
||||
|
||||
// the path is like "spout://csv_name" so the actual file name correspond the name of the host.
|
||||
$filePath = $this->getFilePathFromStreamPath($path);
|
||||
$this->fileHandle = fopen($filePath, $mode);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $numBytes
|
||||
* @return string
|
||||
*/
|
||||
public function stream_read($numBytes)
|
||||
{
|
||||
$this->position += $numBytes;
|
||||
return fread($this->fileHandle, $numBytes);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function stream_tell()
|
||||
{
|
||||
return $this->position;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $offset
|
||||
* @param int|void $whence
|
||||
* @return bool
|
||||
*/
|
||||
public function stream_seek($offset, $whence = SEEK_SET)
|
||||
{
|
||||
$result = fseek($this->fileHandle, $offset, $whence);
|
||||
if ($result === -1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($whence === SEEK_SET) {
|
||||
$this->position = $offset;
|
||||
} else if ($whence === SEEK_CUR) {
|
||||
$this->position += $offset;
|
||||
} else {
|
||||
// not implemented
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function stream_close()
|
||||
{
|
||||
return fclose($this->fileHandle);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function stream_eof()
|
||||
{
|
||||
return feof($this->fileHandle);
|
||||
}
|
||||
}
|
||||
@@ -363,6 +363,30 @@ class ReaderTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertEquals($expectedRows, $allRows);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Box\Spout\Common\Exception\IOException
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testReadWithUnsupportedCustomStreamWrapper()
|
||||
{
|
||||
/** @var \Box\Spout\Reader\ODS\Reader $reader */
|
||||
$reader = ReaderFactory::create(Type::ODS);
|
||||
$reader->open('unsupported://foobar');
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Box\Spout\Common\Exception\IOException
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testReadWithSupportedCustomStreamWrapper()
|
||||
{
|
||||
/** @var \Box\Spout\Reader\ODS\Reader $reader */
|
||||
$reader = ReaderFactory::create(Type::ODS);
|
||||
$reader->open('php://memory');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $fileName
|
||||
* @return array All the read rows the given file
|
||||
|
||||
@@ -404,6 +404,30 @@ class ReaderTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertEquals($expectedRows, $allRows);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Box\Spout\Common\Exception\IOException
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testReadWithUnsupportedCustomStreamWrapper()
|
||||
{
|
||||
/** @var \Box\Spout\Reader\XLSX\Reader $reader */
|
||||
$reader = ReaderFactory::create(Type::XLSX);
|
||||
$reader->open('unsupported://foobar');
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Box\Spout\Common\Exception\IOException
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testReadWithSupportedCustomStreamWrapper()
|
||||
{
|
||||
/** @var \Box\Spout\Reader\XLSX\Reader $reader */
|
||||
$reader = ReaderFactory::create(Type::XLSX);
|
||||
$reader->open('php://memory');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $fileName
|
||||
* @return array All the read rows the given file
|
||||
|
||||
Reference in new issue
Block a user