. */ /** * File iterator. * * * $file = new \Textpattern\Iterator\FileIterator('file.txt'); * foreach ($file as $key => $line) { * echo $line; * } * * * @since 4.6.0 * @package Iterator */ namespace Textpattern\Iterator; class FileIterator implements \Iterator { /** * Filename. * * @var string */ protected $filename; /** * Line length. * * @var int */ protected $lineLength = 4096; /** * Filepointer. * * @var resource */ protected $filepointer; /** * The current element. */ protected $current; /** * The current index. * * @var int */ protected $key = -1; /** * Whether it's valid or not. * * @var bool */ protected $valid; /** * Constructor. * * @param string $filename The filename */ public function __construct($filename) { $this->filename = $filename; $this->rewind(); } /** * Destructor. */ public function __destruct() { if (is_resource($this->filepointer)) { fclose($this->filepointer); } } /** * Returns the current element. * * @return \Textpattern\Type\StringType */ public function current() { return new \Textpattern\Type\StringType($this->current); } /** * {@inheritdoc} */ public function key() { return $this->key; } /** * {@inheritdoc} */ public function next() { if (!feof($this->filepointer)) { $this->current = fgets($this->filepointer, $this->lineLength); $this->key++; $this->valid = true; } else { $this->valid = false; fclose($this->filepointer); } } /** * {@inheritdoc} */ public function rewind() { if (is_resource($this->filepointer) === false) { if (($this->filepointer = fopen($this->filename, 'r')) === false) { throw new \Exception(gTxt('invalid_argument', array('{name}' => 'filename'))); } } rewind($this->filepointer); $this->key = -1; $this->next(); } /** * {@inheritdoc} */ public function valid() { return $this->valid; } }