[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Utility/ -> BufferStreamHandler.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2007 Open Source Matters, Inc. <https://www.joomla.org>
   7   * @license    GNU General Public License version 2 or later; see LICENSE.txt
   8   *
   9   * Remove phpcs exception with deprecated autoloading BufferStreamHandler::stream_register();
  10   * @phpcs:disable PSR1.Files.SideEffects
  11   */
  12  
  13  namespace Joomla\CMS\Utility;
  14  
  15  \defined('JPATH_PLATFORM') or die;
  16  
  17  /**
  18   * @deprecated 5.0 Workaround for B/C. (removal missed in 4.0, also remove phpcs exception).
  19   * If BufferStreamHandler is needed directly call BufferStreamHandler::stream_register();
  20   */
  21  BufferStreamHandler::stream_register();
  22  
  23  /**
  24   * Generic Buffer stream handler
  25   *
  26   * This class provides a generic buffer stream.  It can be used to store/retrieve/manipulate
  27   * string buffers with the standard PHP filesystem I/O methods.
  28   *
  29   * @since  1.7.0
  30   */
  31  class BufferStreamHandler
  32  {
  33      /**
  34       * Stream position
  35       *
  36       * @var    integer
  37       * @since  1.7.0
  38       */
  39      public $position = 0;
  40  
  41      /**
  42       * Buffer name
  43       *
  44       * @var    string
  45       * @since  1.7.0
  46       */
  47      public $name = null;
  48  
  49      /**
  50       * Buffer hash
  51       *
  52       * @var    array
  53       * @since  3.0.0
  54       */
  55      public $buffers = array();
  56  
  57      /**
  58       * Status of registering the wrapper
  59       *
  60       * @var    boolean
  61       * @since  3.8.2
  62       */
  63      private static $registered = false;
  64  
  65      /**
  66       * Function to register the stream wrapper
  67       *
  68       * @return  void
  69       *
  70       * @since  3.8.2
  71       */
  72      public static function stream_register()
  73      {
  74          if (!self::$registered) {
  75              stream_wrapper_register('buffer', '\\Joomla\\CMS\\Utility\\BufferStreamHandler');
  76  
  77              self::$registered = true;
  78          }
  79      }
  80  
  81      /**
  82       * Function to open file or url
  83       *
  84       * @param   string   $path         The URL that was passed
  85       * @param   string   $mode         Mode used to open the file @see fopen
  86       * @param   integer  $options      Flags used by the API, may be STREAM_USE_PATH and
  87       *                                 STREAM_REPORT_ERRORS
  88       * @param   string   &$openedPath  Full path of the resource. Used with STREAM_USE_PATH option
  89       *
  90       * @return  boolean
  91       *
  92       * @since   1.7.0
  93       * @see     streamWrapper::stream_open
  94       */
  95      public function stream_open($path, $mode, $options, &$openedPath)
  96      {
  97          $url = parse_url($path);
  98          $this->name = $url['host'];
  99          $this->buffers[$this->name] = null;
 100          $this->position = 0;
 101  
 102          return true;
 103      }
 104  
 105      /**
 106       * Read stream
 107       *
 108       * @param   integer  $count  How many bytes of data from the current position should be returned.
 109       *
 110       * @return  mixed    The data from the stream up to the specified number of bytes (all data if
 111       *                   the total number of bytes in the stream is less than $count. Null if
 112       *                   the stream is empty.
 113       *
 114       * @see     streamWrapper::stream_read
 115       * @since   1.7.0
 116       */
 117      public function stream_read($count)
 118      {
 119          $ret = substr($this->buffers[$this->name], $this->position, $count);
 120          $this->position += \strlen($ret);
 121  
 122          return $ret;
 123      }
 124  
 125      /**
 126       * Write stream
 127       *
 128       * @param   string  $data  The data to write to the stream.
 129       *
 130       * @return  integer
 131       *
 132       * @see     streamWrapper::stream_write
 133       * @since   1.7.0
 134       */
 135      public function stream_write($data)
 136      {
 137          $left = substr($this->buffers[$this->name], 0, $this->position);
 138          $right = substr($this->buffers[$this->name], $this->position + \strlen($data));
 139          $this->buffers[$this->name] = $left . $data . $right;
 140          $this->position += \strlen($data);
 141  
 142          return \strlen($data);
 143      }
 144  
 145      /**
 146       * Function to get the current position of the stream
 147       *
 148       * @return  integer
 149       *
 150       * @see     streamWrapper::stream_tell
 151       * @since   1.7.0
 152       */
 153      public function stream_tell()
 154      {
 155          return $this->position;
 156      }
 157  
 158      /**
 159       * Function to test for end of file pointer
 160       *
 161       * @return  boolean  True if the pointer is at the end of the stream
 162       *
 163       * @see     streamWrapper::stream_eof
 164       * @since   1.7.0
 165       */
 166      public function stream_eof()
 167      {
 168          return $this->position >= \strlen($this->buffers[$this->name]);
 169      }
 170  
 171      /**
 172       * The read write position updates in response to $offset and $whence
 173       *
 174       * @param   integer  $offset  The offset in bytes
 175       * @param   integer  $whence  Position the offset is added to
 176       *                            Options are SEEK_SET, SEEK_CUR, and SEEK_END
 177       *
 178       * @return  boolean  True if updated
 179       *
 180       * @see     streamWrapper::stream_seek
 181       * @since   1.7.0
 182       */
 183      public function stream_seek($offset, $whence)
 184      {
 185          switch ($whence) {
 186              case SEEK_SET:
 187                  return $this->seek_set($offset);
 188  
 189              case SEEK_CUR:
 190                  return $this->seek_cur($offset);
 191  
 192              case SEEK_END:
 193                  return $this->seek_end($offset);
 194          }
 195  
 196          return false;
 197      }
 198  
 199      /**
 200       * Set the position to the offset
 201       *
 202       * @param   integer  $offset  The offset in bytes
 203       *
 204       * @return  boolean
 205       */
 206      protected function seek_set($offset)
 207      {
 208          if ($offset < 0 || $offset > \strlen($this->buffers[$this->name])) {
 209              return false;
 210          }
 211  
 212          $this->position = $offset;
 213  
 214          return true;
 215      }
 216  
 217      /**
 218       * Adds the offset to current position
 219       *
 220       * @param   integer  $offset  The offset in bytes
 221       *
 222       * @return  boolean
 223       */
 224      protected function seek_cur($offset)
 225      {
 226          if ($offset < 0) {
 227              return false;
 228          }
 229  
 230          $this->position += $offset;
 231  
 232          return true;
 233      }
 234  
 235      /**
 236       * Sets the position to the end of the current buffer + offset
 237       *
 238       * @param   integer  $offset  The offset in bytes
 239       *
 240       * @return  boolean
 241       */
 242      protected function seek_end($offset)
 243      {
 244          $offset += \strlen($this->buffers[$this->name]);
 245  
 246          if ($offset < 0) {
 247              return false;
 248          }
 249  
 250          $this->position = $offset;
 251  
 252          return true;
 253      }
 254  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer