[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * PHPExcel_CachedObjectStorage_DiscISAM
   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_DiscISAM 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 $fileName = null;
  36  
  37      /**
  38       * File handle for this cache file
  39       *
  40       * @var resource
  41       */
  42      private $fileHandle = null;
  43  
  44      /**
  45       * Directory/Folder where the cache file is located
  46       *
  47       * @var string
  48       */
  49      private $cacheDirectory = null;
  50  
  51      /**
  52       * Store cell data in cache for the current cell object if it's "dirty",
  53       *     and the 'nullify' the current cell object
  54       *
  55       * @return    void
  56       * @throws    PHPExcel_Exception
  57       */
  58      protected function storeData()
  59      {
  60          if ($this->currentCellIsDirty && !empty($this->currentObjectID)) {
  61              $this->currentObject->detach();
  62  
  63              fseek($this->fileHandle, 0, SEEK_END);
  64  
  65              $this->cellCache[$this->currentObjectID] = array(
  66                  'ptr' => ftell($this->fileHandle),
  67                  'sz'  => fwrite($this->fileHandle, serialize($this->currentObject))
  68              );
  69              $this->currentCellIsDirty = false;
  70          }
  71          $this->currentObjectID = $this->currentObject = null;
  72      }
  73  
  74      /**
  75       * Add or Update a cell in cache identified by coordinate address
  76       *
  77       * @param    string            $pCoord        Coordinate address of the cell to update
  78       * @param    PHPExcel_Cell    $cell        Cell to update
  79       * @return    PHPExcel_Cell
  80       * @throws    PHPExcel_Exception
  81       */
  82      public function addCacheData($pCoord, PHPExcel_Cell $cell)
  83      {
  84          if (($pCoord !== $this->currentObjectID) && ($this->currentObjectID !== null)) {
  85              $this->storeData();
  86          }
  87  
  88          $this->currentObjectID = $pCoord;
  89          $this->currentObject = $cell;
  90          $this->currentCellIsDirty = true;
  91  
  92          return $cell;
  93      }
  94  
  95      /**
  96       * Get cell at a specific coordinate
  97       *
  98       * @param     string             $pCoord        Coordinate of the cell
  99       * @throws     PHPExcel_Exception
 100       * @return     PHPExcel_Cell     Cell that was found, or null if not found
 101       */
 102      public function getCacheData($pCoord)
 103      {
 104          if ($pCoord === $this->currentObjectID) {
 105              return $this->currentObject;
 106          }
 107          $this->storeData();
 108  
 109          //    Check if the entry that has been requested actually exists
 110          if (!isset($this->cellCache[$pCoord])) {
 111              //    Return null if requested entry doesn't exist in cache
 112              return null;
 113          }
 114  
 115          //    Set current entry to the requested entry
 116          $this->currentObjectID = $pCoord;
 117          fseek($this->fileHandle, $this->cellCache[$pCoord]['ptr']);
 118          $this->currentObject = unserialize(fread($this->fileHandle, $this->cellCache[$pCoord]['sz']));
 119          //    Re-attach this as the cell's parent
 120          $this->currentObject->attach($this);
 121  
 122          //    Return requested entry
 123          return $this->currentObject;
 124      }
 125  
 126      /**
 127       * Get a list of all cell addresses currently held in cache
 128       *
 129       * @return  string[]
 130       */
 131      public function getCellList()
 132      {
 133          if ($this->currentObjectID !== null) {
 134              $this->storeData();
 135          }
 136  
 137          return parent::getCellList();
 138      }
 139  
 140      /**
 141       * Clone the cell collection
 142       *
 143       * @param    PHPExcel_Worksheet    $parent        The new worksheet
 144       */
 145      public function copyCellCollection(PHPExcel_Worksheet $parent)
 146      {
 147          parent::copyCellCollection($parent);
 148          //    Get a new id for the new file name
 149          $baseUnique = $this->getUniqueID();
 150          $newFileName = $this->cacheDirectory.'/PHPExcel.'.$baseUnique.'.cache';
 151          //    Copy the existing cell cache file
 152          copy($this->fileName, $newFileName);
 153          $this->fileName = $newFileName;
 154          //    Open the copied cell cache file
 155          $this->fileHandle = fopen($this->fileName, 'a+');
 156      }
 157  
 158      /**
 159       * Clear the cell collection and disconnect from our parent
 160       *
 161       */
 162      public function unsetWorksheetCells()
 163      {
 164          if (!is_null($this->currentObject)) {
 165              $this->currentObject->detach();
 166              $this->currentObject = $this->currentObjectID = null;
 167          }
 168          $this->cellCache = array();
 169  
 170          //    detach ourself from the worksheet, so that it can then delete this object successfully
 171          $this->parent = null;
 172  
 173          //    Close down the temporary cache file
 174          $this->__destruct();
 175      }
 176  
 177      /**
 178       * Initialise this new cell collection
 179       *
 180       * @param    PHPExcel_Worksheet    $parent        The worksheet for this cell collection
 181       * @param    array of mixed        $arguments    Additional initialisation arguments
 182       */
 183      public function __construct(PHPExcel_Worksheet $parent, $arguments)
 184      {
 185          $this->cacheDirectory    = ((isset($arguments['dir'])) && ($arguments['dir'] !== null))
 186                                      ? $arguments['dir']
 187                                      : PHPExcel_Shared_File::sys_get_temp_dir();
 188  
 189          parent::__construct($parent);
 190          if (is_null($this->fileHandle)) {
 191              $baseUnique = $this->getUniqueID();
 192              $this->fileName = $this->cacheDirectory.'/PHPExcel.'.$baseUnique.'.cache';
 193              $this->fileHandle = fopen($this->fileName, 'a+');
 194          }
 195      }
 196  
 197      /**
 198       * Destroy this cell collection
 199       */
 200      public function __destruct()
 201      {
 202          if (!is_null($this->fileHandle)) {
 203              fclose($this->fileHandle);
 204              unlink($this->fileName);
 205          }
 206          $this->fileHandle = null;
 207      }
 208  }


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