[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * PHPExcel_CachedObjectStorage_APC
   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_APC extends PHPExcel_CachedObjectStorage_CacheBase implements PHPExcel_CachedObjectStorage_ICache
  29  {
  30      /**
  31       * Prefix used to uniquely identify cache data for this worksheet
  32       *
  33       * @access    private
  34       * @var string
  35       */
  36      private $cachePrefix = null;
  37  
  38      /**
  39       * Cache timeout
  40       *
  41       * @access    private
  42       * @var integer
  43       */
  44      private $cacheTime = 600;
  45  
  46      /**
  47       * Store cell data in cache for the current cell object if it's "dirty",
  48       *     and the 'nullify' the current cell object
  49       *
  50       * @access  private
  51       * @return  void
  52       * @throws  PHPExcel_Exception
  53       */
  54      protected function storeData()
  55      {
  56          if ($this->currentCellIsDirty && !empty($this->currentObjectID)) {
  57              $this->currentObject->detach();
  58  
  59              if (!apc_store(
  60                  $this->cachePrefix . $this->currentObjectID . '.cache',
  61                  serialize($this->currentObject),
  62                  $this->cacheTime
  63              )) {
  64                  $this->__destruct();
  65                  throw new PHPExcel_Exception('Failed to store cell ' . $this->currentObjectID . ' in APC');
  66              }
  67              $this->currentCellIsDirty = false;
  68          }
  69          $this->currentObjectID = $this->currentObject = null;
  70      }
  71  
  72      /**
  73       * Add or Update a cell in cache identified by coordinate address
  74       *
  75       * @access  public
  76       * @param   string         $pCoord  Coordinate address of the cell to update
  77       * @param   PHPExcel_Cell  $cell    Cell to update
  78       * @return  PHPExcel_Cell
  79       * @throws  PHPExcel_Exception
  80       */
  81      public function addCacheData($pCoord, PHPExcel_Cell $cell)
  82      {
  83          if (($pCoord !== $this->currentObjectID) && ($this->currentObjectID !== null)) {
  84              $this->storeData();
  85          }
  86          $this->cellCache[$pCoord] = true;
  87  
  88          $this->currentObjectID = $pCoord;
  89          $this->currentObject = $cell;
  90          $this->currentCellIsDirty = true;
  91  
  92          return $cell;
  93      }
  94  
  95      /**
  96       * Is a value set in the current PHPExcel_CachedObjectStorage_ICache for an indexed cell?
  97       *
  98       * @access  public
  99       * @param   string  $pCoord  Coordinate address of the cell to check
 100       * @throws  PHPExcel_Exception
 101       * @return  boolean
 102       */
 103      public function isDataSet($pCoord)
 104      {
 105          //    Check if the requested entry is the current object, or exists in the cache
 106          if (parent::isDataSet($pCoord)) {
 107              if ($this->currentObjectID == $pCoord) {
 108                  return true;
 109              }
 110              //    Check if the requested entry still exists in apc
 111              $success = apc_fetch($this->cachePrefix.$pCoord.'.cache');
 112              if ($success === false) {
 113                  //    Entry no longer exists in APC, so clear it from the cache array
 114                  parent::deleteCacheData($pCoord);
 115                  throw new PHPExcel_Exception('Cell entry '.$pCoord.' no longer exists in APC cache');
 116              }
 117              return true;
 118          }
 119          return false;
 120      }
 121  
 122      /**
 123       * Get cell at a specific coordinate
 124       *
 125       * @access  public
 126       * @param   string         $pCoord  Coordinate of the cell
 127       * @throws  PHPExcel_Exception
 128       * @return  PHPExcel_Cell  Cell that was found, or null if not found
 129       */
 130      public function getCacheData($pCoord)
 131      {
 132          if ($pCoord === $this->currentObjectID) {
 133              return $this->currentObject;
 134          }
 135          $this->storeData();
 136  
 137          //    Check if the entry that has been requested actually exists
 138          if (parent::isDataSet($pCoord)) {
 139              $obj = apc_fetch($this->cachePrefix . $pCoord . '.cache');
 140              if ($obj === false) {
 141                  //    Entry no longer exists in APC, so clear it from the cache array
 142                  parent::deleteCacheData($pCoord);
 143                  throw new PHPExcel_Exception('Cell entry '.$pCoord.' no longer exists in APC cache');
 144              }
 145          } else {
 146              //    Return null if requested entry doesn't exist in cache
 147              return null;
 148          }
 149  
 150          //    Set current entry to the requested entry
 151          $this->currentObjectID = $pCoord;
 152          $this->currentObject = unserialize($obj);
 153          //    Re-attach this as the cell's parent
 154          $this->currentObject->attach($this);
 155  
 156          //    Return requested entry
 157          return $this->currentObject;
 158      }
 159  
 160      /**
 161       * Get a list of all cell addresses currently held in cache
 162       *
 163       * @return  string[]
 164       */
 165      public function getCellList()
 166      {
 167          if ($this->currentObjectID !== null) {
 168              $this->storeData();
 169          }
 170  
 171          return parent::getCellList();
 172      }
 173  
 174      /**
 175       * Delete a cell in cache identified by coordinate address
 176       *
 177       * @access  public
 178       * @param   string  $pCoord  Coordinate address of the cell to delete
 179       * @throws  PHPExcel_Exception
 180       */
 181      public function deleteCacheData($pCoord)
 182      {
 183          //    Delete the entry from APC
 184          apc_delete($this->cachePrefix.$pCoord.'.cache');
 185  
 186          //    Delete the entry from our cell address array
 187          parent::deleteCacheData($pCoord);
 188      }
 189  
 190      /**
 191       * Clone the cell collection
 192       *
 193       * @access  public
 194       * @param   PHPExcel_Worksheet  $parent  The new worksheet
 195       * @throws  PHPExcel_Exception
 196       * @return  void
 197       */
 198      public function copyCellCollection(PHPExcel_Worksheet $parent)
 199      {
 200          parent::copyCellCollection($parent);
 201          //    Get a new id for the new file name
 202          $baseUnique = $this->getUniqueID();
 203          $newCachePrefix = substr(md5($baseUnique), 0, 8) . '.';
 204          $cacheList = $this->getCellList();
 205          foreach ($cacheList as $cellID) {
 206              if ($cellID != $this->currentObjectID) {
 207                  $obj = apc_fetch($this->cachePrefix . $cellID . '.cache');
 208                  if ($obj === false) {
 209                      //    Entry no longer exists in APC, so clear it from the cache array
 210                      parent::deleteCacheData($cellID);
 211                      throw new PHPExcel_Exception('Cell entry ' . $cellID . ' no longer exists in APC');
 212                  }
 213                  if (!apc_store($newCachePrefix . $cellID . '.cache', $obj, $this->cacheTime)) {
 214                      $this->__destruct();
 215                      throw new PHPExcel_Exception('Failed to store cell ' . $cellID . ' in APC');
 216                  }
 217              }
 218          }
 219          $this->cachePrefix = $newCachePrefix;
 220      }
 221  
 222      /**
 223       * Clear the cell collection and disconnect from our parent
 224       *
 225       * @return  void
 226       */
 227      public function unsetWorksheetCells()
 228      {
 229          if ($this->currentObject !== null) {
 230              $this->currentObject->detach();
 231              $this->currentObject = $this->currentObjectID = null;
 232          }
 233  
 234          //    Flush the APC cache
 235          $this->__destruct();
 236  
 237          $this->cellCache = array();
 238  
 239          //    detach ourself from the worksheet, so that it can then delete this object successfully
 240          $this->parent = null;
 241      }
 242  
 243      /**
 244       * Initialise this new cell collection
 245       *
 246       * @param  PHPExcel_Worksheet  $parent     The worksheet for this cell collection
 247       * @param  array of mixed      $arguments  Additional initialisation arguments
 248       */
 249      public function __construct(PHPExcel_Worksheet $parent, $arguments)
 250      {
 251          $cacheTime = (isset($arguments['cacheTime'])) ? $arguments['cacheTime'] : 600;
 252  
 253          if ($this->cachePrefix === null) {
 254              $baseUnique = $this->getUniqueID();
 255              $this->cachePrefix = substr(md5($baseUnique), 0, 8) . '.';
 256              $this->cacheTime = $cacheTime;
 257  
 258              parent::__construct($parent);
 259          }
 260      }
 261  
 262      /**
 263       * Destroy this cell collection
 264       */
 265      public function __destruct()
 266      {
 267          $cacheList = $this->getCellList();
 268          foreach ($cacheList as $cellID) {
 269              apc_delete($this->cachePrefix . $cellID . '.cache');
 270          }
 271      }
 272  
 273      /**
 274       * Identify whether the caching method is currently available
 275       * Some methods are dependent on the availability of certain extensions being enabled in the PHP build
 276       *
 277       * @return  boolean
 278       */
 279      public static function cacheMethodIsAvailable()
 280      {
 281          if (!function_exists('apc_store')) {
 282              return false;
 283          }
 284          if (apc_sma_info() === false) {
 285              return false;
 286          }
 287  
 288          return true;
 289      }
 290  }


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