ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
PHPTemp.php
Go to the documentation of this file.
1 <?php
37 
38  private $_fileHandle = null;
39 
40 
41  private $_memoryCacheSize = null;
42 
43  private function _storeData() {
44  $this->_currentObject->detach();
45 
46  fseek($this->_fileHandle,0,SEEK_END);
47  $offset = ftell($this->_fileHandle);
48  fwrite($this->_fileHandle, serialize($this->_currentObject));
49  $this->_cellCache[$this->_currentObjectID] = array('ptr' => $offset,
50  'sz' => ftell($this->_fileHandle) - $offset
51  );
52  $this->_currentObjectID = $this->_currentObject = null;
53  } // function _storeData()
54 
55 
64  public function addCacheData($pCoord, PHPExcel_Cell $cell) {
65  if (($pCoord !== $this->_currentObjectID) && ($this->_currentObjectID !== null)) {
66  $this->_storeData();
67  }
68 
69  $this->_currentObjectID = $pCoord;
70  $this->_currentObject = $cell;
71 
72  return $cell;
73  } // function addCacheData()
74 
75 
83  public function getCacheData($pCoord) {
84  if ($pCoord === $this->_currentObjectID) {
85  return $this->_currentObject;
86  }
87  $this->_storeData();
88 
89  // Check if the entry that has been requested actually exists
90  if (!isset($this->_cellCache[$pCoord])) {
91  // Return null if requested entry doesn't exist in cache
92  return null;
93  }
94 
95  // Set current entry to the requested entry
96  $this->_currentObjectID = $pCoord;
97  fseek($this->_fileHandle,$this->_cellCache[$pCoord]['ptr']);
98  $this->_currentObject = unserialize(fread($this->_fileHandle,$this->_cellCache[$pCoord]['sz']));
99  // Re-attach the parent worksheet
100  $this->_currentObject->attach($this->_parent);
101 
102  // Return requested entry
103  return $this->_currentObject;
104  } // function getCacheData()
105 
106 
112  public function copyCellCollection(PHPExcel_Worksheet $parent) {
114  // Open a new stream for the cell cache data
115  $newFileHandle = fopen('php://temp/maxmemory:'.$this->_memoryCacheSize,'a+');
116  // Copy the existing cell cache data to the new stream
117  fseek($this->_fileHandle,0);
118  while (!feof($this->_fileHandle)) {
119  fwrite($newFileHandle,fread($this->_fileHandle, 1024));
120  }
121  $this->_fileHandle = $newFileHandle;
122  } // function copyCellCollection()
123 
124 
125  public function unsetWorksheetCells() {
126  if(!is_null($this->_currentObject)) {
127  $this->_currentObject->detach();
128  $this->_currentObject = $this->_currentObjectID = null;
129  }
130  $this->_cellCache = array();
131 
132  // detach ourself from the worksheet, so that it can then delete this object successfully
133  $this->_parent = null;
134 
135  // Close down the php://temp file
136  $this->__destruct();
137  } // function unsetWorksheetCells()
138 
139 
140  public function __construct(PHPExcel_Worksheet $parent, $memoryCacheSize = '1MB') {
141  $this->_memoryCacheSize = (isset($arguments['memoryCacheSize'])) ? $arguments['memoryCacheSize'] : '1MB';
142 
143  parent::__construct($parent);
144  if (is_null($this->_fileHandle)) {
145  $this->_fileHandle = fopen('php://temp/maxmemory:'.$this->_memoryCacheSize,'a+');
146  }
147  } // function __construct()
148 
149 
150  public function __destruct() {
151  if (!is_null($this->_fileHandle)) {
152  fclose($this->_fileHandle);
153  }
154  $this->_fileHandle = null;
155  } // function __destruct()
156 
157 }