本文整理汇总了PHP中SplFileObject::key方法的典型用法代码示例。如果您正苦于以下问题:PHP SplFileObject::key方法的具体用法?PHP SplFileObject::key怎么用?PHP SplFileObject::key使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SplFileObject
的用法示例。
在下文中一共展示了SplFileObject::key方法的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: 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";
}
示例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: test
function test($name)
{
echo "==={$name}===\n";
$o = new SplFileObject(dirname(__FILE__) . '/' . $name);
var_dump($o->key());
while (($c = $o->fgetc()) !== false) {
var_dump($o->key(), $c, $o->eof());
}
echo "===EOF?===\n";
var_dump($o->eof());
var_dump($o->key());
var_dump($o->eof());
}
示例5: 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;
}
示例6: 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);
}
示例7: read
/**
* {@inheritdoc}
*/
public function read()
{
if (null === $this->csv) {
if (mime_content_type($this->filePath) === 'application/zip') {
$this->extractZipArchive();
}
$this->csv = new \SplFileObject($this->filePath);
$this->csv->setFlags(\SplFileObject::READ_CSV | \SplFileObject::READ_AHEAD | \SplFileObject::SKIP_EMPTY | \SplFileObject::DROP_NEW_LINE);
$this->csv->setCsvControl($this->delimiter, $this->enclosure, $this->escape);
$this->fieldNames = $this->csv->fgetcsv();
}
$data = $this->csv->fgetcsv();
if (false !== $data) {
if ($data === array(null) || $data === null) {
return null;
}
if ($this->stepExecution) {
$this->stepExecution->incrementSummaryInfo('read');
}
if (count($this->fieldNames) !== count($data)) {
throw new InvalidItemException('pim_base_connector.steps.csv_reader.invalid_item_columns_count', $data, array('%totalColumnsCount%' => count($this->fieldNames), '%itemColumnsCount%' => count($data), '%csvPath%' => $this->csv->getRealPath(), '%lineno%' => $this->csv->key()));
}
$data = array_combine($this->fieldNames, $data);
} else {
throw new \RuntimeException('An error occured while reading the csv.');
}
return $data;
}
示例8: evaluate
/**
* Evaluates the closure and the code in the reset of the file. This sets
* the code for the closure, the namespace it is declared in (if any) and
* any applicable namespace imports
* @param ReflectionFunction The reflected function of the closure
* @return String The code the closure runs
*/
private function evaluate(\ReflectionFunction $reflection)
{
$code = '';
$full = '';
$file = new \SplFileObject($reflection->getFileName());
while (!$file->eof()) {
if ($file->key() >= $reflection->getStartLine() - 1 && $file->key() < $reflection->getEndLine()) {
$code .= $file->current();
}
$full .= $file->current();
$file->next();
}
//@todo this assumes the function will be the only one on that line
$begin = strpos($code, 'function');
//@todo this assumes the } will be the only one on that line
$end = strrpos($code, '}');
$this->code = substr($code, $begin, $end - $begin + 1);
$this->extractDetail($full);
}
示例9: current
public function current()
{
if ($this->_columns) {
if (false === ($combined = @array_combine($this->_columns, parent::current()))) {
throw new Exception(sprintf("Column count did not match expected on line %d", parent::key()));
}
return $combined;
}
return parent::current();
}
示例10: 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);
}
示例11: getClosureCode
private function getClosureCode(\Closure $closure)
{
$reflection = new \ReflectionFunction($closure);
// Open file and seek to the first line of the closure
$file = new \SplFileObject($reflection->getFileName());
$file->seek($reflection->getStartLine() - 1);
// Retrieve all of the lines that contain code for the closure
$code = '';
while ($file->key() < $reflection->getEndLine()) {
$line = $file->current();
$line = ltrim($line);
$code .= $line;
$file->next();
}
return $code;
}
示例12: _fetchCode
protected function _fetchCode()
{
// Open file and seek to the first line of the closure
$file = new SplFileObject($this->reflection->getFileName());
$file->seek($this->reflection->getStartLine() - 1);
// Retrieve all of the lines that contain code for the closure
$code = '';
while ($file->key() < $this->reflection->getEndLine()) {
$code .= $file->current();
$file->next();
}
// Only keep the code defining that closure
$begin = strpos($code, 'function');
$end = strrpos($code, '}');
$code = substr($code, $begin, $end - $begin + 1);
return $code;
}
示例13: getHook
/**
* Get's the next string of hook code to process.
*
* @return string
*/
public function getHook()
{
foreach ($this->hooks as $value) {
$hook = '<?php function ' . $value[1] . '(){';
$reflection = new \ReflectionFunction($value[2]);
$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') + 8;
$begin = strrpos($code, '{', $begin) + 1;
$end = strrpos($code, '}');
$hook .= substr($code, $begin, $end - $begin);
$hook .= '}' . PHP_EOL;
(yield $value[0] => $hook);
}
}
示例14: read
/**
* {@inheritdoc}
*/
public function read()
{
if (null === $this->csv) {
$this->initializeRead();
}
$data = $this->csv->fgetcsv();
if (false !== $data) {
if ([null] === $data || null === $data) {
return null;
}
if ($this->stepExecution) {
$this->stepExecution->incrementSummaryInfo('read_lines');
}
if (count($this->fieldNames) !== count($data)) {
throw new InvalidItemException('pim_connector.steps.csv_reader.invalid_item_columns_count', $data, ['%totalColumnsCount%' => count($this->fieldNames), '%itemColumnsCount%' => count($data), '%csvPath%' => $this->csv->getRealPath(), '%lineno%' => $this->csv->key()]);
}
$data = array_combine($this->fieldNames, $data);
} else {
throw new \RuntimeException('An error occurred while reading the csv.');
}
return $data;
}
示例15: parseFile
public function parseFile($logFile, &$startId, $finish = false)
{
if (!$logFile || !file_exists($logFile)) {
return $this->setError('Log file error');
}
try {
$file = new SplFileObject($logFile);
$file->seek(!$startId ? 0 : $startId - 1);
$counter = 0;
$items = array();
$item = null;
while ($file->valid()) {
$row = trim($file->current());
$file->next();
if (!$row) {
continue;
}
$item = Mage::getModel('mpbackup/backup_progress_item')->parse($row, $item);
$items[] = $item;
$counter += $item->getLength();
if (!$finish && $counter > Mageplace_Backup_Helper_Const::BACKUP_PROCESS_REQUEST_PERIOD * Mageplace_Backup_Helper_Const::BACKUP_PROCESS_RESPONSE_SIZE) {
break;
}
}
$startId = $file->key();
} catch (Exception $e) {
Mage::logException($e);
return $this->setError($e->getMessage());
}
if (empty($items)) {
if ($finish) {
return $this->setError('Log is empty (' . print_r($items, true) . ') and log process is finished');
} else {
return $this->setData(self::TEXT, self::TEXT_TREE_POINT)->setError(print_r($items, true));
}
}
return $this->setData(self::ITEMS, $items);
}