本文整理汇总了PHP中SplFileObject::fseek方法的典型用法代码示例。如果您正苦于以下问题:PHP SplFileObject::fseek方法的具体用法?PHP SplFileObject::fseek怎么用?PHP SplFileObject::fseek使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SplFileObject
的用法示例。
在下文中一共展示了SplFileObject::fseek方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getFile
/**
* Get an SplFileObject representing the file to be read.
*
* @return \SplFileObject The log file object.
*/
protected function getFile()
{
if (!isset($this->file)) {
$this->file = new \SplFileObject($this->path, 'r');
$this->file->fseek(0, SEEK_END);
$this->cursor = $this->file->ftell();
}
return $this->file;
}
示例2: getTotal
/**
* @return int
*/
public function getTotal()
{
if ($this->total === null) {
$this->csvFile->fseek(0);
$lines = 0;
while (!$this->csvFile->eof()) {
$lines += substr_count($this->csvFile->fread(8192), $this->lineSeparator);
}
$this->total = (int) $lines;
}
return $this->total;
}
示例3: getFile
/**
* @inheritdoc
*/
public function getFile()
{
if ($this->file === null) {
try {
$this->file = new TempFile();
$this->file->fwrite($this->data);
$this->file->fseek(0);
} catch (\RuntimeException $e) {
throw new TransportException($e->getMessage(), null, $e);
}
}
return $this->file;
}
示例4: readSnapshotEvent
/**
* Reads the latest snapshot of the given aggregate identifier.
*
* @param string $type the aggregate's type
* @param string $identifier the aggregate's identifier
* @return SerializedDomainEventDataInterface The latest snapshot of the given aggregate identifier
*
* @throws EventStoreException when reading the <code>snapshotEventFile</code> or reading the <code>eventFile</code> failed
*/
public function readSnapshotEvent($type, $identifier)
{
$snapshotEvent = null;
$fileSystemSnapshotEvent = $this->readLastSnapshotEntry();
if (null !== $fileSystemSnapshotEvent) {
$this->eventFile->fseek($fileSystemSnapshotEvent['bytesToSkip']);
$actuallySkipped = $this->eventFile->ftell();
if ($actuallySkipped !== $fileSystemSnapshotEvent['bytesToSkip']) {
throw new EventStoreException(sprintf("The skip operation did not actually skip the expected amount of bytes. " . "The event log of aggregate of type %s and identifier %s might be corrupt.", $type, $identifier));
}
$snapshotEvent = $fileSystemSnapshotEvent['snapshotEvent'];
}
return $snapshotEvent;
}
示例5: _openFile
/**
* Open the file and optionally restore the position
*
* @return void
*/
private function _openFile()
{
$this->_fieldMap = array();
$this->_fieldMapCount = 0;
if (!file_exists($this->_filename)) {
$this->_file = false;
return;
}
try {
$this->_file = new \SplFileObject($this->_filename, 'r');
$firstline = trim(\MUtil_Encoding::removeBOM($this->_file->current(), "\r\n"));
if ($firstline) {
$this->_fieldMap = call_user_func($this->_splitFunction, $firstline);
$this->_fieldMapCount = count($this->_fieldMap);
// Check for fields, do not run when empty
if (0 === $this->_fieldMapCount) {
$this->_file = false;
return;
}
}
// Restore old file position if any
if (null !== $this->_filepos) {
$this->_file->fseek($this->_filepos, SEEK_SET);
}
// Always move to next, even if there was no first line
$this->next();
} catch (\Exception $e) {
$this->_file = false;
}
}
示例6: insertOne
/**
* Adds a single line to a CSV document
*
* @param string[]|string $row a string, an array or an object implementing to '__toString' method
*
* @return static
*/
public function insertOne($row)
{
if (!is_array($row)) {
$row = str_getcsv($row, $this->delimiter, $this->enclosure, $this->escape);
}
$row = $this->formatRow($row);
$this->validateRow($row);
if (is_null($this->csv)) {
$this->csv = $this->getIterator();
}
$this->csv->fputcsv($row, $this->delimiter, $this->enclosure);
if ("\n" !== $this->newline) {
$this->csv->fseek(-1, SEEK_CUR);
$this->csv->fwrite($this->newline);
}
return $this;
}
示例7: modifyFileLine
function modifyFileLine($filename, $linenum, $lineText)
{
$fp = new SplFileObject($filename);
$fp->seek($linenum);
$line = $fp->current();
$fp->fseek($linenum, SEEK_CUR);
$fp->fwrite($lineText);
}
示例8: putCSVRecord
/**
* フィールドの配列をCSVの行として書き出します。
* @param \SplFileObject $file
* @param string[] $fields
*/
protected function putCSVRecord(\SplFileObject $file, array $fields)
{
$file->fputcsv(array_map(function (string $field) : string {
return preg_replace('/[\\x00-\\x09\\x11\\x7F]+/u', '', strtr($field, ["\r\n" => "\r\n", "\r" => "\r\n", "\n" => "\r\n"]));
}, $fields));
$file->fseek(-1, SEEK_CUR);
$file->fwrite("\r\n");
}
示例9: rewind
/**
* @param bool $skipColumns
*
* @return void
*/
public function rewind($skipColumns = true)
{
$this->csvFile->fseek(0);
if ($skipColumns) {
$this->getFile()->fseek($this->getCsvMeta()->getColumnsOffset());
return;
}
}
示例10: content
private function content(SplFileObject $file)
{
$result = "";
$file->fseek(0);
while (!$file->eof()) {
$result .= $file->fread(1024);
}
return $result;
}
示例11: getCoverageObject
/**
* Returns coverage object from file.
*
* @param \SplFileObject $coverageFile Coverage file.
*
* @return \PHP_CodeCoverage|CodeCoverage
*/
private function getCoverageObject(\SplFileObject $coverageFile)
{
if ('<?php' === $coverageFile->fread(5)) {
return include $coverageFile->getRealPath();
}
$coverageFile->fseek(0);
// the PHPUnit 3.x and below
return unserialize($coverageFile->fread($coverageFile->getSize()));
}
示例12: testWriteMiddleNewLine
/**
* @covers TextFile\Writer\ErasingWriter::write
*/
public function testWriteMiddleNewLine()
{
$prependingWriter = new PrependingWriter();
$filePath = $this->createTestFileFromFixtures('complex_singleline_file.txt');
$file = new \SplFileObject($filePath, 'r+');
$file->fseek(5);
$prependingWriter->write($file, 'test', true);
$file->rewind();
$this->assertEquals('firsttest', trim($file->current()));
$file->next();
$this->assertEquals('secondthirdfourthfifth', trim($file->current()));
}
示例13: getMetadata
/**
* Retrieves the Metadata for a Key/Value pair
*
* Optionally allows a specific position offset in the file
* Return Array
* - klen: The length of the Key
* - vlen: The length of the Value
* - length: The total combined length of the Key and the Value
*
* @param integer $position An offset to seek to in a file
*
* @return array|bool
*/
protected function getMetadata($position = null)
{
if (!is_null($position)) {
$this->file->fseek($position);
}
$metadata = $this->file->fread(8);
if ($metadata) {
list(, $klen, $vlen) = unpack('N*', $metadata);
return (object) ['klen' => $klen, 'vlen' => $vlen, 'length' => $klen + $vlen];
}
return false;
}
示例14: merge
/**
* This method merges the data in another array with this array.
*
* @access public
*/
public function merge()
{
$arrays = func_get_args();
$this->writer->fseek($this->writer->ftell());
foreach ($arrays as $values) {
if (is_array($values)) {
foreach ($values as $value) {
$this->writer->fwrite(serialize($value) . PHP_EOL);
}
}
$this->length += count($values);
}
}
示例15: write
/**
* {@inheritdoc}
*/
public function write(\SplFileObject $file, $text, $newLineAtEnd = false)
{
$originalSeek = $file->ftell();
// Refresh file size
clearstatcache($file->getFilename());
if ($file->getSize() - $file->ftell() > 0) {
$contentAfter = $file->fread($file->getSize() - $file->ftell());
} else {
$contentAfter = '';
}
$file->fseek($originalSeek);
$file->fwrite($text . ($newLineAtEnd ? PHP_EOL : ''));
$file->fwrite($contentAfter);
return $file;
}