Textpattern PHP Cross Reference Content Management Systems

Source: /textpattern/vendors/Textpattern/Iterator/FileIterator.php - 172 lines - 3176 bytes - Summary - Text - Print

Description: File iterator.

   1  <?php
   2  
   3  /*
   4   * Textpattern Content Management System
   5   * https://textpattern.com/
   6   *
   7   * Copyright (C) 2020 The Textpattern Development Team
   8   *
   9   * This file is part of Textpattern.
  10   *
  11   * Textpattern is free software; you can redistribute it and/or
  12   * modify it under the terms of the GNU General Public License
  13   * as published by the Free Software Foundation, version 2.
  14   *
  15   * Textpattern is distributed in the hope that it will be useful,
  16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18   * GNU General Public License for more details.
  19   *
  20   * You should have received a copy of the GNU General Public License
  21   * along with Textpattern. If not, see <https://www.gnu.org/licenses/>.
  22   */
  23  
  24  /**
  25   * File iterator.
  26   *
  27   * <code>
  28   * $file = new \Textpattern\Iterator\FileIterator('file.txt');
  29   * foreach ($file as $key => $line) {
  30   *     echo $line;
  31   * }
  32   * </code>
  33   *
  34   * @since   4.6.0
  35   * @package Iterator
  36   */
  37  
  38  namespace Textpattern\Iterator;
  39  
  40  class FileIterator implements \Iterator
  41  {
  42      /**
  43       * Filename.
  44       *
  45       * @var string
  46       */
  47  
  48      protected $filename;
  49  
  50      /**
  51       * Line length.
  52       *
  53       * @var int
  54       */
  55  
  56      protected $lineLength = 4096;
  57  
  58      /**
  59       * Filepointer.
  60       *
  61       * @var resource
  62       */
  63  
  64      protected $filepointer;
  65  
  66      /**
  67       * The current element.
  68       */
  69  
  70      protected $current;
  71  
  72      /**
  73       * The current index.
  74       *
  75       * @var int
  76       */
  77  
  78      protected $key = -1;
  79  
  80      /**
  81       * Whether it's valid or not.
  82       *
  83       * @var bool
  84       */
  85  
  86      protected $valid;
  87  
  88      /**
  89       * Constructor.
  90       *
  91       * @param string $filename The filename
  92       */
  93  
  94      public function __construct($filename)
  95      {
  96          $this->filename = $filename;
  97          $this->rewind();
  98      }
  99  
 100      /**
 101       * Destructor.
 102       */
 103  
 104      public function __destruct()
 105      {
 106          if (is_resource($this->filepointer)) {
 107              fclose($this->filepointer);
 108          }
 109      }
 110  
 111      /**
 112       * Returns the current element.
 113       *
 114       * @return \Textpattern\Type\StringType
 115       */
 116  
 117      public function current()
 118      {
 119          return new \Textpattern\Type\StringType($this->current);
 120      }
 121  
 122      /**
 123       * {@inheritdoc}
 124       */
 125  
 126      public function key()
 127      {
 128          return $this->key;
 129      }
 130  
 131      /**
 132       * {@inheritdoc}
 133       */
 134  
 135      public function next()
 136      {
 137          if (!feof($this->filepointer)) {
 138              $this->current = fgets($this->filepointer, $this->lineLength);
 139              $this->key++;
 140              $this->valid = true;
 141          } else {
 142              $this->valid = false;
 143              fclose($this->filepointer);
 144          }
 145      }
 146  
 147      /**
 148       * {@inheritdoc}
 149       */
 150  
 151      public function rewind()
 152      {
 153          if (is_resource($this->filepointer) === false) {
 154              if (($this->filepointer = fopen($this->filename, 'r')) === false) {
 155                  throw new \Exception(gTxt('invalid_argument', array('{name}' => 'filename')));
 156              }
 157          }
 158  
 159          rewind($this->filepointer);
 160          $this->key = -1;
 161          $this->next();
 162      }
 163  
 164      /**
 165       * {@inheritdoc}
 166       */
 167  
 168      public function valid()
 169      {
 170          return $this->valid;
 171      }
 172  }

title

Description

title

Description

title

Description

title

title

Body