Igbinary.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /**
  3. * PHPExcel_CachedObjectStorage_Igbinary
  4. *
  5. * Copyright (c) 2006 - 2015 PHPExcel
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * @category PHPExcel
  22. * @package PHPExcel_CachedObjectStorage
  23. * @copyright Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version ##VERSION##, ##DATE##
  26. */
  27. class PHPExcel_CachedObjectStorage_Igbinary extends PHPExcel_CachedObjectStorage_CacheBase implements PHPExcel_CachedObjectStorage_ICache
  28. {
  29. /**
  30. * Store cell data in cache for the current cell object if it's "dirty",
  31. * and the 'nullify' the current cell object
  32. *
  33. * @return void
  34. * @throws PHPExcel_Exception
  35. */
  36. protected function storeData()
  37. {
  38. if ($this->currentCellIsDirty && !empty($this->currentObjectID)) {
  39. $this->currentObject->detach();
  40. $this->cellCache[$this->currentObjectID] = igbinary_serialize($this->currentObject);
  41. $this->currentCellIsDirty = false;
  42. }
  43. $this->currentObjectID = $this->currentObject = null;
  44. } // function _storeData()
  45. /**
  46. * Add or Update a cell in cache identified by coordinate address
  47. *
  48. * @param string $pCoord Coordinate address of the cell to update
  49. * @param PHPExcel_Cell $cell Cell to update
  50. * @return PHPExcel_Cell
  51. * @throws PHPExcel_Exception
  52. */
  53. public function addCacheData($pCoord, PHPExcel_Cell $cell)
  54. {
  55. if (($pCoord !== $this->currentObjectID) && ($this->currentObjectID !== null)) {
  56. $this->storeData();
  57. }
  58. $this->currentObjectID = $pCoord;
  59. $this->currentObject = $cell;
  60. $this->currentCellIsDirty = true;
  61. return $cell;
  62. } // function addCacheData()
  63. /**
  64. * Get cell at a specific coordinate
  65. *
  66. * @param string $pCoord Coordinate of the cell
  67. * @throws PHPExcel_Exception
  68. * @return PHPExcel_Cell Cell that was found, or null if not found
  69. */
  70. public function getCacheData($pCoord)
  71. {
  72. if ($pCoord === $this->currentObjectID) {
  73. return $this->currentObject;
  74. }
  75. $this->storeData();
  76. // Check if the entry that has been requested actually exists
  77. if (!isset($this->cellCache[$pCoord])) {
  78. // Return null if requested entry doesn't exist in cache
  79. return null;
  80. }
  81. // Set current entry to the requested entry
  82. $this->currentObjectID = $pCoord;
  83. $this->currentObject = igbinary_unserialize($this->cellCache[$pCoord]);
  84. // Re-attach this as the cell's parent
  85. $this->currentObject->attach($this);
  86. // Return requested entry
  87. return $this->currentObject;
  88. } // function getCacheData()
  89. /**
  90. * Get a list of all cell addresses currently held in cache
  91. *
  92. * @return string[]
  93. */
  94. public function getCellList()
  95. {
  96. if ($this->currentObjectID !== null) {
  97. $this->storeData();
  98. }
  99. return parent::getCellList();
  100. }
  101. /**
  102. * Clear the cell collection and disconnect from our parent
  103. *
  104. * @return void
  105. */
  106. public function unsetWorksheetCells()
  107. {
  108. if (!is_null($this->currentObject)) {
  109. $this->currentObject->detach();
  110. $this->currentObject = $this->currentObjectID = null;
  111. }
  112. $this->cellCache = array();
  113. // detach ourself from the worksheet, so that it can then delete this object successfully
  114. $this->parent = null;
  115. } // function unsetWorksheetCells()
  116. /**
  117. * Identify whether the caching method is currently available
  118. * Some methods are dependent on the availability of certain extensions being enabled in the PHP build
  119. *
  120. * @return boolean
  121. */
  122. public static function cacheMethodIsAvailable()
  123. {
  124. if (!function_exists('igbinary_serialize')) {
  125. return false;
  126. }
  127. return true;
  128. }
  129. }