本文整理汇总了PHP中SplFileObject::seek方法的典型用法代码示例。如果您正苦于以下问题:PHP SplFileObject::seek方法的具体用法?PHP SplFileObject::seek怎么用?PHP SplFileObject::seek使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SplFileObject
的用法示例。
在下文中一共展示了SplFileObject::seek方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getCode
/**
* Get Code from file
* @param $path
* @param $line
* @param $numLines
* @return array|null
*/
private function getCode($path, $line, $numLines)
{
if (empty($path) || empty($line) || !file_exists($path)) {
return NULL;
}
try {
// Get the number of lines in the file
$file = new \SplFileObject($path);
$file->seek(PHP_INT_MAX);
$totalLines = $file->key() + 1;
// Work out which lines we should fetch
$start = max($line - floor($numLines / 2), 1);
$end = $start + ($numLines - 1);
if ($end > $totalLines) {
$end = $totalLines;
$start = max($end - ($numLines - 1), 1);
}
// Get the code for this range
$code = array();
$file->seek($start - 1);
while ($file->key() < $end) {
$code[$file->key() + 1] = rtrim($file->current());
$file->next();
}
return $code;
} catch (RuntimeException $ex) {
return null;
}
}
示例2: offsetGet
/**
* {@inheritdoc}
*/
public function offsetGet($offset)
{
$key = $this->file->key();
$this->file->seek($offset);
$log = $this->current();
$this->file->seek($key);
$this->file->current();
return $log;
}
示例3: countLines
/**
* {@inheritdoc}
*/
public function countLines(\SplFileObject $file)
{
// Refresh file size
clearstatcache($file->getFilename());
$previous = $file->key();
$file->seek($file->getSize());
$count = $file->key();
$file->seek($previous);
return $count;
}
示例4: read
/**
* {@inheritdoc}
*/
public function read()
{
if ($this->file->valid()) {
$this->file->seek($this->getContext()->getReadCount());
$line = rtrim($this->file->current(), PHP_EOL);
$this->getContext()->incrementReadCount();
return json_decode($line, true);
}
return null;
}
示例5: execute
protected function execute(InputInterface $input, OutputInterface $output)
{
if (parent::execute($input, $output)) {
$this->loadOCConfig();
$properties = array('string $id', 'string $template', 'array $children', 'array $data', 'string $output');
// TODO: get all library classes as well, maybe extract from registry somehow...
$searchLine = "abstract class Controller {";
$pathToController = "engine/controller.php";
$catalogModels = $this->getModels(\DIR_APPLICATION);
$adminModels = $this->getModels(str_ireplace("catalog/", "admin/", \DIR_APPLICATION));
$textToInsert = array_unique(array_merge($properties, $catalogModels, $adminModels));
//get line number where start Controller description
$fp = fopen(\DIR_SYSTEM . $pathToController, 'r');
$lineNumber = $this->getLineOfFile($fp, $searchLine);
fclose($fp);
//regenerate Controller text with properties
$file = new \SplFileObject(\DIR_SYSTEM . $pathToController);
$file->seek($lineNumber);
$tempFile = sprintf("<?php %s \t/**%s", PHP_EOL, PHP_EOL);
foreach ($textToInsert as $val) {
$tempFile .= sprintf("\t* @property %s%s", $val, PHP_EOL);
}
$tempFile .= sprintf("\t**/%s%s%s", PHP_EOL, $searchLine, PHP_EOL);
while (!$file->eof()) {
$tempFile .= $file->fgets();
}
//write Controller
$fp = fopen(\DIR_SYSTEM . $pathToController, 'w');
fwrite($fp, $tempFile);
fclose($fp);
}
}
示例6: readFileByLines
/**
* 返回文件从X行到Y行的内容(支持php5、php4)
* @param String $filename 文件名
* @param integer $startLine 开始行
* @param integer $endLine 结束行
* @param string $method 方法
* @return array() 返回数组
*/
function readFileByLines($filename, $startLine = 1, $endLine = 50, $method = 'rb')
{
$content = array();
$count = $endLine - $startLine;
// 判断php版本(因为要用到SplFileObject,PHP>=5.1.0)
if (version_compare(PHP_VERSION, '5.1.0', '>=')) {
$fp = new SplFileObject($filename, $method);
// 转到第N行, seek方法参数从0开始计数
$fp->seek($startLine - 1);
for ($i = 0; $i <= $count; ++$i) {
// current()获取当前行内容
$content[] = $fp->current();
// 下一行
$fp->next();
}
} else {
//PHP<5.1
$fp = fopen($filename, $method);
if (!$fp) {
return 'error:can not read file';
}
// 跳过前$startLine行
for ($i = 1; $i < $startLine; ++$i) {
fgets($fp);
}
// 读取文件行内容
for ($i; $i <= $endLine; ++$i) {
$content[] = fgets($fp);
}
fclose($fp);
}
// array_filter过滤:false,null,''
return array_filter($content);
}
示例7: setBodyFromReflection
/**
* @param \ReflectionFunctionAbstract $reflection
*/
public function setBodyFromReflection(\ReflectionFunctionAbstract $reflection)
{
/** @var $reflection \ReflectionMethod */
if (is_a($reflection, '\\ReflectionMethod') && $reflection->isAbstract()) {
$this->_code = null;
return;
}
$file = new \SplFileObject($reflection->getFileName());
$file->seek($reflection->getStartLine() - 1);
$code = '';
while ($file->key() < $reflection->getEndLine()) {
$code .= $file->current();
$file->next();
}
$begin = strpos($code, 'function');
$code = substr($code, $begin);
$begin = strpos($code, '{');
$end = strrpos($code, '}');
$code = substr($code, $begin + 1, $end - $begin - 1);
$code = preg_replace('/^\\s*[\\r\\n]+/', '', $code);
$code = preg_replace('/[\\r\\n]+\\s*$/', '', $code);
if (!trim($code)) {
$code = null;
}
$this->setCode($code);
}
示例8: export
protected function export($var, $return = false)
{
if ($var instanceof Closure) {
/* dump anonymous function in to plain code.*/
$ref = new ReflectionFunction($var);
$file = new SplFileObject($ref->getFileName());
$file->seek($ref->getStartLine() - 1);
$result = '';
while ($file->key() < $ref->getEndLine()) {
$result .= $file->current();
$file->next();
}
$begin = strpos($result, 'function');
$end = strrpos($result, '}');
$result = substr($result, $begin, $end - $begin + 1);
} elseif (is_object($var)) {
/* dump object with construct function. */
$result = 'new ' . get_class($var) . '(' . $this->export(get_object_vars($var), true) . ')';
} elseif (is_array($var)) {
/* dump array in plain array.*/
$array = array();
foreach ($var as $k => $v) {
$array[] = var_export($k, true) . ' => ' . $this->export($v, true);
}
$result = 'array(' . implode(', ', $array) . ')';
} else {
$result = var_export($var, true);
}
if (!$return) {
print $result;
}
return $result;
}
示例9: modifyFileLine
function modifyFileLine($filename, $linenum, $lineText)
{
$fp = new SplFileObject($filename);
$fp->seek($linenum);
$line = $fp->current();
$fp->fseek($linenum, SEEK_CUR);
$fp->fwrite($lineText);
}
示例10: assertSameContentAtLine
/**
* @param string $expectedContent
* @param integer $zeroBasedLineNumber
* @param boolean $trimLine
* @return void
*/
private function assertSameContentAtLine($expectedContent, $zeroBasedLineNumber, $trimLine = true)
{
$this->fileObject->seek($zeroBasedLineNumber);
$actualContent = $this->fileObject->current();
if ($trimLine) {
$actualContent = trim($actualContent);
}
$this->assertSame($expectedContent, $actualContent);
}
示例11: actionIndex
public function actionIndex()
{
Yii::import("application.models.*");
//get the latest idle cron job
/* @var $latestidle JobQueue*/
$latestidle = JobQueue::model()->findByAttributes(array("status" => JobQueue::$JOBQUEUE_STATUS_IDLE));
if (!$latestidle) {
echo "No file queued";
die;
}
//set status to on-going
$latestidle->status = JobQueue::$JOBQUEUE_STATUS_ON_GOING;
$latestidle->save(false);
//retrieve file
$queueFile = new SplFileObject($latestidle->filename);
//read file
$queueFile->setFlags(SplFileObject::READ_CSV | SplFileObject::READ_AHEAD | SplFileObject::DROP_NEW_LINE | SplFileObject::SKIP_EMPTY);
$queueFile->next();
// Total_records ,
$queueFile->seek(PHP_INT_MAX);
$linesTotal = $queueFile->key();
$latestidle->total_records = $linesTotal;
$latestidle->save(false);
$index = 0;
foreach ($queueFile as $currentLine) {
//iterate content
if ($queueFile->key() === 0) {
continue;
}
//TODO: processed_record
$latestidle->processed_record = ++$index;
$latestidle->save(false);
$currentMobile = $currentLine[0];
$newBlackListedmobile = new BlackListedMobile();
//cleaning time
$currentMobile = trim($currentMobile);
$currentMobile = rtrim($currentMobile);
$currentMobile = ltrim($currentMobile);
$newBlackListedmobile->mobile_number = $currentMobile;
$newBlackListedmobile->queue_id = $latestidle->queue_id;
//set queueid
if ($newBlackListedmobile->save()) {
//save content
echo "{$newBlackListedmobile->mobile_number} : Saved \n";
} else {
echo "{$newBlackListedmobile->mobile_number} : Failed \n";
}
}
//when done
//set status to done
$latestidle->status = JobQueue::$JOBQUEUE_STATUS_DONE;
$latestidle->date_done = date("Y-m-d H:i:s");
//set done datetime to now()
$latestidle->save();
echo "Queue DONE \n";
}
示例12: getValue
/**
* This method returns the element at the the specified index.
*
* @access public
* @param integer $index the index of the element
* @return mixed the element at the specified index
* @throws Throwable\InvalidArgument\Exception indicates that an index must be an integer
* @throws Throwable\OutOfBounds\Exception indicates that the index is out of bounds
*/
public function getValue($index)
{
if (!$this->hasIndex($index)) {
throw new Throwable\OutOfBounds\Exception('Unable to get element. Undefined index at ":index" specified', array(':index' => $index));
}
$this->reader->seek($index);
$line = $this->reader->current();
$value = unserialize($line);
return $value;
}
示例13: read
public function read($id)
{
$file = new \SplFileObject($this->filename, "r");
if ($file->flock(LOCK_EX)) {
$file->seek($id);
$data = $file->current();
$file->flock(LOCK_UN);
} else {
return false;
}
return $this->mapStringDataToObject($data);
}
示例14: getContents
/**
* @param $fileName
* @param $page
* @param $rows
* @param string $methord
* @return array
*/
public static function getContents($fileName, $page, $rows, $methord = 'rb')
{
$content = array();
$start = ($page - 1) * $rows;
$fp = new \SplFileObject($fileName, $methord);
$fp->seek($start);
for ($i = 0; $i < $rows; ++$i) {
$content[] = $fp->current();
$fp->next();
}
return array_filter($content);
}
示例15: setUpBeforeClass
public static function setUpBeforeClass()
{
$reflection = new ReflectionClass(__NAMESPACE__ . '\\TestClass');
self::$startLine = $reflection->getStartLine();
self::$endLine = $reflection->getEndLine();
self::$testClassCode = NULL;
/* GetCode as String */
$file = new \SplFileObject($reflection->getFileName());
$file->seek(self::$startLine - 1);
while ($file->key() < $reflection->getEndLine()) {
self::$testClassCode .= $file->current();
$file->next();
}
self::$testClassCode = rtrim(self::$testClassCode, "\n");
unset($file);
}