Source for file string.php

Documentation is available at string.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Platform
  4.  * @subpackage  FileSystem
  5.  *
  6.  * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  7.  * @license     GNU General Public License version 2 or later; see LICENSE
  8.  */
  9.  
  10. defined('JPATH_PLATFORM'or die;
  11.  
  12. jimport('joomla.filesystem.support.stringcontroller');
  13.  
  14. /**
  15.  * String Stream Wrapper
  16.  *
  17.  * This class allows you to use a PHP string in the same way that
  18.  * you would normally use a regular stream wrapper
  19.  *
  20.  * @package     Joomla.Platform
  21.  * @subpackage  FileSystem
  22.  * @since       11.1
  23.  */
  24. {
  25.     /**
  26.      * The current string
  27.      *
  28.      * @var   string 
  29.      * @since  12.1
  30.      */
  31.     protected $currentString;
  32.  
  33.     /**
  34.      *
  35.      * The path
  36.      *
  37.      * @var   string 
  38.      * @since  12.1
  39.      */
  40.     protected $path;
  41.  
  42.     /**
  43.      *
  44.      * The mode
  45.      *
  46.      * @var   string 
  47.      * @since  12.1
  48.      */
  49.     protected $mode;
  50.  
  51.     /**
  52.      *
  53.      * Enter description here ...
  54.      * @var   string 
  55.      *
  56.      * @since  12.1
  57.      */
  58.     protected $options;
  59.  
  60.     /**
  61.      *
  62.      * Enter description here ...
  63.      * @var   string 
  64.      *
  65.      * @since  12.1
  66.      */
  67.     protected $openedPath;
  68.  
  69.     /**
  70.      * Current position
  71.      *
  72.      * @var   integer 
  73.      * @since  12.1
  74.      */
  75.     protected $pos;
  76.  
  77.     /**
  78.      * Length of the string
  79.      *
  80.      * @var   string 
  81.      *
  82.      * @since  12.1
  83.      */
  84.     protected $len;
  85.  
  86.     /**
  87.      * Statistics for a file
  88.      *
  89.      * @var    array 
  90.      * @since  12.1
  91.      *
  92.      * @see    http://us.php.net/manual/en/function.stat.php
  93.      */
  94.     protected $stat;
  95.  
  96.     /**
  97.      * Method to open a file or URL.
  98.      *
  99.      * @param   string   $path          The stream path.
  100.      * @param   string   $mode          Not used.
  101.      * @param   integer  $options       Not used.
  102.      * @param   string   &$opened_path  Not used.
  103.      *
  104.      * @return  boolean 
  105.      *
  106.      * @since   11.1
  107.      */
  108.     public function stream_open($path$mode$options&$opened_path)
  109.     {
  110.         $this->currentString = &JStringController::getRef(str_replace('string://'''$path));
  111.  
  112.         if ($this->currentString)
  113.         {
  114.             $this->len = strlen($this->currentString);
  115.             $this->pos = 0;
  116.             $this->stat = $this->url_stat($path0);
  117.  
  118.             return true;
  119.         }
  120.         else
  121.         {
  122.             return false;
  123.         }
  124.     }
  125.  
  126.     /**
  127.      * Method to retrieve information from a file resource
  128.      *
  129.      * @return  array 
  130.      *
  131.      * @see     http://www.php.net/manual/en/streamwrapper.stream-stat.php
  132.      * @since   11.1
  133.      */
  134.     public function stream_stat()
  135.     {
  136.         return $this->stat;
  137.     }
  138.  
  139.     /**
  140.      * Method to retrieve information about a file.
  141.      *
  142.      * @param   string   $path   File path or URL to stat
  143.      * @param   integer  $flags  Additional flags set by the streams API
  144.      *
  145.      * @return  array 
  146.      *
  147.      * @see     http://php.net/manual/en/streamwrapper.url-stat.php
  148.      * @since   11.1
  149.      */
  150.     public function url_stat($path$flags 0)
  151.     {
  152.         $now time();
  153.         $string &JStringController::getRef(str_replace('string://'''$path));
  154.         $stat array(
  155.             'dev' => 0,
  156.             'ino' => 0,
  157.             'mode' => 0,
  158.             'nlink' => 1,
  159.             'uid' => 0,
  160.             'gid' => 0,
  161.             'rdev' => 0,
  162.             'size' => strlen($string),
  163.             'atime' => $now,
  164.             'mtime' => $now,
  165.             'ctime' => $now,
  166.             'blksize' => '512',
  167.             'blocks' => ceil(strlen($string512));
  168.  
  169.         return $stat;
  170.     }
  171.  
  172.     /**
  173.      * Method to read a given number of bytes starting at the current position
  174.      * and moving to the end of the string defined by the current position plus the
  175.      * given number.
  176.      *
  177.      * @param   integer  $count  Bytes of data from the current position should be returned.
  178.      *
  179.      * @return  void 
  180.      *
  181.      * @since   11.1
  182.      *
  183.      * @see     http://www.php.net/manual/en/streamwrapper.stream-read.php
  184.      */
  185.     public function stream_read($count)
  186.     {
  187.         $result substr($this->currentString$this->pos$count);
  188.         $this->pos += $count;
  189.  
  190.         return $result;
  191.     }
  192.  
  193.     /**
  194.      * Stream write, always returning false.
  195.      *
  196.      * @param   string  $data  The data to write.
  197.      *
  198.      * @return  boolean 
  199.      *
  200.      * @since   11.1
  201.      * @note    Updating the string is not supported.
  202.      */
  203.     public function stream_write($data)
  204.     {
  205.         // We don't support updating the string.
  206.         return false;
  207.     }
  208.  
  209.     /**
  210.      * Method to get the current position
  211.      *
  212.      * @return  integer  The position
  213.      *
  214.      * @since   11.1
  215.      */
  216.     public function stream_tell()
  217.     {
  218.         return $this->pos;
  219.     }
  220.  
  221.     /**
  222.      * End of field check
  223.      *
  224.      * @return  boolean  True if at end of field.
  225.      *
  226.      * @since   11.1
  227.      */
  228.     public function stream_eof()
  229.     {
  230.         if ($this->pos > $this->len)
  231.         {
  232.             return true;
  233.         }
  234.  
  235.         return false;
  236.     }
  237.  
  238.     /**
  239.      * Stream offset
  240.      *
  241.      * @param   integer  $offset  The starting offset.
  242.      * @param   integer  $whence  SEEK_SET, SEEK_CUR, SEEK_END
  243.      *
  244.      * @return  boolean  True on success.
  245.      *
  246.      * @since   11.1
  247.      */
  248.     public function stream_seek($offset$whence)
  249.     {
  250.         // $whence: SEEK_SET, SEEK_CUR, SEEK_END
  251.         if ($offset $this->len)
  252.         {
  253.             // We can't seek beyond our len.
  254.             return false;
  255.         }
  256.  
  257.         switch ($whence)
  258.         {
  259.             case SEEK_SET:
  260.                 $this->pos = $offset;
  261.                 break;
  262.  
  263.             case SEEK_CUR:
  264.                 if (($this->pos + $offset$this->len)
  265.                 {
  266.                     $this->pos += $offset;
  267.                 }
  268.                 else
  269.                 {
  270.                     return false;
  271.                 }
  272.                 break;
  273.  
  274.             case SEEK_END:
  275.                 $this->pos = $this->len - $offset;
  276.                 break;
  277.         }
  278.  
  279.         return true;
  280.     }
  281.  
  282.     /**
  283.      * Stream flush, always returns true.
  284.      *
  285.      * @return  boolean 
  286.      *
  287.      * @since   11.1
  288.      * @note    Data storage is not supported
  289.      */
  290.     public function stream_flush()
  291.     {
  292.         // We don't store data.
  293.         return true;
  294.     }
  295. }
  296.  
  297. stream_wrapper_register('string''JStreamString'or die('JStreamString Wrapper Registration Failed');

Documentation generated on Tue, 19 Nov 2013 15:14:32 +0100 by phpDocumentor 1.4.3