PageRenderTime 26ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/phpexcel/Classes/PHPExcel/CachedObjectStorage/DiscISAM.php

https://github.com/Borluse/ProjetOption
PHP | 143 lines | 65 code | 24 blank | 54 comment | 9 complexity | 8ba4033dfb5fa8d081469e1b6737b707 MD5 | raw file
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2010 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 - 2010 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.3c, 2010-06-01
  26. */
  27. /**
  28. * PHPExcel_CachedObjectStorage_DiscISAM
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_CachedObjectStorage
  32. * @copyright Copyright (c) 2006 - 2010 PHPExcel (http://www.codeplex.com/PHPExcel)
  33. */
  34. class PHPExcel_CachedObjectStorage_DiscISAM extends PHPExcel_CachedObjectStorage_CacheBase implements PHPExcel_CachedObjectStorage_ICache {
  35. private $_fileName = null;
  36. private $_fileHandle = null;
  37. private function _storeData() {
  38. $this->_currentObject->detach();
  39. fseek($this->_fileHandle,0,SEEK_END);
  40. $offset = ftell($this->_fileHandle);
  41. fwrite($this->_fileHandle, serialize($this->_currentObject));
  42. $this->_cellCache[$this->_currentObjectID] = array('ptr' => $offset,
  43. 'sz' => ftell($this->_fileHandle) - $offset
  44. );
  45. $this->_currentObjectID = $this->_currentObject = null;
  46. } // function _storeData()
  47. /**
  48. * Add or Update a cell in cache identified by coordinate address
  49. *
  50. * @param string $pCoord Coordinate address of the cell to update
  51. * @param PHPExcel_Cell $cell Cell to update
  52. * @return void
  53. * @throws Exception
  54. */
  55. public function addCacheData($pCoord, PHPExcel_Cell $cell) {
  56. if (($pCoord !== $this->_currentObjectID) && ($this->_currentObjectID !== null)) {
  57. $this->_storeData();
  58. }
  59. $this->_currentObjectID = $pCoord;
  60. $this->_currentObject = $cell;
  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 Exception
  68. * @return PHPExcel_Cell Cell that was found, or null if not found
  69. */
  70. public function getCacheData($pCoord) {
  71. if ($pCoord === $this->_currentObjectID) {
  72. return $this->_currentObject;
  73. }
  74. $this->_storeData();
  75. // Check if the entry that has been requested actually exists
  76. if (!isset($this->_cellCache[$pCoord])) {
  77. // Return null if requested entry doesn't exist in cache
  78. return null;
  79. }
  80. // Set current entry to the requested entry
  81. $this->_currentObjectID = $pCoord;
  82. fseek($this->_fileHandle,$this->_cellCache[$pCoord]['ptr']);
  83. $this->_currentObject = unserialize(fread($this->_fileHandle,$this->_cellCache[$pCoord]['sz']));
  84. // Re-attach the parent worksheet
  85. $this->_currentObject->attach($this->_parent);
  86. // Return requested entry
  87. return $this->_currentObject;
  88. } // function getCacheData()
  89. public function unsetWorksheetCells() {
  90. if(!is_null($this->_currentObject)) {
  91. $this->_currentObject->detach();
  92. $this->_currentObject = $this->_currentObjectID = null;
  93. }
  94. $this->_cellCache = array();
  95. // detach ourself from the worksheet, so that it can then delete this object successfully
  96. $this->_parent = null;
  97. // Close down the temporary cache file
  98. $this->__destruct();
  99. } // function unsetWorksheetCells()
  100. public function __construct(PHPExcel_Worksheet $parent) {
  101. parent::__construct($parent);
  102. if (is_null($this->_fileHandle)) {
  103. if (function_exists('posix_getpid')) {
  104. $baseUnique = posix_getpid();
  105. } else {
  106. $baseUnique = mt_rand();
  107. }
  108. $this->_fileName = sys_get_temp_dir().'/PHPExcel.'.uniqid($baseUnique,true).'.cache';
  109. $this->_fileHandle = fopen($this->_fileName,'a+');
  110. }
  111. } // function __construct()
  112. public function __destruct() {
  113. if (!is_null($this->_fileHandle)) {
  114. fclose($this->_fileHandle);
  115. unlink($this->_fileName);
  116. }
  117. $this->_fileHandle = null;
  118. } // function __destruct()
  119. }