[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/phpexcel/PHPExcel/CachedObjectStorage/ -> PHPTemp.php (source)

   1  <?php
   2  
   3  /**
   4   * PHPExcel_CachedObjectStorage_PHPTemp
   5   *
   6   * Copyright (c) 2006 - 2015 PHPExcel
   7   *
   8   * This library is free software; you can redistribute it and/or
   9   * modify it under the terms of the GNU Lesser General Public
  10   * License as published by the Free Software Foundation; either
  11   * version 2.1 of the License, or (at your option) any later version.
  12   *
  13   * This library is distributed in the hope that it will be useful,
  14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16   * Lesser General Public License for more details.
  17   *
  18   * You should have received a copy of the GNU Lesser General Public
  19   * License along with this library; if not, write to the Free Software
  20   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  21   *
  22   * @category   PHPExcel
  23   * @package    PHPExcel_CachedObjectStorage
  24   * @copyright  Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
  25   * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  26   * @version    ##VERSION##, ##DATE##
  27   */
  28  class PHPExcel_CachedObjectStorage_PHPTemp extends PHPExcel_CachedObjectStorage_CacheBase implements PHPExcel_CachedObjectStorage_ICache
  29  {
  30      /**
  31       * Name of the file for this cache
  32       *
  33       * @var string
  34       */
  35      private $fileHandle = null;
  36  
  37      /**
  38       * Memory limit to use before reverting to file cache
  39       *
  40       * @var integer
  41       */
  42      private $memoryCacheSize = null;
  43  
  44      /**
  45       * Store cell data in cache for the current cell object if it's "dirty",
  46       *     and the 'nullify' the current cell object
  47       *
  48       * @return    void
  49       * @throws    PHPExcel_Exception
  50       */
  51      protected function storeData()
  52      {
  53          if ($this->currentCellIsDirty && !empty($this->currentObjectID)) {
  54              $this->currentObject->detach();
  55  
  56              fseek($this->fileHandle, 0, SEEK_END);
  57  
  58              $this->cellCache[$this->currentObjectID] = array(
  59                  'ptr' => ftell($this->fileHandle),
  60                  'sz'  => fwrite($this->fileHandle, serialize($this->currentObject))
  61              );
  62              $this->currentCellIsDirty = false;
  63          }
  64          $this->currentObjectID = $this->currentObject = null;
  65      }
  66  
  67  
  68      /**
  69       * Add or Update a cell in cache identified by coordinate address
  70       *
  71       * @param    string            $pCoord        Coordinate address of the cell to update
  72       * @param    PHPExcel_Cell    $cell        Cell to update
  73       * @return    PHPExcel_Cell
  74       * @throws    PHPExcel_Exception
  75       */
  76      public function addCacheData($pCoord, PHPExcel_Cell $cell)
  77      {
  78          if (($pCoord !== $this->currentObjectID) && ($this->currentObjectID !== null)) {
  79              $this->storeData();
  80          }
  81  
  82          $this->currentObjectID = $pCoord;
  83          $this->currentObject = $cell;
  84          $this->currentCellIsDirty = true;
  85  
  86          return $cell;
  87      }
  88  
  89  
  90      /**
  91       * Get cell at a specific coordinate
  92       *
  93       * @param     string             $pCoord        Coordinate of the cell
  94       * @throws     PHPExcel_Exception
  95       * @return     PHPExcel_Cell     Cell that was found, or null if not found
  96       */
  97      public function getCacheData($pCoord)
  98      {
  99          if ($pCoord === $this->currentObjectID) {
 100              return $this->currentObject;
 101          }
 102          $this->storeData();
 103  
 104          //    Check if the entry that has been requested actually exists
 105          if (!isset($this->cellCache[$pCoord])) {
 106              //    Return null if requested entry doesn't exist in cache
 107              return null;
 108          }
 109  
 110          //    Set current entry to the requested entry
 111          $this->currentObjectID = $pCoord;
 112          fseek($this->fileHandle, $this->cellCache[$pCoord]['ptr']);
 113          $this->currentObject = unserialize(fread($this->fileHandle, $this->cellCache[$pCoord]['sz']));
 114          //    Re-attach this as the cell's parent
 115          $this->currentObject->attach($this);
 116  
 117          //    Return requested entry
 118          return $this->currentObject;
 119      }
 120  
 121      /**
 122       * Get a list of all cell addresses currently held in cache
 123       *
 124       * @return  string[]
 125       */
 126      public function getCellList()
 127      {
 128          if ($this->currentObjectID !== null) {
 129              $this->storeData();
 130          }
 131  
 132          return parent::getCellList();
 133      }
 134  
 135      /**
 136       * Clone the cell collection
 137       *
 138       * @param    PHPExcel_Worksheet    $parent        The new worksheet
 139       * @return    void
 140       */
 141      public function copyCellCollection(PHPExcel_Worksheet $parent)
 142      {
 143          parent::copyCellCollection($parent);
 144          //    Open a new stream for the cell cache data
 145          $newFileHandle = fopen('php://temp/maxmemory:' . $this->memoryCacheSize, 'a+');
 146          //    Copy the existing cell cache data to the new stream
 147          fseek($this->fileHandle, 0);
 148          while (!feof($this->fileHandle)) {
 149              fwrite($newFileHandle, fread($this->fileHandle, 1024));
 150          }
 151          $this->fileHandle = $newFileHandle;
 152      }
 153  
 154      /**
 155       * Clear the cell collection and disconnect from our parent
 156       *
 157       * @return    void
 158       */
 159      public function unsetWorksheetCells()
 160      {
 161          if (!is_null($this->currentObject)) {
 162              $this->currentObject->detach();
 163              $this->currentObject = $this->currentObjectID = null;
 164          }
 165          $this->cellCache = array();
 166  
 167          //    detach ourself from the worksheet, so that it can then delete this object successfully
 168          $this->parent = null;
 169  
 170          //    Close down the php://temp file
 171          $this->__destruct();
 172      }
 173  
 174      /**
 175       * Initialise this new cell collection
 176       *
 177       * @param    PHPExcel_Worksheet    $parent        The worksheet for this cell collection
 178       * @param    array of mixed        $arguments    Additional initialisation arguments
 179       */
 180      public function __construct(PHPExcel_Worksheet $parent, $arguments)
 181      {
 182          $this->memoryCacheSize = (isset($arguments['memoryCacheSize'])) ? $arguments['memoryCacheSize'] : '1MB';
 183  
 184          parent::__construct($parent);
 185          if (is_null($this->fileHandle)) {
 186              $this->fileHandle = fopen('php://temp/maxmemory:' . $this->memoryCacheSize, 'a+');
 187          }
 188      }
 189  
 190      /**
 191       * Destroy this cell collection
 192       */
 193      public function __destruct()
 194      {
 195          if (!is_null($this->fileHandle)) {
 196              fclose($this->fileHandle);
 197          }
 198          $this->fileHandle = null;
 199      }
 200  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1