[ Index ]

PHP Cross Reference of PHK Manager

title

Body

[close]

/PHK/Stream/ -> Backend.php (source)

   1  <?php
   2  //=============================================================================
   3  //
   4  //   Licensed under the Apache License, Version 2.0 (the "License");
   5  //   you may not use this file except in compliance with the License.
   6  //   You may obtain a copy of the License at
   7  //
   8  //       http://www.apache.org/licenses/LICENSE-2.0
   9  //
  10  //   Unless required by applicable law or agreed to in writing, software
  11  //   distributed under the License is distributed on an "AS IS" BASIS,
  12  //   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13  //   See the License for the specific language governing permissions and
  14  //   limitations under the License.
  15  //
  16  //=============================================================================
  17  /**
  18  * @copyright Francois Laupretre <phk@tekwire.net>
  19  * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, V 2.0
  20  * @category PHK
  21  * @package PHK
  22  *///==========================================================================
  23  
  24  namespace PHK\Stream {
  25  
  26  if (!class_exists('PHK\Stream\Backend',false))
  27  {
  28  //=============================================================================
  29  /**
  30  * The 'slow' backend to \PHK\Stream\Wrapper
  31  *
  32  * This class is called when \PHK\Stream\Wrapper cannot retrieve the information it needs
  33  * from the cache or for uncacheable information.
  34  *
  35  * Note (30-NOV-2007): As long as the extension does not trap exceptions
  36  * correctly, we trap them here and return null instead.
  37  *
  38  * API status: Private
  39  * Included in the PHK PHP runtime: Yes
  40  * Implemented in the extension: No
  41  *///==========================================================================
  42  
  43  class Backend
  44  {
  45  
  46  /** @var string        Used as temp storage by stringInclude() */
  47  
  48  private static $tmp_data=null;
  49  
  50  /** tmp pseudo-file URI */
  51  
  52  const TMP_URI='phk://?tmp';
  53  
  54  //---------------------------------
  55  // Returns pseudo-file data string.
  56  // If a command does not want to be cached, it sets $cache to false.
  57  // stat() calls are used mainly for file existence/type. Size is less
  58  // important.
  59  
  60  private static function commandOpenOrStat($stat_flag,$mnt,$command,$params
  61      ,$path,&$cache)
  62  {
  63  $cache=true; // Default
  64  
  65  try
  66  {
  67  if (is_null($mnt))    // Global command
  68      {
  69      switch($command)
  70          {
  71          case 'test':
  72              return "Test line 1/3\nTest line2/3\nTest line 3/3\n";
  73              break;
  74  
  75          case 'tmp':    // Special: Used by \PHK::stringInclude();
  76              $cache=false;
  77              return self::$tmp_data;
  78              break;
  79  
  80          default:
  81              throw new \Exception($command.': Unknown global command');
  82          }
  83      }
  84  else // Slow path
  85      {
  86      $proxy=\PHK\Mgr::proxy($mnt);
  87  
  88      switch ($command)
  89          {
  90          // These commands :
  91          //    - go to the proxy
  92          //    - are cached
  93          //    - take a mandatory 'name' argument and send it to the method
  94          //    - take the data returned by the method
  95  
  96          case 'section':
  97           case 'magicField':
  98              if (!isset($params['name']))
  99                  throw new \Exception($command
 100                      .': command needs this argument: name');
 101              return $proxy->$command($params['name']);
 102  
 103          // These commands :
 104          //    - go to the proxy
 105          //    - are cached
 106          //    - serialize the data returned by the method
 107  
 108          case 'pathList':
 109          case 'sectionList':
 110              return serialize($proxy->$command());
 111  
 112          default:
 113              throw new \Exception($command.': Unknown command');
 114          }
 115      }
 116  }
 117  catch (\Exception $e)
 118      {
 119      throw new \Exception($command.': Error during command execution - '
 120          .$e->getMessage());
 121      }
 122  }
 123  
 124  //---------------------------------
 125  // Segfault in extension if this function throws an exception. As long as
 126  // this bug is not fixed, trap the exception before returning to PHK\Stream\Wrapper
 127  
 128  public static function getFileData($mnt,$command,$params,$path,&$cache)
 129  {
 130  $cache=true;
 131  
 132  try
 133  {
 134  if (is_null($command))
 135      {
 136      $node=\PHK\Mgr::proxy($mnt)->ftree()->lookupFile($path,false);
 137      if (is_null($node)) return null;
 138      return $node->read();
 139      }
 140  else
 141      {
 142      return self::commandOpenOrStat(false,$mnt,$command,$params,$path,$cache);
 143      }
 144  }
 145  catch (\Exception $e) { return null; }
 146  }
 147  
 148  //---------------------------------
 149  // Must accept the same parameters as getFileData()
 150  
 151  public static function getDirData($mnt,$command,$params,$path)
 152  {
 153  try
 154  {
 155  if (!is_null($command)) return null;
 156  
 157  $node=\PHK\Mgr::proxy($mnt)->ftree()->lookup($path,false);
 158  if (is_null($node)) return null;
 159  return $node->getDir();
 160  }
 161  catch (\Exception $e) { return null; }
 162  }
 163  
 164  //---------------------------------
 165  
 166  public static function getStatData($mnt,$command,$params,$path,$cache
 167      ,&$mode,&$size,&$mtime)
 168  {
 169  if (!is_null($command))
 170      {
 171      $mode=0100444;    // Pseudo regular file
 172      // Throws exception if command invalid or no target
 173      $size=strlen(self::commandOpenOrStat(true,$mnt,$command,$params
 174          ,$path,$cache));
 175      }
 176  else
 177      {
 178      $node=\PHK\Mgr::proxy($mnt)->ftree()->lookup($path);
 179  
 180      $mode=$node->mode();
 181      $size=$node->size();
 182      }
 183  $mtime=(is_null($mnt) ? time() : \PHK\Mgr::instance($mnt)->mtime());
 184  }
 185  
 186  //----
 187  // Undocumented
 188  
 189  public static function setTmpData($str)
 190  {
 191  $prev=self::$tmp_data;
 192  self::$tmp_data=$str;
 193  return $prev;
 194  }
 195  
 196  //----
 197  // Undocumented
 198  // Applies php_strip_whitespace() to a string
 199  
 200  public static function _stripString($str)
 201  {
 202  if (getenv('PHK_NO_STRIP')!==false) return $str;
 203  
 204  $save=self::setTmpData($str);
 205  $res=php_strip_whitespace(self::TMP_URI);
 206  self::setTmpData($save);
 207  return $res;
 208  }
 209  
 210  //----
 211  // Undocumented
 212  // Include a string as if it was in a source file
 213  
 214  public static function _includeString($str)
 215  {
 216  $save=self::setTmpData($str);
 217  $res=require(self::TMP_URI);
 218  self::setTmpData($save);
 219  return $res;
 220  }
 221  
 222  //---
 223  } // End of class
 224  //===========================================================================
 225  } // End of class_exists
 226  //===========================================================================
 227  } // End of namespace
 228  //===========================================================================
 229  ?>


Generated: Thu Jun 4 18:33:15 2015 Cross-referenced by PHPXref 0.7.1