/** * File constructor. * @param $uri String File path or URL * @throws \ErrorException * @throws \Exception */ public function __construct($uri) { try { $this->messageFactory = new MessageFactory(); $file = new \SplFileObject($uri, 'rb'); while ($file->valid()) { $raw_time = $file->fread(8); if (!$raw_time) { break; } $header = new Header($file->fread(6)); if ($header->magic !== 0xfe) { throw new \ErrorException("Unexpected magic number ({$header->magic})"); } $payload = $file->fread($header->length); $raw_crc = $file->fread(2); $entry = new Entry($raw_time, $header, $payload, $raw_crc); $this->entries[] = $entry; $this->messages[] = $this->messageFactory->build($entry); } if (!$this->hasEntries()) { throw new \ErrorException('No entries found in file.'); } $this->startTime = $this->entries[0]->time; $this->endTime = end($this->entries)->time; } catch (\Exception $ex) { // echo $ex->getMessage() . ' ' . $ex->getFile() . ':' . $ex->getLine(); die; } }
/** * 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())); }
/** * コンストラクタ * 副作用が大量にあるので注意 */ public function __construct(ClientInterface $client, $filename = 'stamp.json', $span = 0, $mark_limit = 10000, $back_limit = 3600) { // ヘッダの送出 if (!headers_sent()) { header('Content-Type: text/html; charset=UTF-8'); } // エラー表示の設定 error_reporting(-1); ini_set('log_errors', PHP_SAPI === 'cli'); ini_set('display_errors', PHP_SAPI !== 'cli'); // 重複起動防止 $file = new \SplFileObject($filename, 'a+b'); if (!$file->flock(LOCK_EX | LOCK_NB)) { throw new \RuntimeException('Failed to lock file.'); } // JSONとして保存してあるデータを取得 clearstatcache(); $json = $file->getSize() > 0 ? json_decode($file->fread($file->getSize()), true) : []; // JSONに前回実行時刻が保存されていた時 if (isset($json['prev'])) { // 十分に時間が空いたかどうかをチェック if (!static::expired($json['prev'], $span)) { throw new \RuntimeException('Execution span is not enough.'); } } // JSONにマーク済みステータス一覧が記録されていたとき復元する if (isset($json['marked'])) { $this->marked = array_map('filter_var', (array) $json['marked']); } $this->client = $client; $this->file = $file; $this->prev = (new \DateTimeImmutable('now', new \DateTimeZone('UTC')))->format('r'); $this->mark_limit = $mark_limit; $this->back_limit = $back_limit; }
private function content(SplFileObject $file) { $result = ""; $file->fseek(0); while (!$file->eof()) { $result .= $file->fread(1024); } return $result; }
private function readComplexRSS() { $filename = $this->createComplexRSS(); $file = new \SplFileObject($filename, "r"); $contentToTest = $file->fread($file->getSize()); $filename = 'tests/RSS/ComplexRSS.rss'; $file = new \SplFileObject($filename, "r"); $content = $file->fread($file->getSize()); return array($contentToTest, $content); }
/** * @param integer $length * @return mixed */ public function read($length) { if (version_compare(phpversion(), '5.5.11', '<')) { trigger_error(sprintf('%s is not supported on PHP %s < 5.5.11.', __METHOD__, phpversion()), E_USER_WARNING); $buffer = ''; while (strlen($buffer) < $length && !$this->eof()) { $buffer .= $this->fgets(); } return $buffer; } return parent::fread($length); }
/** * 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; }
/** * @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; }
/** * Read all classes from path * * @return array $classes */ protected function readClasses() { $classes = array(); $directory = new RecursiveDirectoryIterator($this->pathTop, RecursiveDirectoryIterator::SKIP_DOTS); $fileIterator = new RecursiveIteratorIterator($directory, RecursiveIteratorIterator::LEAVES_ONLY); foreach ($fileIterator as $file) { $fileClass = new SplFileObject($file->getPathname()); $fileExtension = new SplFileInfo($file->getPathname()); if ($fileExtension->getExtension() == $this->fileExt) { $classes[] = $fileClass->fread($fileClass->getSize()); } } return $classes; }
/** * {@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; }
/** * It skips the file headers * @return boolean * @access private * @final */ private final function skipHead() { if ($this->fileObject->getSize() < 0) { return false; } $this->fileObject->rewind(); $this->fileObject->fseek(7, SEEK_CUR); $mainHead = $this->fileObject->fread(7); if (ord($mainHead[2]) != 0x73) { return false; } $headSize = $this->getBytes($mainHead, 5, 2); $this->fileObject->fseek($headSize - 7, SEEK_CUR); unset($mainHead, $headSize); return true; }
public function finalizeFile(\SplFileObject $fileObject) { if ($this->headers !== null) { // Create tmp file with header $fd = fopen('php://temp', 'w+b'); fputcsv($fd, $this->headers); // Copy file content into tmp file $fileObject->rewind(); fwrite($fd, $fileObject->fread($fileObject->getSize())); // Overwrite file content with tmp content rewind($fd); $fileObject->rewind(); $fileObject->fwrite(stream_get_contents($fd)); clearstatcache(true, $fileObject->getPathname()); fclose($fd); } // Remove last line feed $fileObject->ftruncate($fileObject->getSize() - 1); }
/** * @inheritDoc */ public function fread($length) { $buffer = parent::fread($length); if ($this->eof()) { return $buffer; } $byte = ord(substr($buffer, -1, 1)); if ($byte <= 0x7f) { return $this->fixLength($buffer, $length); } $count = 1; while ($byte < 0xc0 && $count < $length) { $count++; $byte = ord(substr($buffer, -$count, 1)); } $mask = 0x40; $bitsCount = 1; while ($byte & $mask) { $mask = $mask >> 1; $bitsCount++; } $bytesToEnd = $bitsCount - $count; return $this->fixLength($buffer . ($bytesToEnd ? parent::fread($bytesToEnd) : ''), $length); }
<?php $obj = new SplFileObject(__FILE__, 'r'); $data = $obj->fread(5); var_dump($data); $data = $obj->fread(); var_dump($data); $data = $obj->fread(0); var_dump($data); // read more data than is available $data = $obj->fread(filesize(__FILE__) + 32); var_dump(strlen($data) === filesize(__FILE__) - 5);
/** * @todo fix mixed return types! * @param integer $length * @return string|boolean */ public function read(int $length) { return parent::fread($length); }
/** * Unserializes the object from JSON contained in the given file. * * @param \SplFileObject $file * The file to be read from. Since it may also be written to, it should * be opened with mode `c+`. * * @return DivideIQ * The unserialized object. */ public static function fromFile(\SplFileObject $file) { $file->rewind(); $object = static::fromJson($file->fread($file->getSize())); $object->file = $file; return $object; }
<?php $file = new SplFileObject(__FILE__); var_dump($file->fread(5));
/** * Takes the file objec for a history file and seeks to the beginning of the tempData-section. * @param SplFileObject $historyFileObject Uncompiled history-file * @return int The bytesize of the tempData-section*/ private static function seekHistoryFileTempData($historyFileObject) { $historyFileObject->fseek(-4, SEEK_END); $tempDataSize = unpack('N', $historyFileObject->fread(4))[1]; $historyFileObject->fseek(-4 - $tempDataSize, SEEK_END); return $tempDataSize; }