PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/common/libraries/plugin/phpexcel/PHPExcel/CachedObjectStorage/DiscISAM.php

https://bitbucket.org/chamilo/chamilo-dev/
PHP | 170 lines | 83 code | 25 blank | 62 comment | 7 complexity | 864bae14916af1b771a0a9f42622c70d MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, LGPL-3.0, GPL-3.0, MIT
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2011 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 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.6, 2011-02-27
  26. */
  27. /**
  28. * PHPExcel_CachedObjectStorage_DiscISAM
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_CachedObjectStorage
  32. * @copyright Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  33. */
  34. class PHPExcel_CachedObjectStorage_DiscISAM extends PHPExcel_CachedObjectStorage_CacheBase implements
  35. PHPExcel_CachedObjectStorage_ICache
  36. {
  37. private $_fileName = null;
  38. private $_fileHandle = null;
  39. private function _storeData()
  40. {
  41. $this->_currentObject->detach();
  42. fseek($this->_fileHandle, 0, SEEK_END);
  43. $offset = ftell($this->_fileHandle);
  44. fwrite($this->_fileHandle, serialize($this->_currentObject));
  45. $this->_cellCache[$this->_currentObjectID] = array('ptr' => $offset,
  46. 'sz' => ftell($this->_fileHandle) - $offset);
  47. $this->_currentObjectID = $this->_currentObject = null;
  48. } // function _storeData()
  49. /**
  50. * Add or Update a cell in cache identified by coordinate address
  51. *
  52. * @param string $pCoord Coordinate address of the cell to update
  53. * @param PHPExcel_Cell $cell Cell to update
  54. * @return void
  55. * @throws Exception
  56. */
  57. public function addCacheData($pCoord, PHPExcel_Cell $cell)
  58. {
  59. if (($pCoord !== $this->_currentObjectID) && ($this->_currentObjectID !== null))
  60. {
  61. $this->_storeData();
  62. }
  63. $this->_currentObjectID = $pCoord;
  64. $this->_currentObject = $cell;
  65. return $cell;
  66. } // function addCacheData()
  67. /**
  68. * Get cell at a specific coordinate
  69. *
  70. * @param string $pCoord Coordinate of the cell
  71. * @throws Exception
  72. * @return PHPExcel_Cell Cell that was found, or null if not found
  73. */
  74. public function getCacheData($pCoord)
  75. {
  76. if ($pCoord === $this->_currentObjectID)
  77. {
  78. return $this->_currentObject;
  79. }
  80. $this->_storeData();
  81. // Check if the entry that has been requested actually exists
  82. if (! isset($this->_cellCache[$pCoord]))
  83. {
  84. // Return null if requested entry doesn't exist in cache
  85. return null;
  86. }
  87. // Set current entry to the requested entry
  88. $this->_currentObjectID = $pCoord;
  89. fseek($this->_fileHandle, $this->_cellCache[$pCoord]['ptr']);
  90. $this->_currentObject = unserialize(fread($this->_fileHandle, $this->_cellCache[$pCoord]['sz']));
  91. // Re-attach the parent worksheet
  92. $this->_currentObject->attach($this->_parent);
  93. // Return requested entry
  94. return $this->_currentObject;
  95. } // function getCacheData()
  96. /**
  97. * Clone the cell collection
  98. *
  99. * @return void
  100. */
  101. public function copyCellCollection(PHPExcel_Worksheet $parent)
  102. {
  103. parent :: copyCellCollection($parent);
  104. // Get a new id for the new file name
  105. $baseUnique = $this->_getUniqueID();
  106. $newFileName = PHPExcel_Shared_File :: sys_get_temp_dir() . '/PHPExcel.' . $baseUnique . '.cache';
  107. // Copy the existing cell cache file
  108. copy($this->_fileName, $newFileName);
  109. $this->_fileName = $newFileName;
  110. // Open the copied cell cache file
  111. $this->_fileHandle = fopen($this->_fileName, 'a+');
  112. } // function copyCellCollection()
  113. public function unsetWorksheetCells()
  114. {
  115. if (! is_null($this->_currentObject))
  116. {
  117. $this->_currentObject->detach();
  118. $this->_currentObject = $this->_currentObjectID = null;
  119. }
  120. $this->_cellCache = array();
  121. // detach ourself from the worksheet, so that it can then delete this object successfully
  122. $this->_parent = null;
  123. // Close down the temporary cache file
  124. $this->__destruct();
  125. } // function unsetWorksheetCells()
  126. public function __construct(PHPExcel_Worksheet $parent)
  127. {
  128. parent :: __construct($parent);
  129. if (is_null($this->_fileHandle))
  130. {
  131. $baseUnique = $this->_getUniqueID();
  132. $this->_fileName = PHPExcel_Shared_File :: sys_get_temp_dir() . '/PHPExcel.' . $baseUnique . '.cache';
  133. $this->_fileHandle = fopen($this->_fileName, 'a+');
  134. }
  135. } // function __construct()
  136. public function __destruct()
  137. {
  138. if (! is_null($this->_fileHandle))
  139. {
  140. fclose($this->_fileHandle);
  141. unlink($this->_fileName);
  142. }
  143. $this->_fileHandle = null;
  144. } // function __destruct()
  145. }