diff --git a/Classes/PHPExcel/CachedObjectStorage/APC.php b/Classes/PHPExcel/CachedObjectStorage/APC.php index e25f3f0..b15a42e 100644 --- a/Classes/PHPExcel/CachedObjectStorage/APC.php +++ b/Classes/PHPExcel/CachedObjectStorage/APC.php @@ -67,8 +67,8 @@ class CachedObjectStorage_APC extends CachedObjectStorage_CacheBase implements C if (!apc_store( $this->cachePrefix.$this->currentObjectID.'.cache', serialize($this->currentObject), - $this->cacheTime) - ) { + $this->cacheTime + )) { $this->__destruct(); throw new Exception('Failed to store cell '.$this->currentObjectID.' in APC'); } @@ -268,7 +268,7 @@ class CachedObjectStorage_APC extends CachedObjectStorage_CacheBase implements C public function __destruct() { $cacheList = $this->getCellList(); - foreach($cacheList as $cellID) { + foreach ($cacheList as $cellID) { apc_delete($this->cachePrefix.$cellID.'.cache'); } } diff --git a/Classes/PHPExcel/CachedObjectStorage/CacheBase.php b/Classes/PHPExcel/CachedObjectStorage/CacheBase.php index b18b512..24b7b88 100644 --- a/Classes/PHPExcel/CachedObjectStorage/CacheBase.php +++ b/Classes/PHPExcel/CachedObjectStorage/CacheBase.php @@ -1,318 +1,318 @@ -parent = $parent; - } - - /** - * Return the parent worksheet for this cell collection - * - * @return PHPExcel\Worksheet - */ - public function getParent() - { - return $this->parent; - } - - /** - * Is a value set in the current PHPExcel\CachedObjectStorage_ICache for an indexed cell? - * - * @param string $pCoord Coordinate address of the cell to check - * @return boolean - */ - public function isDataSet($pCoord) - { - if ($pCoord === $this->currentObjectID) { - return true; - } - // Check if the requested entry exists in the cache - return isset($this->cellCache[$pCoord]); - } - - /** - * Move a cell object from one address to another - * - * @param string $fromAddress Current address of the cell to move - * @param string $toAddress Destination address of the cell to move - * @return boolean - */ - public function moveCell($fromAddress, $toAddress) - { - if ($fromAddress === $this->currentObjectID) { - $this->currentObjectID = $toAddress; - } - $this->currentCellIsDirty = true; - if (isset($this->cellCache[$fromAddress])) { - $this->cellCache[$toAddress] = &$this->cellCache[$fromAddress]; - unset($this->cellCache[$fromAddress]); - } - - return true; - } - - /** - * Add or Update a cell in cache - * - * @param PHPExcel\Cell $cell Cell to update - * @return void - * @throws PHPExcel\Exception - */ - public function updateCacheData(Cell $cell) - { - return $this->addCacheData($cell->getCoordinate(), $cell); - } - - /** - * Delete a cell in cache identified by coordinate address - * - * @param string $pCoord Coordinate address of the cell to delete - * @throws PHPExcel\Exception - */ - public function deleteCacheData($pCoord) - { - if ($pCoord === $this->currentObjectID) { - $this->currentObject->detach(); - $this->currentObjectID = $this->currentObject = null; - } - - if (is_object($this->cellCache[$pCoord])) { - $this->cellCache[$pCoord]->detach(); - unset($this->cellCache[$pCoord]); - } - $this->currentCellIsDirty = false; - } - - /** - * Get a list of all cell addresses currently held in cache - * - * @return array of string - */ - public function getCellList() - { - return array_keys($this->cellCache); - } - - /** - * Sort the list of all cell addresses currently held in cache by row and column - * - * @return void - */ - public function getSortedCellList() - { - $sortKeys = array(); - foreach ($this->getCellList() as $coord) { - sscanf($coord, '%[A-Z]%d', $column, $row); - $sortKeys[sprintf('%09d%3s', $row, $column)] = $coord; - } - ksort($sortKeys); - - return array_values($sortKeys); - } - - /** - * Get highest worksheet column and highest row that have cell records - * - * @return array Highest column name and highest row number - */ - public function getHighestRowAndColumn() - { - // Lookup highest column and highest row - $col = array('A' => '1A'); - $row = array(1); - foreach ($this->getCellList() as $coord) { - sscanf($coord, '%[A-Z]%d', $c, $r); - $row[$r] = $r; - $col[$c] = strlen($c).$c; - } - if (!empty($row)) { - // Determine highest column and row - $highestRow = max($row); - $highestColumn = substr(max($col), 1); - } - - return array( - 'row' => $highestRow, - 'column' => $highestColumn - ); - } - - /** - * Return the cell address of the currently active cell object - * - * @return string - */ - public function getCurrentAddress() - { - return $this->currentObjectID; - } - - /** - * Return the column address of the currently active cell object - * - * @return string - */ - public function getCurrentColumn() - { - sscanf($this->currentObjectID, '%[A-Z]%d', $column, $row); - return $column; - } - - /** - * Return the row address of the currently active cell object - * - * @return string - */ - public function getCurrentRow() - { - sscanf($this->currentObjectID, '%[A-Z]%d', $column, $row); - return $row; - } - - /** - * Get highest worksheet column - * - * @return string Highest column name - */ - public function getHighestColumn() - { - $colRow = $this->getHighestRowAndColumn(); - return $colRow['column']; - } - - /** - * Get highest worksheet row - * - * @return int Highest row number - */ - public function getHighestRow() - { - $colRow = $this->getHighestRowAndColumn(); - return $colRow['row']; - } - - /** - * Generate a unique ID for cache referencing - * - * @return string Unique Reference - */ - protected function getUniqueID() - { - if (function_exists('posix_getpid')) { - $baseUnique = posix_getpid(); - } else { - $baseUnique = mt_rand(); - } - return uniqid($baseUnique, true); - } - - /** - * Clone the cell collection - * - * @param PHPExcel\Worksheet $parent The new worksheet - * @return void - */ - public function copyCellCollection(Worksheet $parent) - { - $this->currentCellIsDirty; - $this->storeData(); - - $this->parent = $parent; - if (($this->currentObject !== null) && (is_object($this->currentObject))) { - $this->currentObject->attach($this); - } - } - - /** - * Identify whether the caching method is currently available - * Some methods are dependent on the availability of certain extensions being enabled in the PHP build - * - * @return boolean - */ - public static function cacheMethodIsAvailable() - { - return true; - } -} +parent = $parent; + } + + /** + * Return the parent worksheet for this cell collection + * + * @return PHPExcel\Worksheet + */ + public function getParent() + { + return $this->parent; + } + + /** + * Is a value set in the current PHPExcel\CachedObjectStorage_ICache for an indexed cell? + * + * @param string $pCoord Coordinate address of the cell to check + * @return boolean + */ + public function isDataSet($pCoord) + { + if ($pCoord === $this->currentObjectID) { + return true; + } + // Check if the requested entry exists in the cache + return isset($this->cellCache[$pCoord]); + } + + /** + * Move a cell object from one address to another + * + * @param string $fromAddress Current address of the cell to move + * @param string $toAddress Destination address of the cell to move + * @return boolean + */ + public function moveCell($fromAddress, $toAddress) + { + if ($fromAddress === $this->currentObjectID) { + $this->currentObjectID = $toAddress; + } + $this->currentCellIsDirty = true; + if (isset($this->cellCache[$fromAddress])) { + $this->cellCache[$toAddress] = &$this->cellCache[$fromAddress]; + unset($this->cellCache[$fromAddress]); + } + + return true; + } + + /** + * Add or Update a cell in cache + * + * @param PHPExcel\Cell $cell Cell to update + * @return void + * @throws PHPExcel\Exception + */ + public function updateCacheData(Cell $cell) + { + return $this->addCacheData($cell->getCoordinate(), $cell); + } + + /** + * Delete a cell in cache identified by coordinate address + * + * @param string $pCoord Coordinate address of the cell to delete + * @throws PHPExcel\Exception + */ + public function deleteCacheData($pCoord) + { + if ($pCoord === $this->currentObjectID) { + $this->currentObject->detach(); + $this->currentObjectID = $this->currentObject = null; + } + + if (is_object($this->cellCache[$pCoord])) { + $this->cellCache[$pCoord]->detach(); + unset($this->cellCache[$pCoord]); + } + $this->currentCellIsDirty = false; + } + + /** + * Get a list of all cell addresses currently held in cache + * + * @return array of string + */ + public function getCellList() + { + return array_keys($this->cellCache); + } + + /** + * Sort the list of all cell addresses currently held in cache by row and column + * + * @return void + */ + public function getSortedCellList() + { + $sortKeys = array(); + foreach ($this->getCellList() as $coord) { + sscanf($coord, '%[A-Z]%d', $column, $row); + $sortKeys[sprintf('%09d%3s', $row, $column)] = $coord; + } + ksort($sortKeys); + + return array_values($sortKeys); + } + + /** + * Get highest worksheet column and highest row that have cell records + * + * @return array Highest column name and highest row number + */ + public function getHighestRowAndColumn() + { + // Lookup highest column and highest row + $col = array('A' => '1A'); + $row = array(1); + foreach ($this->getCellList() as $coord) { + sscanf($coord, '%[A-Z]%d', $c, $r); + $row[$r] = $r; + $col[$c] = strlen($c).$c; + } + if (!empty($row)) { + // Determine highest column and row + $highestRow = max($row); + $highestColumn = substr(max($col), 1); + } + + return array( + 'row' => $highestRow, + 'column' => $highestColumn + ); + } + + /** + * Return the cell address of the currently active cell object + * + * @return string + */ + public function getCurrentAddress() + { + return $this->currentObjectID; + } + + /** + * Return the column address of the currently active cell object + * + * @return string + */ + public function getCurrentColumn() + { + sscanf($this->currentObjectID, '%[A-Z]%d', $column, $row); + return $column; + } + + /** + * Return the row address of the currently active cell object + * + * @return string + */ + public function getCurrentRow() + { + sscanf($this->currentObjectID, '%[A-Z]%d', $column, $row); + return $row; + } + + /** + * Get highest worksheet column + * + * @return string Highest column name + */ + public function getHighestColumn() + { + $colRow = $this->getHighestRowAndColumn(); + return $colRow['column']; + } + + /** + * Get highest worksheet row + * + * @return int Highest row number + */ + public function getHighestRow() + { + $colRow = $this->getHighestRowAndColumn(); + return $colRow['row']; + } + + /** + * Generate a unique ID for cache referencing + * + * @return string Unique Reference + */ + protected function getUniqueID() + { + if (function_exists('posix_getpid')) { + $baseUnique = posix_getpid(); + } else { + $baseUnique = mt_rand(); + } + return uniqid($baseUnique, true); + } + + /** + * Clone the cell collection + * + * @param PHPExcel\Worksheet $parent The new worksheet + * @return void + */ + public function copyCellCollection(Worksheet $parent) + { + $this->currentCellIsDirty; + $this->storeData(); + + $this->parent = $parent; + if (($this->currentObject !== null) && (is_object($this->currentObject))) { + $this->currentObject->attach($this); + } + } + + /** + * Identify whether the caching method is currently available + * Some methods are dependent on the availability of certain extensions being enabled in the PHP build + * + * @return boolean + */ + public static function cacheMethodIsAvailable() + { + return true; + } +} diff --git a/Classes/PHPExcel/CachedObjectStorage/DiscISAM.php b/Classes/PHPExcel/CachedObjectStorage/DiscISAM.php index 80bd204..5f17446 100644 --- a/Classes/PHPExcel/CachedObjectStorage/DiscISAM.php +++ b/Classes/PHPExcel/CachedObjectStorage/DiscISAM.php @@ -1,222 +1,222 @@ -currentCellIsDirty) { - $this->currentObject->detach(); - - fseek($this->fileHandle, 0, SEEK_END); - $offset = ftell($this->fileHandle); - fwrite($this->fileHandle, serialize($this->currentObject)); - $this->cellCache[$this->currentObjectID] = array( - 'ptr' => $offset, - 'sz' => ftell($this->fileHandle) - $offset - ); - $this->currentCellIsDirty = false; - } - $this->currentObjectID = $this->currentObject = null; - } - - /** - * Add or Update a cell in cache identified by coordinate address - * - * @param string $pCoord Coordinate address of the cell to update - * @param PHPExcel\Cell $cell Cell to update - * @return void - * @throws PHPExcel\Exception - */ - public function addCacheData($pCoord, Cell $cell) - { - if (($pCoord !== $this->currentObjectID) && ($this->currentObjectID !== null)) { - $this->storeData(); - } - - $this->currentObjectID = $pCoord; - $this->currentObject = $cell; - $this->currentCellIsDirty = true; - - return $cell; - } - - /** - * Get cell at a specific coordinate - * - * @param string $pCoord Coordinate of the cell - * @throws PHPExcel\Exception - * @return PHPExcel\Cell Cell that was found, or null if not found - */ - public function getCacheData($pCoord) - { - if ($pCoord === $this->currentObjectID) { - return $this->currentObject; - } - $this->storeData(); - - // Check if the entry that has been requested actually exists - if (!isset($this->cellCache[$pCoord])) { - // Return null if requested entry doesn't exist in cache - return null; - } - - // Set current entry to the requested entry - $this->currentObjectID = $pCoord; - fseek($this->fileHandle, $this->cellCache[$pCoord]['ptr']); - $this->currentObject = unserialize(fread($this->fileHandle, $this->cellCache[$pCoord]['sz'])); - // Re-attach this as the cell's parent - $this->currentObject->attach($this); - - // Return requested entry - return $this->currentObject; - } - - /** - * Get a list of all cell addresses currently held in cache - * - * @return array of string - */ - public function getCellList() - { - if ($this->currentObjectID !== null) { - $this->storeData(); - } - - return parent::getCellList(); - } - - /** - * Clone the cell collection - * - * @param PHPExcel\Worksheet $parent The new worksheet - * @return void - */ - public function copyCellCollection(Worksheet $parent) - { - parent::copyCellCollection($parent); - // Get a new id for the new file name - $baseUnique = $this->getUniqueID(); - $newFileName = $this->cacheDirectory.'/PHPExcel.'.$baseUnique.'.cache'; - // Copy the existing cell cache file - copy ($this->fileName, $newFileName); - $this->fileName = $newFileName; - // Open the copied cell cache file - $this->fileHandle = fopen($this->fileName, 'a+'); - } - - /** - * Clear the cell collection and disconnect from our parent - * - * @return void - */ - public function unsetWorksheetCells() - { - if (!is_null($this->currentObject)) { - $this->currentObject->detach(); - $this->currentObject = $this->currentObjectID = null; - } - $this->cellCache = array(); - - // detach ourself from the worksheet, so that it can then delete this object successfully - $this->parent = null; - - // Close down the temporary cache file - $this->__destruct(); - } - - /** - * Initialise this new cell collection - * - * @param PHPExcel\Worksheet $parent The worksheet for this cell collection - * @param array of mixed $arguments Additional initialisation arguments - */ - public function __construct(Worksheet $parent, $arguments) - { - $this->cacheDirectory = ((isset($arguments['dir'])) && ($arguments['dir'] !== null)) - ? $arguments['dir'] - : Shared_File::sys_get_temp_dir(); - - parent::__construct($parent); - if (is_null($this->fileHandle)) { - $baseUnique = $this->getUniqueID(); - $this->fileName = $this->cacheDirectory.'/PHPExcel.'.$baseUnique.'.cache'; - $this->fileHandle = fopen($this->fileName, 'a+'); - } - } - - /** - * Destroy this cell collection - */ - public function __destruct() - { - if (!is_null($this->fileHandle)) { - fclose($this->fileHandle); - unlink($this->fileName); - } - $this->fileHandle = null; - } -} +currentCellIsDirty) { + $this->currentObject->detach(); + + fseek($this->fileHandle, 0, SEEK_END); + $offset = ftell($this->fileHandle); + fwrite($this->fileHandle, serialize($this->currentObject)); + $this->cellCache[$this->currentObjectID] = array( + 'ptr' => $offset, + 'sz' => ftell($this->fileHandle) - $offset + ); + $this->currentCellIsDirty = false; + } + $this->currentObjectID = $this->currentObject = null; + } + + /** + * Add or Update a cell in cache identified by coordinate address + * + * @param string $pCoord Coordinate address of the cell to update + * @param PHPExcel\Cell $cell Cell to update + * @return void + * @throws PHPExcel\Exception + */ + public function addCacheData($pCoord, Cell $cell) + { + if (($pCoord !== $this->currentObjectID) && ($this->currentObjectID !== null)) { + $this->storeData(); + } + + $this->currentObjectID = $pCoord; + $this->currentObject = $cell; + $this->currentCellIsDirty = true; + + return $cell; + } + + /** + * Get cell at a specific coordinate + * + * @param string $pCoord Coordinate of the cell + * @throws PHPExcel\Exception + * @return PHPExcel\Cell Cell that was found, or null if not found + */ + public function getCacheData($pCoord) + { + if ($pCoord === $this->currentObjectID) { + return $this->currentObject; + } + $this->storeData(); + + // Check if the entry that has been requested actually exists + if (!isset($this->cellCache[$pCoord])) { + // Return null if requested entry doesn't exist in cache + return null; + } + + // Set current entry to the requested entry + $this->currentObjectID = $pCoord; + fseek($this->fileHandle, $this->cellCache[$pCoord]['ptr']); + $this->currentObject = unserialize(fread($this->fileHandle, $this->cellCache[$pCoord]['sz'])); + // Re-attach this as the cell's parent + $this->currentObject->attach($this); + + // Return requested entry + return $this->currentObject; + } + + /** + * Get a list of all cell addresses currently held in cache + * + * @return array of string + */ + public function getCellList() + { + if ($this->currentObjectID !== null) { + $this->storeData(); + } + + return parent::getCellList(); + } + + /** + * Clone the cell collection + * + * @param PHPExcel\Worksheet $parent The new worksheet + * @return void + */ + public function copyCellCollection(Worksheet $parent) + { + parent::copyCellCollection($parent); + // Get a new id for the new file name + $baseUnique = $this->getUniqueID(); + $newFileName = $this->cacheDirectory.'/PHPExcel.'.$baseUnique.'.cache'; + // Copy the existing cell cache file + copy($this->fileName, $newFileName); + $this->fileName = $newFileName; + // Open the copied cell cache file + $this->fileHandle = fopen($this->fileName, 'a+'); + } + + /** + * Clear the cell collection and disconnect from our parent + * + * @return void + */ + public function unsetWorksheetCells() + { + if (!is_null($this->currentObject)) { + $this->currentObject->detach(); + $this->currentObject = $this->currentObjectID = null; + } + $this->cellCache = array(); + + // detach ourself from the worksheet, so that it can then delete this object successfully + $this->parent = null; + + // Close down the temporary cache file + $this->__destruct(); + } + + /** + * Initialise this new cell collection + * + * @param PHPExcel\Worksheet $parent The worksheet for this cell collection + * @param array of mixed $arguments Additional initialisation arguments + */ + public function __construct(Worksheet $parent, $arguments) + { + $this->cacheDirectory = ((isset($arguments['dir'])) && ($arguments['dir'] !== null)) + ? $arguments['dir'] + : Shared_File::sys_get_temp_dir(); + + parent::__construct($parent); + if (is_null($this->fileHandle)) { + $baseUnique = $this->getUniqueID(); + $this->fileName = $this->cacheDirectory.'/PHPExcel.'.$baseUnique.'.cache'; + $this->fileHandle = fopen($this->fileName, 'a+'); + } + } + + /** + * Destroy this cell collection + */ + public function __destruct() + { + if (!is_null($this->fileHandle)) { + fclose($this->fileHandle); + unlink($this->fileName); + } + $this->fileHandle = null; + } +} diff --git a/Classes/PHPExcel/CachedObjectStorage/Igbinary.php b/Classes/PHPExcel/CachedObjectStorage/Igbinary.php index bd61415..570ad5d 100644 --- a/Classes/PHPExcel/CachedObjectStorage/Igbinary.php +++ b/Classes/PHPExcel/CachedObjectStorage/Igbinary.php @@ -1,150 +1,150 @@ -currentCellIsDirty) { - $this->currentObject->detach(); - - $this->cellCache[$this->currentObjectID] = igbinary_serialize($this->currentObject); - $this->currentCellIsDirty = false; - } - $this->currentObjectID = $this->currentObject = null; - } - - /** - * Add or Update a cell in cache identified by coordinate address - * - * @param string $pCoord Coordinate address of the cell to update - * @param PHPExcel\Cell $cell Cell to update - * @return void - * @throws PHPExcel\Exception - */ - public function addCacheData($pCoord, Cell $cell) - { - if (($pCoord !== $this->currentObjectID) && ($this->currentObjectID !== null)) { - $this->storeData(); - } - - $this->currentObjectID = $pCoord; - $this->currentObject = $cell; - $this->currentCellIsDirty = true; - - return $cell; - } - - /** - * Get cell at a specific coordinate - * - * @param string $pCoord Coordinate of the cell - * @throws PHPExcel\Exception - * @return PHPExcel\Cell Cell that was found, or null if not found - */ - public function getCacheData($pCoord) - { - if ($pCoord === $this->currentObjectID) { - return $this->currentObject; - } - $this->storeData(); - - // Check if the entry that has been requested actually exists - if (!isset($this->cellCache[$pCoord])) { - // Return null if requested entry doesn't exist in cache - return null; - } - - // Set current entry to the requested entry - $this->currentObjectID = $pCoord; - $this->currentObject = igbinary_unserialize($this->cellCache[$pCoord]); - // Re-attach this as the cell's parent - $this->currentObject->attach($this); - - // Return requested entry - return $this->currentObject; - } - - /** - * Get a list of all cell addresses currently held in cache - * - * @return array of string - */ - public function getCellList() - { - if ($this->currentObjectID !== null) { - $this->storeData(); - } - - return parent::getCellList(); - } - - /** - * Clear the cell collection and disconnect from our parent - * - * @return void - */ - public function unsetWorksheetCells() - { - if (!is_null($this->currentObject)) { - $this->currentObject->detach(); - $this->currentObject = $this->currentObjectID = null; - } - $this->cellCache = array(); - - // detach ourself from the worksheet, so that it can then delete this object successfully - $this->parent = null; - } - - /** - * Identify whether the caching method is currently available - * Some methods are dependent on the availability of certain extensions being enabled in the PHP build - * - * @return boolean - */ - public static function cacheMethodIsAvailable() - { - return function_exists('igbinary_serialize'); - } -} +currentCellIsDirty) { + $this->currentObject->detach(); + + $this->cellCache[$this->currentObjectID] = igbinary_serialize($this->currentObject); + $this->currentCellIsDirty = false; + } + $this->currentObjectID = $this->currentObject = null; + } + + /** + * Add or Update a cell in cache identified by coordinate address + * + * @param string $pCoord Coordinate address of the cell to update + * @param PHPExcel\Cell $cell Cell to update + * @return void + * @throws PHPExcel\Exception + */ + public function addCacheData($pCoord, Cell $cell) + { + if (($pCoord !== $this->currentObjectID) && ($this->currentObjectID !== null)) { + $this->storeData(); + } + + $this->currentObjectID = $pCoord; + $this->currentObject = $cell; + $this->currentCellIsDirty = true; + + return $cell; + } + + /** + * Get cell at a specific coordinate + * + * @param string $pCoord Coordinate of the cell + * @throws PHPExcel\Exception + * @return PHPExcel\Cell Cell that was found, or null if not found + */ + public function getCacheData($pCoord) + { + if ($pCoord === $this->currentObjectID) { + return $this->currentObject; + } + $this->storeData(); + + // Check if the entry that has been requested actually exists + if (!isset($this->cellCache[$pCoord])) { + // Return null if requested entry doesn't exist in cache + return null; + } + + // Set current entry to the requested entry + $this->currentObjectID = $pCoord; + $this->currentObject = igbinary_unserialize($this->cellCache[$pCoord]); + // Re-attach this as the cell's parent + $this->currentObject->attach($this); + + // Return requested entry + return $this->currentObject; + } + + /** + * Get a list of all cell addresses currently held in cache + * + * @return array of string + */ + public function getCellList() + { + if ($this->currentObjectID !== null) { + $this->storeData(); + } + + return parent::getCellList(); + } + + /** + * Clear the cell collection and disconnect from our parent + * + * @return void + */ + public function unsetWorksheetCells() + { + if (!is_null($this->currentObject)) { + $this->currentObject->detach(); + $this->currentObject = $this->currentObjectID = null; + } + $this->cellCache = array(); + + // detach ourself from the worksheet, so that it can then delete this object successfully + $this->parent = null; + } + + /** + * Identify whether the caching method is currently available + * Some methods are dependent on the availability of certain extensions being enabled in the PHP build + * + * @return boolean + */ + public static function cacheMethodIsAvailable() + { + return function_exists('igbinary_serialize'); + } +} diff --git a/Classes/PHPExcel/CachedObjectStorage/Memcache.php b/Classes/PHPExcel/CachedObjectStorage/Memcache.php index fa93050..4127e5a 100644 --- a/Classes/PHPExcel/CachedObjectStorage/Memcache.php +++ b/Classes/PHPExcel/CachedObjectStorage/Memcache.php @@ -1,333 +1,333 @@ -currentCellIsDirty) { - $this->currentObject->detach(); - - $obj = serialize($this->currentObject); - if (!$this->memcache->replace( - $this->cachePrefix.$this->currentObjectID.'.cache', - $obj, - null, - $this->cacheTime - )) { - if (!$this->memcache->add($this->cachePrefix.$this->currentObjectID.'.cache', - $obj, - null, - $this->cacheTime - )) { - $this->__destruct(); - throw new Exception('Failed to store cell '.$this->currentObjectID.' in MemCache'); - } - } - $this->currentCellIsDirty = false; - } - $this->currentObjectID = $this->currentObject = null; - } - - /** - * Add or Update a cell in cache identified by coordinate address - * - * @param string $pCoord Coordinate address of the cell to update - * @param PHPExcel\Cell $cell Cell to update - * @return void - * @throws PHPExcel\Exception - */ - public function addCacheData($pCoord, Cell $cell) - { - if (($pCoord !== $this->currentObjectID) && ($this->currentObjectID !== null)) { - $this->storeData(); - } - $this->cellCache[$pCoord] = true; - - $this->currentObjectID = $pCoord; - $this->currentObject = $cell; - $this->currentCellIsDirty = true; - - return $cell; - } - - /** - * Is a value set in the current PHPExcel\CachedObjectStorage_ICache for an indexed cell? - * - * @param string $pCoord Coordinate address of the cell to check - * @return void - * @return boolean - */ - public function isDataSet($pCoord) - { - // Check if the requested entry is the current object, or exists in the cache - if (parent::isDataSet($pCoord)) { - if ($this->currentObjectID == $pCoord) { - return true; - } - // Check if the requested entry still exists in Memcache - $success = $this->memcache->get($this->cachePrefix.$pCoord.'.cache'); - if ($success === false) { - // Entry no longer exists in Memcache, so clear it from the cache array - parent::deleteCacheData($pCoord); - throw new Exception('Cell entry '.$pCoord.' no longer exists in MemCache'); - } - return true; - } - return false; - } - - /** - * Get cell at a specific coordinate - * - * @param string $pCoord Coordinate of the cell - * @throws PHPExcel\Exception - * @return PHPExcel\Cell Cell that was found, or null if not found - */ - public function getCacheData($pCoord) - { - if ($pCoord === $this->currentObjectID) - { - return $this->currentObject; - } - $this->storeData(); - - // Check if the entry that has been requested actually exists - if (parent::isDataSet($pCoord)) { - $obj = $this->memcache->get($this->cachePrefix.$pCoord.'.cache'); - if ($obj === false) { - // Entry no longer exists in Memcache, so clear it from the cache array - parent::deleteCacheData($pCoord); - throw new Exception('Cell entry '.$pCoord.' no longer exists in MemCache'); - } - } else { - // Return null if requested entry doesn't exist in cache - return null; - } - - // Set current entry to the requested entry - $this->currentObjectID = $pCoord; - $this->currentObject = unserialize($obj); - // Re-attach this as the cell's parent - $this->currentObject->attach($this); - - // Return requested entry - return $this->currentObject; - } - - /** - * Get a list of all cell addresses currently held in cache - * - * @return array of string - */ - public function getCellList() - { - if ($this->currentObjectID !== null) { - $this->storeData(); - } - - return parent::getCellList(); - } - - /** - * Delete a cell in cache identified by coordinate address - * - * @param string $pCoord Coordinate address of the cell to delete - * @throws PHPExcel\Exception - */ - public function deleteCacheData($pCoord) - { - // Delete the entry from Memcache - $this->memcache->delete($this->cachePrefix.$pCoord.'.cache'); - - // Delete the entry from our cell address array - parent::deleteCacheData($pCoord); - } - - /** - * Clone the cell collection - * - * @param PHPExcel\Worksheet $parent The new worksheet - * @throws PHPExcel\Exception - * @return void - */ - public function copyCellCollection(Worksheet $parent) - { - parent::copyCellCollection($parent); - // Get a new id for the new file name - $baseUnique = $this->getUniqueID(); - $newCachePrefix = substr(md5($baseUnique), 0, 8).'.'; - $cacheList = $this->getCellList(); - foreach ($cacheList as $cellID) { - if ($cellID != $this->currentObjectID) { - $obj = $this->memcache->get($this->cachePrefix.$cellID.'.cache'); - if ($obj === false) { - // Entry no longer exists in Memcache, so clear it from the cache array - parent::deleteCacheData($cellID); - throw new Exception('Cell entry '.$cellID.' no longer exists in MemCache'); - } - if (!$this->memcache->add($newCachePrefix.$cellID.'.cache', $obj, null, $this->cacheTime)) { - $this->__destruct(); - throw new Exception('Failed to store cell '.$cellID.' in MemCache'); - } - } - } - $this->cachePrefix = $newCachePrefix; - } - - /** - * Clear the cell collection and disconnect from our parent - * - * @return void - */ - public function unsetWorksheetCells() - { - if (!is_null($this->currentObject)) { - $this->currentObject->detach(); - $this->currentObject = $this->currentObjectID = null; - } - - // Flush the Memcache cache - $this->__destruct(); - - $this->cellCache = array(); - - // detach ourself from the worksheet, so that it can then delete this object successfully - $this->parent = null; - } - - /** - * Initialise this new cell collection - * - * @param PHPExcel\Worksheet $parent The worksheet for this cell collection - * @param array of mixed $arguments Additional initialisation arguments - */ - public function __construct(Worksheet $parent, $arguments) - { - $memcacheServer = (isset($arguments['memcacheServer'])) ? $arguments['memcacheServer'] : 'localhost'; - $memcachePort = (isset($arguments['memcachePort'])) ? $arguments['memcachePort'] : 11211; - $cacheTime = (isset($arguments['cacheTime'])) ? $arguments['cacheTime'] : 600; - - if (is_null($this->cachePrefix)) { - $baseUnique = $this->getUniqueID(); - $this->cachePrefix = substr(md5($baseUnique), 0, 8).'.'; - - // Set a new Memcache object and connect to the Memcache server - $this->memcache = new Memcache(); - if (!$this->memcache->addServer( - $memcacheServer, - $memcachePort, - false, - 50, - 5, - 5, - true, - array($this, 'failureCallback') - )) { - throw new Exception( - 'Could not connect to MemCache server at '.$memcacheServer.':'.$memcachePort - ); - } - $this->cacheTime = $cacheTime; - - parent::__construct($parent); - } - } - - /** - * Memcache error handler - * - * @param string $host Memcache server - * @param integer $port Memcache port - * @throws PHPExcel\Exception - */ - public function failureCallback($host, $port) - { - throw new Exception('memcache '.$host.':'.$port.' failed'); - } - - /** - * Destroy this cell collection - */ - public function __destruct() - { - $cacheList = $this->getCellList(); - foreach ($cacheList as $cellID) { - $this->memcache->delete($this->cachePrefix.$cellID.'.cache'); - } - } - - /** - * Identify whether the caching method is currently available - * Some methods are dependent on the availability of certain extensions being enabled in the PHP build - * - * @return boolean - */ - public static function cacheMethodIsAvailable() - { - return function_exists('memcache_add'); - } -} +currentCellIsDirty) { + $this->currentObject->detach(); + + $obj = serialize($this->currentObject); + if (!$this->memcache->replace( + $this->cachePrefix.$this->currentObjectID.'.cache', + $obj, + null, + $this->cacheTime + )) { + if (!$this->memcache->add( + $this->cachePrefix.$this->currentObjectID.'.cache', + $obj, + null, + $this->cacheTime + )) { + $this->__destruct(); + throw new Exception('Failed to store cell '.$this->currentObjectID.' in MemCache'); + } + } + $this->currentCellIsDirty = false; + } + $this->currentObjectID = $this->currentObject = null; + } + + /** + * Add or Update a cell in cache identified by coordinate address + * + * @param string $pCoord Coordinate address of the cell to update + * @param PHPExcel\Cell $cell Cell to update + * @return void + * @throws PHPExcel\Exception + */ + public function addCacheData($pCoord, Cell $cell) + { + if (($pCoord !== $this->currentObjectID) && ($this->currentObjectID !== null)) { + $this->storeData(); + } + $this->cellCache[$pCoord] = true; + + $this->currentObjectID = $pCoord; + $this->currentObject = $cell; + $this->currentCellIsDirty = true; + + return $cell; + } + + /** + * Is a value set in the current PHPExcel\CachedObjectStorage_ICache for an indexed cell? + * + * @param string $pCoord Coordinate address of the cell to check + * @return void + * @return boolean + */ + public function isDataSet($pCoord) + { + // Check if the requested entry is the current object, or exists in the cache + if (parent::isDataSet($pCoord)) { + if ($this->currentObjectID == $pCoord) { + return true; + } + // Check if the requested entry still exists in Memcache + $success = $this->memcache->get($this->cachePrefix.$pCoord.'.cache'); + if ($success === false) { + // Entry no longer exists in Memcache, so clear it from the cache array + parent::deleteCacheData($pCoord); + throw new Exception('Cell entry '.$pCoord.' no longer exists in MemCache'); + } + return true; + } + return false; + } + + /** + * Get cell at a specific coordinate + * + * @param string $pCoord Coordinate of the cell + * @throws PHPExcel\Exception + * @return PHPExcel\Cell Cell that was found, or null if not found + */ + public function getCacheData($pCoord) + { + if ($pCoord === $this->currentObjectID) { + return $this->currentObject; + } + $this->storeData(); + + // Check if the entry that has been requested actually exists + if (parent::isDataSet($pCoord)) { + $obj = $this->memcache->get($this->cachePrefix.$pCoord.'.cache'); + if ($obj === false) { + // Entry no longer exists in Memcache, so clear it from the cache array + parent::deleteCacheData($pCoord); + throw new Exception('Cell entry '.$pCoord.' no longer exists in MemCache'); + } + } else { + // Return null if requested entry doesn't exist in cache + return null; + } + + // Set current entry to the requested entry + $this->currentObjectID = $pCoord; + $this->currentObject = unserialize($obj); + // Re-attach this as the cell's parent + $this->currentObject->attach($this); + + // Return requested entry + return $this->currentObject; + } + + /** + * Get a list of all cell addresses currently held in cache + * + * @return array of string + */ + public function getCellList() + { + if ($this->currentObjectID !== null) { + $this->storeData(); + } + + return parent::getCellList(); + } + + /** + * Delete a cell in cache identified by coordinate address + * + * @param string $pCoord Coordinate address of the cell to delete + * @throws PHPExcel\Exception + */ + public function deleteCacheData($pCoord) + { + // Delete the entry from Memcache + $this->memcache->delete($this->cachePrefix.$pCoord.'.cache'); + + // Delete the entry from our cell address array + parent::deleteCacheData($pCoord); + } + + /** + * Clone the cell collection + * + * @param PHPExcel\Worksheet $parent The new worksheet + * @throws PHPExcel\Exception + * @return void + */ + public function copyCellCollection(Worksheet $parent) + { + parent::copyCellCollection($parent); + // Get a new id for the new file name + $baseUnique = $this->getUniqueID(); + $newCachePrefix = substr(md5($baseUnique), 0, 8).'.'; + $cacheList = $this->getCellList(); + foreach ($cacheList as $cellID) { + if ($cellID != $this->currentObjectID) { + $obj = $this->memcache->get($this->cachePrefix.$cellID.'.cache'); + if ($obj === false) { + // Entry no longer exists in Memcache, so clear it from the cache array + parent::deleteCacheData($cellID); + throw new Exception('Cell entry '.$cellID.' no longer exists in MemCache'); + } + if (!$this->memcache->add($newCachePrefix.$cellID.'.cache', $obj, null, $this->cacheTime)) { + $this->__destruct(); + throw new Exception('Failed to store cell '.$cellID.' in MemCache'); + } + } + } + $this->cachePrefix = $newCachePrefix; + } + + /** + * Clear the cell collection and disconnect from our parent + * + * @return void + */ + public function unsetWorksheetCells() + { + if (!is_null($this->currentObject)) { + $this->currentObject->detach(); + $this->currentObject = $this->currentObjectID = null; + } + + // Flush the Memcache cache + $this->__destruct(); + + $this->cellCache = array(); + + // detach ourself from the worksheet, so that it can then delete this object successfully + $this->parent = null; + } + + /** + * Initialise this new cell collection + * + * @param PHPExcel\Worksheet $parent The worksheet for this cell collection + * @param array of mixed $arguments Additional initialisation arguments + */ + public function __construct(Worksheet $parent, $arguments) + { + $memcacheServer = (isset($arguments['memcacheServer'])) ? $arguments['memcacheServer'] : 'localhost'; + $memcachePort = (isset($arguments['memcachePort'])) ? $arguments['memcachePort'] : 11211; + $cacheTime = (isset($arguments['cacheTime'])) ? $arguments['cacheTime'] : 600; + + if (is_null($this->cachePrefix)) { + $baseUnique = $this->getUniqueID(); + $this->cachePrefix = substr(md5($baseUnique), 0, 8).'.'; + + // Set a new Memcache object and connect to the Memcache server + $this->memcache = new Memcache(); + if (!$this->memcache->addServer( + $memcacheServer, + $memcachePort, + false, + 50, + 5, + 5, + true, + array($this, 'failureCallback') + )) { + throw new Exception( + 'Could not connect to MemCache server at '.$memcacheServer.':'.$memcachePort + ); + } + $this->cacheTime = $cacheTime; + + parent::__construct($parent); + } + } + + /** + * Memcache error handler + * + * @param string $host Memcache server + * @param integer $port Memcache port + * @throws PHPExcel\Exception + */ + public function failureCallback($host, $port) + { + throw new Exception('memcache '.$host.':'.$port.' failed'); + } + + /** + * Destroy this cell collection + */ + public function __destruct() + { + $cacheList = $this->getCellList(); + foreach ($cacheList as $cellID) { + $this->memcache->delete($this->cachePrefix.$cellID.'.cache'); + } + } + + /** + * Identify whether the caching method is currently available + * Some methods are dependent on the availability of certain extensions being enabled in the PHP build + * + * @return boolean + */ + public static function cacheMethodIsAvailable() + { + return function_exists('memcache_add'); + } +} diff --git a/Classes/PHPExcel/CachedObjectStorage/Memory.php b/Classes/PHPExcel/CachedObjectStorage/Memory.php index f29ae93..3da5d9b 100644 --- a/Classes/PHPExcel/CachedObjectStorage/Memory.php +++ b/Classes/PHPExcel/CachedObjectStorage/Memory.php @@ -1,126 +1,128 @@ -cellCache[$pCoord] = $cell; - - // Set current entry to the new/updated entry - $this->currentObjectID = $pCoord; - - return $cell; - } - - /** - * Get cell at a specific coordinate - * - * @param string $pCoord Coordinate of the cell - * @throws PHPExcel\Exception - * @return PHPExcel\Cell Cell that was found, or null if not found - */ - public function getCacheData($pCoord) - { - // Check if the entry that has been requested actually exists - if (!isset($this->cellCache[$pCoord])) { - $this->currentObjectID = null; - // Return null if requested entry doesn't exist in cache - return null; - } - - // Set current entry to the requested entry - $this->currentObjectID = $pCoord; - - // Return requested entry - return $this->cellCache[$pCoord]; - } - - /** - * Clone the cell collection - * - * @param PHPExcel\Worksheet $parent The new worksheet - * @return void - */ - public function copyCellCollection(Worksheet $parent) - { - parent::copyCellCollection($parent); - - $newCollection = array(); - foreach($this->cellCache as $k => &$cell) { - $newCollection[$k] = clone $cell; - $newCollection[$k]->attach($this); - } - - $this->cellCache = $newCollection; - } - - /** - * Clear the cell collection and disconnect from our parent - * - * @return void - */ - public function unsetWorksheetCells() - { - // Because cells are all stored as intact objects in memory, we need to detach each one from the parent - foreach($this->cellCache as $k => &$cell) { - $cell->detach(); - $this->cellCache[$k] = null; - } - unset($cell); - - $this->cellCache = array(); - - // detach ourself from the worksheet, so that it can then delete this object successfully - $this->parent = null; - } -} +cellCache[$pCoord] = $cell; + + // Set current entry to the new/updated entry + $this->currentObjectID = $pCoord; + + return $cell; + } + + /** + * Get cell at a specific coordinate + * + * @param string $pCoord Coordinate of the cell + * @throws PHPExcel\Exception + * @return PHPExcel\Cell Cell that was found, or null if not found + */ + public function getCacheData($pCoord) + { + // Check if the entry that has been requested actually exists + if (!isset($this->cellCache[$pCoord])) { + $this->currentObjectID = null; + // Return null if requested entry doesn't exist in cache + return null; + } + + // Set current entry to the requested entry + $this->currentObjectID = $pCoord; + + // Return requested entry + return $this->cellCache[$pCoord]; + } + + /** + * Clone the cell collection + * + * @param PHPExcel\Worksheet $parent The new worksheet + * @return void + */ + public function copyCellCollection(Worksheet $parent) + { + parent::copyCellCollection($parent); + + $newCollection = array(); + foreach ($this->cellCache as $k => &$cell) { + $newCollection[$k] = clone $cell; + $newCollection[$k]->attach($this); + } + + $this->cellCache = $newCollection; + } + + /** + * Clear the cell collection and disconnect from our parent + * + * @return void + */ + public function unsetWorksheetCells() + { + // Because cells are all stored as intact objects in memory, we need to detach each one from the parent + foreach ($this->cellCache as $k => &$cell) { + $cell->detach(); + $this->cellCache[$k] = null; + } + unset($cell); + + $this->cellCache = array(); + + // detach ourself from the worksheet, so that it can then delete this object successfully + $this->parent = null; + } +} diff --git a/Classes/PHPExcel/CachedObjectStorage/MemoryGZip.php b/Classes/PHPExcel/CachedObjectStorage/MemoryGZip.php index 5022cb4..52e1d77 100644 --- a/Classes/PHPExcel/CachedObjectStorage/MemoryGZip.php +++ b/Classes/PHPExcel/CachedObjectStorage/MemoryGZip.php @@ -1,138 +1,139 @@ -currentCellIsDirty) { - $this->currentObject->detach(); - - $this->cellCache[$this->currentObjectID] = gzdeflate(serialize($this->currentObject)); - $this->currentCellIsDirty = false; - } - $this->currentObjectID = $this->currentObject = null; - } - - /** - * Add or Update a cell in cache identified by coordinate address - * - * @param string $pCoord Coordinate address of the cell to update - * @param PHPExcel\Cell $cell Cell to update - * @return void - * @throws PHPExcel\Exception - */ - public function addCacheData($pCoord, Cell $cell) - { - if (($pCoord !== $this->currentObjectID) && ($this->currentObjectID !== null)) { - $this->storeData(); - } - - $this->currentObjectID = $pCoord; - $this->currentObject = $cell; - $this->currentCellIsDirty = true; - - return $cell; - } - - /** - * Get cell at a specific coordinate - * - * @param string $pCoord Coordinate of the cell - * @throws PHPExcel\Exception - * @return PHPExcel\Cell Cell that was found, or null if not found - */ - public function getCacheData($pCoord) - { - if ($pCoord === $this->currentObjectID) { - return $this->currentObject; - } - $this->storeData(); - - // Check if the entry that has been requested actually exists - if (!isset($this->cellCache[$pCoord])) { - // Return null if requested entry doesn't exist in cache - return null; - } - - // Set current entry to the requested entry - $this->currentObjectID = $pCoord; - $this->currentObject = unserialize(gzinflate($this->cellCache[$pCoord])); - // Re-attach this as the cell's parent - $this->currentObject->attach($this); - - // Return requested entry - return $this->currentObject; - } - - /** - * Get a list of all cell addresses currently held in cache - * - * @return array of string - */ - public function getCellList() { - if ($this->currentObjectID !== null) { - $this->storeData(); - } - - return parent::getCellList(); - } - - /** - * Clear the cell collection and disconnect from our parent - * - * @return void - */ - public function unsetWorksheetCells() - { - if(!is_null($this->currentObject)) { - $this->currentObject->detach(); - $this->currentObject = $this->currentObjectID = null; - } - $this->cellCache = array(); - - // detach ourself from the worksheet, so that it can then delete this object successfully - $this->parent = null; - } -} +currentCellIsDirty) { + $this->currentObject->detach(); + + $this->cellCache[$this->currentObjectID] = gzdeflate(serialize($this->currentObject)); + $this->currentCellIsDirty = false; + } + $this->currentObjectID = $this->currentObject = null; + } + + /** + * Add or Update a cell in cache identified by coordinate address + * + * @param string $pCoord Coordinate address of the cell to update + * @param PHPExcel\Cell $cell Cell to update + * @return void + * @throws PHPExcel\Exception + */ + public function addCacheData($pCoord, Cell $cell) + { + if (($pCoord !== $this->currentObjectID) && ($this->currentObjectID !== null)) { + $this->storeData(); + } + + $this->currentObjectID = $pCoord; + $this->currentObject = $cell; + $this->currentCellIsDirty = true; + + return $cell; + } + + /** + * Get cell at a specific coordinate + * + * @param string $pCoord Coordinate of the cell + * @throws PHPExcel\Exception + * @return PHPExcel\Cell Cell that was found, or null if not found + */ + public function getCacheData($pCoord) + { + if ($pCoord === $this->currentObjectID) { + return $this->currentObject; + } + $this->storeData(); + + // Check if the entry that has been requested actually exists + if (!isset($this->cellCache[$pCoord])) { + // Return null if requested entry doesn't exist in cache + return null; + } + + // Set current entry to the requested entry + $this->currentObjectID = $pCoord; + $this->currentObject = unserialize(gzinflate($this->cellCache[$pCoord])); + // Re-attach this as the cell's parent + $this->currentObject->attach($this); + + // Return requested entry + return $this->currentObject; + } + + /** + * Get a list of all cell addresses currently held in cache + * + * @return array of string + */ + public function getCellList() + { + if ($this->currentObjectID !== null) { + $this->storeData(); + } + + return parent::getCellList(); + } + + /** + * Clear the cell collection and disconnect from our parent + * + * @return void + */ + public function unsetWorksheetCells() + { + if (!is_null($this->currentObject)) { + $this->currentObject->detach(); + $this->currentObject = $this->currentObjectID = null; + } + $this->cellCache = array(); + + // detach ourself from the worksheet, so that it can then delete this object successfully + $this->parent = null; + } +} diff --git a/Classes/PHPExcel/CachedObjectStorage/MemorySerialized.php b/Classes/PHPExcel/CachedObjectStorage/MemorySerialized.php index e6a9361..e6b8dd0 100644 --- a/Classes/PHPExcel/CachedObjectStorage/MemorySerialized.php +++ b/Classes/PHPExcel/CachedObjectStorage/MemorySerialized.php @@ -1,139 +1,139 @@ -currentCellIsDirty) { - $this->currentObject->detach(); - - $this->cellCache[$this->currentObjectID] = serialize($this->currentObject); - $this->currentCellIsDirty = false; - } - $this->currentObjectID = $this->currentObject = null; - } - - /** - * Add or Update a cell in cache identified by coordinate address - * - * @param string $pCoord Coordinate address of the cell to update - * @param PHPExcel\Cell $cell Cell to update - * @return void - * @throws PHPExcel\Exception - */ - public function addCacheData($pCoord, Cell $cell) - { - if (($pCoord !== $this->currentObjectID) && ($this->currentObjectID !== null)) { - $this->storeData(); - } - - $this->currentObjectID = $pCoord; - $this->currentObject = $cell; - $this->currentCellIsDirty = true; - - return $cell; - } - - /** - * Get cell at a specific coordinate - * - * @param string $pCoord Coordinate of the cell - * @throws PHPExcel\Exception - * @return PHPExcel\Cell Cell that was found, or null if not found - */ - public function getCacheData($pCoord) - { - if ($pCoord === $this->currentObjectID) { - return $this->currentObject; - } - $this->storeData(); - - // Check if the entry that has been requested actually exists - if (!isset($this->cellCache[$pCoord])) { - // Return null if requested entry doesn't exist in cache - return null; - } - - // Set current entry to the requested entry - $this->currentObjectID = $pCoord; - $this->currentObject = unserialize($this->cellCache[$pCoord]); - // Re-attach this as the cell's parent - $this->currentObject->attach($this); - - // Return requested entry - return $this->currentObject; - } - - /** - * Get a list of all cell addresses currently held in cache - * - * @return array of string - */ - public function getCellList() - { - if ($this->currentObjectID !== null) { - $this->storeData(); - } - - return parent::getCellList(); - } - - /** - * Clear the cell collection and disconnect from our parent - * - * @return void - */ - public function unsetWorksheetCells() - { - if(!is_null($this->currentObject)) { - $this->currentObject->detach(); - $this->currentObject = $this->currentObjectID = null; - } - $this->cellCache = array(); - - // detach ourself from the worksheet, so that it can then delete this object successfully - $this->parent = null; - } -} +currentCellIsDirty) { + $this->currentObject->detach(); + + $this->cellCache[$this->currentObjectID] = serialize($this->currentObject); + $this->currentCellIsDirty = false; + } + $this->currentObjectID = $this->currentObject = null; + } + + /** + * Add or Update a cell in cache identified by coordinate address + * + * @param string $pCoord Coordinate address of the cell to update + * @param PHPExcel\Cell $cell Cell to update + * @return void + * @throws PHPExcel\Exception + */ + public function addCacheData($pCoord, Cell $cell) + { + if (($pCoord !== $this->currentObjectID) && ($this->currentObjectID !== null)) { + $this->storeData(); + } + + $this->currentObjectID = $pCoord; + $this->currentObject = $cell; + $this->currentCellIsDirty = true; + + return $cell; + } + + /** + * Get cell at a specific coordinate + * + * @param string $pCoord Coordinate of the cell + * @throws PHPExcel\Exception + * @return PHPExcel\Cell Cell that was found, or null if not found + */ + public function getCacheData($pCoord) + { + if ($pCoord === $this->currentObjectID) { + return $this->currentObject; + } + $this->storeData(); + + // Check if the entry that has been requested actually exists + if (!isset($this->cellCache[$pCoord])) { + // Return null if requested entry doesn't exist in cache + return null; + } + + // Set current entry to the requested entry + $this->currentObjectID = $pCoord; + $this->currentObject = unserialize($this->cellCache[$pCoord]); + // Re-attach this as the cell's parent + $this->currentObject->attach($this); + + // Return requested entry + return $this->currentObject; + } + + /** + * Get a list of all cell addresses currently held in cache + * + * @return array of string + */ + public function getCellList() + { + if ($this->currentObjectID !== null) { + $this->storeData(); + } + + return parent::getCellList(); + } + + /** + * Clear the cell collection and disconnect from our parent + * + * @return void + */ + public function unsetWorksheetCells() + { + if (!is_null($this->currentObject)) { + $this->currentObject->detach(); + $this->currentObject = $this->currentObjectID = null; + } + $this->cellCache = array(); + + // detach ourself from the worksheet, so that it can then delete this object successfully + $this->parent = null; + } +} diff --git a/Classes/PHPExcel/CachedObjectStorage/PHPTemp.php b/Classes/PHPExcel/CachedObjectStorage/PHPTemp.php index 71981db..4c8063b 100644 --- a/Classes/PHPExcel/CachedObjectStorage/PHPTemp.php +++ b/Classes/PHPExcel/CachedObjectStorage/PHPTemp.php @@ -1,206 +1,208 @@ -currentCellIsDirty) { - $this->currentObject->detach(); - - fseek($this->fileHandle,0,SEEK_END); - $offset = ftell($this->fileHandle); - fwrite($this->fileHandle, serialize($this->currentObject)); - $this->cellCache[$this->currentObjectID] = array('ptr' => $offset, - 'sz' => ftell($this->fileHandle) - $offset - ); - $this->currentCellIsDirty = false; - } - $this->currentObjectID = $this->currentObject = null; - } - - /** - * Add or Update a cell in cache identified by coordinate address - * - * @param string $pCoord Coordinate address of the cell to update - * @param PHPExcel\Cell $cell Cell to update - * @return void - * @throws PHPExcel\Exception - */ - public function addCacheData($pCoord, Cell $cell) - { - if (($pCoord !== $this->currentObjectID) && ($this->currentObjectID !== null)) { - $this->storeData(); - } - - $this->currentObjectID = $pCoord; - $this->currentObject = $cell; - $this->currentCellIsDirty = true; - - return $cell; - } - - /** - * Get cell at a specific coordinate - * - * @param string $pCoord Coordinate of the cell - * @throws PHPExcel\Exception - * @return PHPExcel\Cell Cell that was found, or null if not found - */ - public function getCacheData($pCoord) - { - if ($pCoord === $this->currentObjectID) { - return $this->currentObject; - } - $this->storeData(); - - // Check if the entry that has been requested actually exists - if (!isset($this->cellCache[$pCoord])) { - // Return null if requested entry doesn't exist in cache - return null; - } - - // Set current entry to the requested entry - $this->currentObjectID = $pCoord; - fseek($this->fileHandle,$this->cellCache[$pCoord]['ptr']); - $this->currentObject = unserialize(fread($this->fileHandle,$this->cellCache[$pCoord]['sz'])); - // Re-attach this as the cell's parent - $this->currentObject->attach($this); - - // Return requested entry - return $this->currentObject; - } - - /** - * Get a list of all cell addresses currently held in cache - * - * @return array of string - */ - public function getCellList() - { - if ($this->currentObjectID !== null) { - $this->storeData(); - } - - return parent::getCellList(); - } - - /** - * Clone the cell collection - * - * @param PHPExcel\Worksheet $parent The new worksheet - * @return void - */ - public function copyCellCollection(Worksheet $parent) - { - parent::copyCellCollection($parent); - // Open a new stream for the cell cache data - $newFileHandle = fopen('php://temp/maxmemory:'.$this->memoryCacheSize,'a+'); - // Copy the existing cell cache data to the new stream - fseek($this->fileHandle,0); - while (!feof($this->fileHandle)) { - fwrite($newFileHandle,fread($this->fileHandle, 1024)); - } - $this->fileHandle = $newFileHandle; - } - - /** - * Clear the cell collection and disconnect from our parent - * - * @return void - */ - public function unsetWorksheetCells() - { - if(!is_null($this->currentObject)) { - $this->currentObject->detach(); - $this->currentObject = $this->currentObjectID = null; - } - $this->cellCache = array(); - - // detach ourself from the worksheet, so that it can then delete this object successfully - $this->parent = null; - - // Close down the php://temp file - $this->__destruct(); - } - - /** - * Initialise this new cell collection - * - * @param PHPExcel\Worksheet $parent The worksheet for this cell collection - * @param array of mixed $arguments Additional initialisation arguments - */ - public function __construct(Worksheet $parent, $arguments) - { - $this->memoryCacheSize = (isset($arguments['memoryCacheSize'])) ? $arguments['memoryCacheSize'] : '1MB'; - - parent::__construct($parent); - if (is_null($this->fileHandle)) { - $this->fileHandle = fopen('php://temp/maxmemory:'.$this->memoryCacheSize,'a+'); - } - } - - /** - * Destroy this cell collection - */ - public function __destruct() - { - if (!is_null($this->fileHandle)) { - fclose($this->fileHandle); - } - $this->fileHandle = null; - } -} +currentCellIsDirty) { + $this->currentObject->detach(); + + fseek($this->fileHandle, 0, SEEK_END); + $offset = ftell($this->fileHandle); + fwrite($this->fileHandle, serialize($this->currentObject)); + $this->cellCache[$this->currentObjectID] = array('ptr' => $offset, + 'sz' => ftell($this->fileHandle) - $offset + ); + $this->currentCellIsDirty = false; + } + $this->currentObjectID = $this->currentObject = null; + } + + /** + * Add or Update a cell in cache identified by coordinate address + * + * @param string $pCoord Coordinate address of the cell to update + * @param PHPExcel\Cell $cell Cell to update + * @return void + * @throws PHPExcel\Exception + */ + public function addCacheData($pCoord, Cell $cell) + { + if (($pCoord !== $this->currentObjectID) && ($this->currentObjectID !== null)) { + $this->storeData(); + } + + $this->currentObjectID = $pCoord; + $this->currentObject = $cell; + $this->currentCellIsDirty = true; + + return $cell; + } + + /** + * Get cell at a specific coordinate + * + * @param string $pCoord Coordinate of the cell + * @throws PHPExcel\Exception + * @return PHPExcel\Cell Cell that was found, or null if not found + */ + public function getCacheData($pCoord) + { + if ($pCoord === $this->currentObjectID) { + return $this->currentObject; + } + $this->storeData(); + + // Check if the entry that has been requested actually exists + if (!isset($this->cellCache[$pCoord])) { + // Return null if requested entry doesn't exist in cache + return null; + } + + // Set current entry to the requested entry + $this->currentObjectID = $pCoord; + fseek($this->fileHandle, $this->cellCache[$pCoord]['ptr']); + $this->currentObject = unserialize(fread($this->fileHandle, $this->cellCache[$pCoord]['sz'])); + // Re-attach this as the cell's parent + $this->currentObject->attach($this); + + // Return requested entry + return $this->currentObject; + } + + /** + * Get a list of all cell addresses currently held in cache + * + * @return array of string + */ + public function getCellList() + { + if ($this->currentObjectID !== null) { + $this->storeData(); + } + + return parent::getCellList(); + } + + /** + * Clone the cell collection + * + * @param PHPExcel\Worksheet $parent The new worksheet + * @return void + */ + public function copyCellCollection(Worksheet $parent) + { + parent::copyCellCollection($parent); + // Open a new stream for the cell cache data + $newFileHandle = fopen('php://temp/maxmemory:'.$this->memoryCacheSize, 'a+'); + // Copy the existing cell cache data to the new stream + fseek($this->fileHandle, 0); + while (!feof($this->fileHandle)) { + fwrite($newFileHandle, fread($this->fileHandle, 1024)); + } + $this->fileHandle = $newFileHandle; + } + + /** + * Clear the cell collection and disconnect from our parent + * + * @return void + */ + public function unsetWorksheetCells() + { + if (!is_null($this->currentObject)) { + $this->currentObject->detach(); + $this->currentObject = $this->currentObjectID = null; + } + $this->cellCache = array(); + + // detach ourself from the worksheet, so that it can then delete this object successfully + $this->parent = null; + + // Close down the php://temp file + $this->__destruct(); + } + + /** + * Initialise this new cell collection + * + * @param PHPExcel\Worksheet $parent The worksheet for this cell collection + * @param array of mixed $arguments Additional initialisation arguments + */ + public function __construct(Worksheet $parent, $arguments) + { + $this->memoryCacheSize = (isset($arguments['memoryCacheSize'])) ? $arguments['memoryCacheSize'] : '1MB'; + + parent::__construct($parent); + if (is_null($this->fileHandle)) { + $this->fileHandle = fopen('php://temp/maxmemory:'.$this->memoryCacheSize, 'a+'); + } + } + + /** + * Destroy this cell collection + */ + public function __destruct() + { + if (!is_null($this->fileHandle)) { + fclose($this->fileHandle); + } + $this->fileHandle = null; + } +} diff --git a/Classes/PHPExcel/CachedObjectStorage/SQLite.php b/Classes/PHPExcel/CachedObjectStorage/SQLite.php index feef8b1..9df9bc1 100644 --- a/Classes/PHPExcel/CachedObjectStorage/SQLite.php +++ b/Classes/PHPExcel/CachedObjectStorage/SQLite.php @@ -1,305 +1,319 @@ -currentCellIsDirty) { - $this->currentObject->detach(); - - if (!$this->DBHandle->queryExec("INSERT OR REPLACE INTO kvp_".$this->TableName." VALUES('".$this->currentObjectID."','".sqlite_escape_string(serialize($this->currentObject))."')")) - throw new Exception(sqlite_error_string($this->DBHandle->lastError())); - $this->currentCellIsDirty = false; - } - $this->currentObjectID = $this->currentObject = null; - } - - /** - * Add or Update a cell in cache identified by coordinate address - * - * @param string $pCoord Coordinate address of the cell to update - * @param PHPExcel\Cell $cell Cell to update - * @return void - * @throws PHPExcel\Exception - */ - public function addCacheData($pCoord, Cell $cell) - { - if (($pCoord !== $this->currentObjectID) && ($this->currentObjectID !== null)) { - $this->storeData(); - } - - $this->currentObjectID = $pCoord; - $this->currentObject = $cell; - $this->currentCellIsDirty = true; - - return $cell; - } - - /** - * Get cell at a specific coordinate - * - * @param string $pCoord Coordinate of the cell - * @throws PHPExcel\Exception - * @return PHPExcel\Cell Cell that was found, or null if not found - */ - public function getCacheData($pCoord) - { - if ($pCoord === $this->currentObjectID) { - return $this->currentObject; - } - $this->storeData(); - - $query = "SELECT value FROM kvp_".$this->TableName." WHERE id='".$pCoord."'"; - $cellResultSet = $this->DBHandle->query($query,SQLITE_ASSOC); - if ($cellResultSet === false) { - throw new Exception(sqlite_error_string($this->DBHandle->lastError())); - } elseif ($cellResultSet->numRows() == 0) { - // Return null if requested entry doesn't exist in cache - return null; - } - - // Set current entry to the requested entry - $this->currentObjectID = $pCoord; - - $cellResult = $cellResultSet->fetchSingle(); - $this->currentObject = unserialize($cellResult); - // Re-attach this as the cell's parent - $this->currentObject->attach($this); - - // Return requested entry - return $this->currentObject; - } - - /** - * Is a value set for an indexed cell? - * - * @param string $pCoord Coordinate address of the cell to check - * @return boolean - */ - public function isDataSet($pCoord) - { - if ($pCoord === $this->currentObjectID) { - return true; - } - - // Check if the requested entry exists in the cache - $query = "SELECT id FROM kvp_".$this->TableName." WHERE id='".$pCoord."'"; - $cellResultSet = $this->DBHandle->query($query,SQLITE_ASSOC); - if ($cellResultSet === false) { - throw new Exception(sqlite_error_string($this->DBHandle->lastError())); - } elseif ($cellResultSet->numRows() == 0) { - // Return null if requested entry doesn't exist in cache - return false; - } - return true; - } - - /** - * Delete a cell in cache identified by coordinate address - * - * @param string $pCoord Coordinate address of the cell to delete - * @throws PHPExcel\Exception - */ - public function deleteCacheData($pCoord) - { - if ($pCoord === $this->currentObjectID) { - $this->currentObject->detach(); - $this->currentObjectID = $this->currentObject = null; - } - - // Check if the requested entry exists in the cache - $query = "DELETE FROM kvp_".$this->TableName." WHERE id='".$pCoord."'"; - if (!$this->DBHandle->queryExec($query)) - throw new Exception(sqlite_error_string($this->DBHandle->lastError())); - - $this->currentCellIsDirty = false; - } - - /** - * Move a cell object from one address to another - * - * @param string $fromAddress Current address of the cell to move - * @param string $toAddress Destination address of the cell to move - * @return boolean - */ - public function moveCell($fromAddress, $toAddress) - { - if ($fromAddress === $this->currentObjectID) { - $this->currentObjectID = $toAddress; - } - - $query = "DELETE FROM kvp_".$this->TableName." WHERE id='".$toAddress."'"; - $result = $this->DBHandle->exec($query); - if ($result === false) - throw new Exception($this->DBHandle->lastErrorMsg()); - - $query = "UPDATE kvp_".$this->TableName." SET id='".$toAddress."' WHERE id='".$fromAddress."'"; - $result = $this->DBHandle->exec($query); - if ($result === false) - throw new Exception($this->DBHandle->lastErrorMsg()); - - return true; - } - - /** - * Get a list of all cell addresses currently held in cache - * - * @return array of string - */ - public function getCellList() - { - if ($this->currentObjectID !== null) { - $this->storeData(); - } - - $query = "SELECT id FROM kvp_".$this->TableName; - $cellIdsResult = $this->DBHandle->unbufferedQuery($query,SQLITE_ASSOC); - if ($cellIdsResult === false) - throw new Exception(sqlite_error_string($this->DBHandle->lastError())); - - $cellKeys = array(); - foreach($cellIdsResult as $row) { - $cellKeys[] = $row['id']; - } - - return $cellKeys; - } - - /** - * Clone the cell collection - * - * @param PHPExcel\Worksheet $parent The new worksheet - * @return void - */ - public function copyCellCollection(Worksheet $parent) - { - $this->currentCellIsDirty; - $this->storeData(); - - // Get a new id for the new table name - $tableName = str_replace('.','_',$this->getUniqueID()); - if (!$this->DBHandle->queryExec('CREATE TABLE kvp_'.$tableName.' (id VARCHAR(12) PRIMARY KEY, value BLOB) - AS SELECT * FROM kvp_'.$this->TableName)) - throw new Exception(sqlite_error_string($this->DBHandle->lastError())); - - // Copy the existing cell cache file - $this->TableName = $tableName; - } - - /** - * Clear the cell collection and disconnect from our parent - * - * @return void - */ - public function unsetWorksheetCells() - { - if(!is_null($this->currentObject)) { - $this->currentObject->detach(); - $this->currentObject = $this->currentObjectID = null; - } - // detach ourself from the worksheet, so that it can then delete this object successfully - $this->parent = null; - - // Close down the temporary cache file - $this->__destruct(); - } - - /** - * Initialise this new cell collection - * - * @param PHPExcel\Worksheet $parent The worksheet for this cell collection - */ - public function __construct(Worksheet $parent) - { - parent::__construct($parent); - if (is_null($this->DBHandle)) { - $this->TableName = str_replace('.','_',$this->getUniqueID()); - $DBName = ':memory:'; - - $this->DBHandle = new SQLiteDatabase($DBName); - if ($this->DBHandle === false) - throw new Exception(sqlite_error_string($this->DBHandle->lastError())); - if (!$this->DBHandle->queryExec('CREATE TABLE kvp_'.$this->TableName.' (id VARCHAR(12) PRIMARY KEY, value BLOB)')) - throw new Exception(sqlite_error_string($this->DBHandle->lastError())); - } - } - - /** - * Destroy this cell collection - */ - public function __destruct() - { - if (!is_null($this->DBHandle)) { - $this->DBHandle->queryExec('DROP TABLE kvp_'.$this->TableName); - } - $this->DBHandle = null; - } - - /** - * Identify whether the caching method is currently available - * Some methods are dependent on the availability of certain extensions being enabled in the PHP build - * - * @return boolean - */ - public static function cacheMethodIsAvailable() - { - return function_exists('sqlite_open'); - } -} +currentCellIsDirty) { + $this->currentObject->detach(); + $query = "INSERT OR REPLACE INTO kvp_" . $this->TableName . + " VALUES('" . $this->currentObjectID . "','" . + sqlite_escape_string(serialize($this->currentObject)) . "')"; + if (!$this->DBHandle->queryExec($query)) { + throw new Exception(sqlite_error_string($this->DBHandle->lastError())); + } + $this->currentCellIsDirty = false; + } + $this->currentObjectID = $this->currentObject = null; + } + + /** + * Add or Update a cell in cache identified by coordinate address + * + * @param string $pCoord Coordinate address of the cell to update + * @param PHPExcel\Cell $cell Cell to update + * @return void + * @throws PHPExcel\Exception + */ + public function addCacheData($pCoord, Cell $cell) + { + if (($pCoord !== $this->currentObjectID) && ($this->currentObjectID !== null)) { + $this->storeData(); + } + + $this->currentObjectID = $pCoord; + $this->currentObject = $cell; + $this->currentCellIsDirty = true; + + return $cell; + } + + /** + * Get cell at a specific coordinate + * + * @param string $pCoord Coordinate of the cell + * @throws PHPExcel\Exception + * @return PHPExcel\Cell Cell that was found, or null if not found + */ + public function getCacheData($pCoord) + { + if ($pCoord === $this->currentObjectID) { + return $this->currentObject; + } + $this->storeData(); + + $query = "SELECT value FROM kvp_".$this->TableName." WHERE id='".$pCoord."'"; + $cellResultSet = $this->DBHandle->query($query, SQLITE_ASSOC); + if ($cellResultSet === false) { + throw new Exception(sqlite_error_string($this->DBHandle->lastError())); + } elseif ($cellResultSet->numRows() == 0) { + // Return null if requested entry doesn't exist in cache + return null; + } + + // Set current entry to the requested entry + $this->currentObjectID = $pCoord; + + $cellResult = $cellResultSet->fetchSingle(); + $this->currentObject = unserialize($cellResult); + // Re-attach this as the cell's parent + $this->currentObject->attach($this); + + // Return requested entry + return $this->currentObject; + } + + /** + * Is a value set for an indexed cell? + * + * @param string $pCoord Coordinate address of the cell to check + * @return boolean + */ + public function isDataSet($pCoord) + { + if ($pCoord === $this->currentObjectID) { + return true; + } + + // Check if the requested entry exists in the cache + $query = "SELECT id FROM kvp_".$this->TableName." WHERE id='".$pCoord."'"; + $cellResultSet = $this->DBHandle->query($query, SQLITE_ASSOC); + if ($cellResultSet === false) { + throw new Exception(sqlite_error_string($this->DBHandle->lastError())); + } elseif ($cellResultSet->numRows() == 0) { + // Return null if requested entry doesn't exist in cache + return false; + } + return true; + } + + /** + * Delete a cell in cache identified by coordinate address + * + * @param string $pCoord Coordinate address of the cell to delete + * @throws PHPExcel\Exception + */ + public function deleteCacheData($pCoord) + { + if ($pCoord === $this->currentObjectID) { + $this->currentObject->detach(); + $this->currentObjectID = $this->currentObject = null; + } + + // Check if the requested entry exists in the cache + $query = "DELETE FROM kvp_".$this->TableName." WHERE id='".$pCoord."'"; + if (!$this->DBHandle->queryExec($query)) { + throw new Exception(sqlite_error_string($this->DBHandle->lastError())); + } + + $this->currentCellIsDirty = false; + } + + /** + * Move a cell object from one address to another + * + * @param string $fromAddress Current address of the cell to move + * @param string $toAddress Destination address of the cell to move + * @return boolean + */ + public function moveCell($fromAddress, $toAddress) + { + if ($fromAddress === $this->currentObjectID) { + $this->currentObjectID = $toAddress; + } + + $query = "DELETE FROM kvp_".$this->TableName." WHERE id='".$toAddress."'"; + if (!$this->DBHandle->exec($query)) { + throw new Exception($this->DBHandle->lastErrorMsg()); + } + + $query = "UPDATE kvp_".$this->TableName." SET id='".$toAddress."' WHERE id='".$fromAddress."'"; + if (!$this->DBHandle->exec($query)) { + throw new Exception($this->DBHandle->lastErrorMsg()); + } + + return true; + } + + /** + * Get a list of all cell addresses currently held in cache + * + * @return array of string + */ + public function getCellList() + { + if ($this->currentObjectID !== null) { + $this->storeData(); + } + + $query = "SELECT id FROM kvp_".$this->TableName; + $cellIdsResult = $this->DBHandle->unbufferedQuery($query, SQLITE_ASSOC); + if (!$cellIdsResult) { + throw new Exception(sqlite_error_string($this->DBHandle->lastError())); + } + + $cellKeys = array(); + foreach ($cellIdsResult as $row) { + $cellKeys[] = $row['id']; + } + + return $cellKeys; + } + + /** + * Clone the cell collection + * + * @param PHPExcel\Worksheet $parent The new worksheet + * @return void + */ + public function copyCellCollection(Worksheet $parent) + { + $this->currentCellIsDirty; + $this->storeData(); + + // Get a new id for the new table name + $tableName = str_replace('.', '_', $this->getUniqueID()); + $result = $this->DBHandle->queryExec( + 'CREATE TABLE kvp_'.$tableName.' (id VARCHAR(12) PRIMARY KEY, value BLOB) + AS SELECT * FROM kvp_'.$this->TableName + ); + if (!$result) { + throw new Exception(sqlite_error_string($this->DBHandle->lastError())); + } + + // Copy the existing cell cache file + $this->TableName = $tableName; + } + + /** + * Clear the cell collection and disconnect from our parent + * + * @return void + */ + public function unsetWorksheetCells() + { + if (!is_null($this->currentObject)) { + $this->currentObject->detach(); + $this->currentObject = $this->currentObjectID = null; + } + // detach ourself from the worksheet, so that it can then delete this object successfully + $this->parent = null; + + // Close down the temporary cache file + $this->__destruct(); + } + + /** + * Initialise this new cell collection + * + * @param PHPExcel\Worksheet $parent The worksheet for this cell collection + */ + public function __construct(Worksheet $parent) + { + parent::__construct($parent); + if (is_null($this->DBHandle)) { + $this->TableName = str_replace('.', '_', $this->getUniqueID()); + $DBName = ':memory:'; + + $this->DBHandle = new SQLiteDatabase($DBName); + if (!$this->DBHandle) { + throw new Exception(sqlite_error_string($this->DBHandle->lastError())); + } + $result = $this->DBHandle->queryExec( + 'CREATE TABLE kvp_'.$this->TableName.' (id VARCHAR(12) PRIMARY KEY, value BLOB)' + ); + if (!$result) { + throw new Exception(sqlite_error_string($this->DBHandle->lastError())); + } + } + } + + /** + * Destroy this cell collection + */ + public function __destruct() + { + if (!is_null($this->DBHandle)) { + $this->DBHandle->queryExec('DROP TABLE kvp_'.$this->TableName); + } + $this->DBHandle = null; + } + + /** + * Identify whether the caching method is currently available + * Some methods are dependent on the availability of certain extensions being enabled in the PHP build + * + * @return boolean + */ + public static function cacheMethodIsAvailable() + { + return function_exists('sqlite_open'); + } +} diff --git a/Classes/PHPExcel/CachedObjectStorage/SQLite3.php b/Classes/PHPExcel/CachedObjectStorage/SQLite3.php index 0f2c907..e397011 100644 --- a/Classes/PHPExcel/CachedObjectStorage/SQLite3.php +++ b/Classes/PHPExcel/CachedObjectStorage/SQLite3.php @@ -1,345 +1,363 @@ -currentCellIsDirty) { - $this->currentObject->detach(); - - $this->insertQuery->bindValue('id', $this->currentObjectID, SQLITE3_TEXT); - $this->insertQuery->bindValue('data', serialize($this->currentObject), SQLITE3_BLOB); - $result = $this->insertQuery->execute(); - if ($result === false) - throw new Exception($this->DBHandle->lastErrorMsg()); - $this->currentCellIsDirty = false; - } - $this->currentObjectID = $this->currentObject = null; - } - - /** - * Add or Update a cell in cache identified by coordinate address - * - * @param string $pCoord Coordinate address of the cell to update - * @param PHPExcel\Cell $cell Cell to update - * @return void - * @throws PHPExcel\Exception - */ - public function addCacheData($pCoord, Cell $cell) - { - if (($pCoord !== $this->currentObjectID) && ($this->currentObjectID !== null)) { - $this->storeData(); - } - - $this->currentObjectID = $pCoord; - $this->currentObject = $cell; - $this->currentCellIsDirty = true; - - return $cell; - } - - /** - * Get cell at a specific coordinate - * - * @param string $pCoord Coordinate of the cell - * @throws PHPExcel\Exception - * @return PHPExcel\Cell Cell that was found, or null if not found - */ - public function getCacheData($pCoord) - { - if ($pCoord === $this->currentObjectID) { - return $this->currentObject; - } - $this->storeData(); - - $this->selectQuery->bindValue('id', $pCoord, SQLITE3_TEXT); - $cellResult = $this->selectQuery->execute(); - if ($cellResult === false) { - throw new Exception($this->DBHandle->lastErrorMsg()); - } - $cellData = $cellResult->fetchArray(SQLITE3_ASSOC); - if ($cellData === false) { - // Return null if requested entry doesn't exist in cache - return null; - } - - // Set current entry to the requested entry - $this->currentObjectID = $pCoord; - - $this->currentObject = unserialize($cellData['value']); - // Re-attach this as the cell's parent - $this->currentObject->attach($this); - - // Return requested entry - return $this->currentObject; - } - - /** - * Is a value set for an indexed cell? - * - * @param string $pCoord Coordinate address of the cell to check - * @return boolean - */ - public function isDataSet($pCoord) - { - if ($pCoord === $this->currentObjectID) { - return true; - } - - // Check if the requested entry exists in the cache - $this->selectQuery->bindValue('id', $pCoord, SQLITE3_TEXT); - $cellResult = $this->selectQuery->execute(); - if ($cellResult === false) { - throw new Exception($this->DBHandle->lastErrorMsg()); - } - $cellData = $cellResult->fetchArray(SQLITE3_ASSOC); - - return ($cellData === false) ? false : true; - } - - /** - * Delete a cell in cache identified by coordinate address - * - * @param string $pCoord Coordinate address of the cell to delete - * @throws PHPExcel\Exception - */ - public function deleteCacheData($pCoord) - { - if ($pCoord === $this->currentObjectID) { - $this->currentObject->detach(); - $this->currentObjectID = $this->currentObject = null; - } - - // Check if the requested entry exists in the cache - $this->deleteQuery->bindValue('id', $pCoord, SQLITE3_TEXT); - $result = $this->deleteQuery->execute(); - if ($result === false) - throw new Exception($this->DBHandle->lastErrorMsg()); - - $this->currentCellIsDirty = false; - } - - /** - * Move a cell object from one address to another - * - * @param string $fromAddress Current address of the cell to move - * @param string $toAddress Destination address of the cell to move - * @return boolean - */ - public function moveCell($fromAddress, $toAddress) - { - if ($fromAddress === $this->currentObjectID) { - $this->currentObjectID = $toAddress; - } - - $this->deleteQuery->bindValue('id', $toAddress, SQLITE3_TEXT); - $result = $this->deleteQuery->execute(); - if ($result === false) - throw new Exception($this->DBHandle->lastErrorMsg()); - - $this->updateQuery->bindValue('toid', $toAddress, SQLITE3_TEXT); - $this->updateQuery->bindValue('fromid', $fromAddress, SQLITE3_TEXT); - $result = $this->updateQuery->execute(); - if ($result === false) - throw new Exception($this->DBHandle->lastErrorMsg()); - - return true; - } - - /** - * Get a list of all cell addresses currently held in cache - * - * @return array of string - */ - public function getCellList() - { - if ($this->currentObjectID !== null) { - $this->storeData(); - } - - $query = "SELECT id FROM kvp_".$this->TableName; - $cellIdsResult = $this->DBHandle->query($query); - if ($cellIdsResult === false) - throw new Exception($this->DBHandle->lastErrorMsg()); - - $cellKeys = array(); - while ($row = $cellIdsResult->fetchArray(SQLITE3_ASSOC)) { - $cellKeys[] = $row['id']; - } - - return $cellKeys; - } - - /** - * Clone the cell collection - * - * @param PHPExcel\Worksheet $parent The new worksheet - * @return void - */ - public function copyCellCollection(Worksheet $parent) - { - $this->currentCellIsDirty; - $this->storeData(); - - // Get a new id for the new table name - $tableName = str_replace('.','_',$this->getUniqueID()); - if (!$this->DBHandle->exec('CREATE TABLE kvp_'.$tableName.' (id VARCHAR(12) PRIMARY KEY, value BLOB) - AS SELECT * FROM kvp_'.$this->TableName)) - throw new Exception($this->DBHandle->lastErrorMsg()); - - // Copy the existing cell cache file - $this->TableName = $tableName; - } - - /** - * Clear the cell collection and disconnect from our parent - * - * @return void - */ - public function unsetWorksheetCells() - { - if(!is_null($this->currentObject)) { - $this->currentObject->detach(); - $this->currentObject = $this->currentObjectID = null; - } - // detach ourself from the worksheet, so that it can then delete this object successfully - $this->parent = null; - - // Close down the temporary cache file - $this->__destruct(); - } - - /** - * Initialise this new cell collection - * - * @param PHPExcel\Worksheet $parent The worksheet for this cell collection - */ - public function __construct(Worksheet $parent) - { - parent::__construct($parent); - if (is_null($this->DBHandle)) { - $this->TableName = str_replace('.', '_', $this->getUniqueID()); - $DBName = ':memory:'; - - $this->DBHandle = new SQLite3($DBName); - if ($this->DBHandle === false) - throw new Exception($this->DBHandle->lastErrorMsg()); - if (!$this->DBHandle->exec('CREATE TABLE kvp_'.$this->TableName.' (id VARCHAR(12) PRIMARY KEY, value BLOB)')) - throw new Exception($this->DBHandle->lastErrorMsg()); - } - - $this->selectQuery = $this->DBHandle->prepare("SELECT value FROM kvp_".$this->TableName." WHERE id = :id"); - $this->insertQuery = $this->DBHandle->prepare("INSERT OR REPLACE INTO kvp_".$this->TableName." VALUES(:id,:data)"); - $this->updateQuery = $this->DBHandle->prepare("UPDATE kvp_".$this->TableName." SET id=:toId WHERE id=:fromId"); - $this->deleteQuery = $this->DBHandle->prepare("DELETE FROM kvp_".$this->TableName." WHERE id = :id"); - } - - /** - * Destroy this cell collection - */ - public function __destruct() - { - if (!is_null($this->DBHandle)) { - $this->DBHandle->exec('DROP TABLE kvp_'.$this->TableName); - $this->DBHandle->close(); - } - $this->DBHandle = null; - } - - - /** - * Identify whether the caching method is currently available - * Some methods are dependent on the availability of certain extensions being enabled in the PHP build - * - * @return boolean - */ - public static function cacheMethodIsAvailable() - { - return class_exists('SQLite3', false); - } -} +currentCellIsDirty) { + $this->currentObject->detach(); + + $this->insertQuery->bindValue('id', $this->currentObjectID, SQLITE3_TEXT); + $this->insertQuery->bindValue('data', serialize($this->currentObject), SQLITE3_BLOB); + $result = $this->insertQuery->execute(); + if (!$result) { + throw new Exception($this->DBHandle->lastErrorMsg()); + } + $this->currentCellIsDirty = false; + } + $this->currentObjectID = $this->currentObject = null; + } + + /** + * Add or Update a cell in cache identified by coordinate address + * + * @param string $pCoord Coordinate address of the cell to update + * @param PHPExcel\Cell $cell Cell to update + * @return void + * @throws PHPExcel\Exception + */ + public function addCacheData($pCoord, Cell $cell) + { + if (($pCoord !== $this->currentObjectID) && ($this->currentObjectID !== null)) { + $this->storeData(); + } + + $this->currentObjectID = $pCoord; + $this->currentObject = $cell; + $this->currentCellIsDirty = true; + + return $cell; + } + + /** + * Get cell at a specific coordinate + * + * @param string $pCoord Coordinate of the cell + * @throws PHPExcel\Exception + * @return PHPExcel\Cell Cell that was found, or null if not found + */ + public function getCacheData($pCoord) + { + if ($pCoord === $this->currentObjectID) { + return $this->currentObject; + } + $this->storeData(); + + $this->selectQuery->bindValue('id', $pCoord, SQLITE3_TEXT); + $cellResult = $this->selectQuery->execute(); + if ($cellResult === false) { + throw new Exception($this->DBHandle->lastErrorMsg()); + } + $cellData = $cellResult->fetchArray(SQLITE3_ASSOC); + if ($cellData === false) { + // Return null if requested entry doesn't exist in cache + return null; + } + + // Set current entry to the requested entry + $this->currentObjectID = $pCoord; + + $this->currentObject = unserialize($cellData['value']); + // Re-attach this as the cell's parent + $this->currentObject->attach($this); + + // Return requested entry + return $this->currentObject; + } + + /** + * Is a value set for an indexed cell? + * + * @param string $pCoord Coordinate address of the cell to check + * @return boolean + */ + public function isDataSet($pCoord) + { + if ($pCoord === $this->currentObjectID) { + return true; + } + + // Check if the requested entry exists in the cache + $this->selectQuery->bindValue('id', $pCoord, SQLITE3_TEXT); + $cellResult = $this->selectQuery->execute(); + if ($cellResult === false) { + throw new Exception($this->DBHandle->lastErrorMsg()); + } + $cellData = $cellResult->fetchArray(SQLITE3_ASSOC); + + return ($cellData === false) ? false : true; + } + + /** + * Delete a cell in cache identified by coordinate address + * + * @param string $pCoord Coordinate address of the cell to delete + * @throws PHPExcel\Exception + */ + public function deleteCacheData($pCoord) + { + if ($pCoord === $this->currentObjectID) { + $this->currentObject->detach(); + $this->currentObjectID = $this->currentObject = null; + } + + // Check if the requested entry exists in the cache + $this->deleteQuery->bindValue('id', $pCoord, SQLITE3_TEXT); + $result = $this->deleteQuery->execute(); + if (!$result) { + throw new Exception($this->DBHandle->lastErrorMsg()); + } + + $this->currentCellIsDirty = false; + } + + /** + * Move a cell object from one address to another + * + * @param string $fromAddress Current address of the cell to move + * @param string $toAddress Destination address of the cell to move + * @return boolean + */ + public function moveCell($fromAddress, $toAddress) + { + if ($fromAddress === $this->currentObjectID) { + $this->currentObjectID = $toAddress; + } + + $this->deleteQuery->bindValue('id', $toAddress, SQLITE3_TEXT); + $result = $this->deleteQuery->execute(); + if (!$result) { + throw new Exception($this->DBHandle->lastErrorMsg()); + } + + $this->updateQuery->bindValue('toid', $toAddress, SQLITE3_TEXT); + $this->updateQuery->bindValue('fromid', $fromAddress, SQLITE3_TEXT); + $result = $this->updateQuery->execute(); + if (!$result) { + throw new Exception($this->DBHandle->lastErrorMsg()); + } + + return true; + } + + /** + * Get a list of all cell addresses currently held in cache + * + * @return array of string + */ + public function getCellList() + { + if ($this->currentObjectID !== null) { + $this->storeData(); + } + + $query = "SELECT id FROM kvp_".$this->TableName; + $cellIdsResult = $this->DBHandle->query($query); + if ($cellIdsResult === false) + throw new Exception($this->DBHandle->lastErrorMsg()); + + $cellKeys = array(); + while ($row = $cellIdsResult->fetchArray(SQLITE3_ASSOC)) { + $cellKeys[] = $row['id']; + } + + return $cellKeys; + } + + /** + * Clone the cell collection + * + * @param PHPExcel\Worksheet $parent The new worksheet + * @return void + */ + public function copyCellCollection(Worksheet $parent) + { + $this->currentCellIsDirty; + $this->storeData(); + + // Get a new id for the new table name + $tableName = str_replace('.', '_', $this->getUniqueID()); + if (!$this->DBHandle->exec( + 'CREATE TABLE kvp_'.$tableName.' (id VARCHAR(12) PRIMARY KEY, value BLOB) + AS SELECT * FROM kvp_'.$this->TableName + )) { + throw new Exception($this->DBHandle->lastErrorMsg()); + } + + // Copy the existing cell cache file + $this->TableName = $tableName; + } + + /** + * Clear the cell collection and disconnect from our parent + * + * @return void + */ + public function unsetWorksheetCells() + { + if (!is_null($this->currentObject)) { + $this->currentObject->detach(); + $this->currentObject = $this->currentObjectID = null; + } + // detach ourself from the worksheet, so that it can then delete this object successfully + $this->parent = null; + + // Close down the temporary cache file + $this->__destruct(); + } + + /** + * Initialise this new cell collection + * + * @param PHPExcel\Worksheet $parent The worksheet for this cell collection + */ + public function __construct(Worksheet $parent) + { + parent::__construct($parent); + if (is_null($this->DBHandle)) { + $this->TableName = str_replace('.', '_', $this->getUniqueID()); + $DBName = ':memory:'; + + $this->DBHandle = new SQLite3($DBName); + if (!$this->DBHandle) { + throw new Exception($this->DBHandle->lastErrorMsg()); + } + $query = 'CREATE TABLE kvp_'.$this->TableName.' (id VARCHAR(12) PRIMARY KEY, value BLOB)'; + if (!$this->DBHandle->exec($query)) { + throw new Exception($this->DBHandle->lastErrorMsg()); + } + } + + $this->selectQuery = $this->DBHandle->prepare( + "SELECT value FROM kvp_".$this->TableName." WHERE id = :id" + ); + $this->insertQuery = $this->DBHandle->prepare( + "INSERT OR REPLACE INTO kvp_".$this->TableName." VALUES(:id,:data)" + ); + $this->updateQuery = $this->DBHandle->prepare( + "UPDATE kvp_".$this->TableName." SET id=:toId WHERE id=:fromId" + ); + $this->deleteQuery = $this->DBHandle->prepare( + "DELETE FROM kvp_".$this->TableName." WHERE id = :id" + ); + } + + /** + * Destroy this cell collection + */ + public function __destruct() + { + if (!is_null($this->DBHandle)) { + $this->DBHandle->exec('DROP TABLE kvp_'.$this->TableName); + $this->DBHandle->close(); + } + $this->DBHandle = null; + } + + + /** + * Identify whether the caching method is currently available + * Some methods are dependent on the availability of certain extensions being enabled in the PHP build + * + * @return boolean + */ + public static function cacheMethodIsAvailable() + { + return class_exists('SQLite3', false); + } +} diff --git a/Classes/PHPExcel/CachedObjectStorage/Wincache.php b/Classes/PHPExcel/CachedObjectStorage/Wincache.php index ddb6e46..c1a17a2 100644 --- a/Classes/PHPExcel/CachedObjectStorage/Wincache.php +++ b/Classes/PHPExcel/CachedObjectStorage/Wincache.php @@ -1,292 +1,292 @@ -currentCellIsDirty) { - $this->currentObject->detach(); - - $obj = serialize($this->currentObject); - if (wincache_ucache_exists($this->cachePrefix.$this->currentObjectID.'.cache')) { - if (!wincache_ucache_set($this->cachePrefix.$this->currentObjectID.'.cache', $obj, $this->cacheTime)) { - $this->__destruct(); - throw new Exception('Failed to store cell '.$this->currentObjectID.' in WinCache'); - } - } else { - if (!wincache_ucache_add($this->cachePrefix.$this->currentObjectID.'.cache', $obj, $this->cacheTime)) { - $this->__destruct(); - throw new Exception('Failed to store cell '.$this->currentObjectID.' in WinCache'); - } - } - $this->currentCellIsDirty = false; - } - - $this->currentObjectID = $this->currentObject = null; - } - - /** - * Add or Update a cell in cache identified by coordinate address - * - * @param string $pCoord Coordinate address of the cell to update - * @param PHPExcel\Cell $cell Cell to update - * @return void - * @throws PHPExcel\Exception - */ - public function addCacheData($pCoord, Cell $cell) - { - if (($pCoord !== $this->currentObjectID) && ($this->currentObjectID !== null)) { - $this->storeData(); - } - $this->cellCache[$pCoord] = true; - - $this->currentObjectID = $pCoord; - $this->currentObject = $cell; - $this->currentCellIsDirty = true; - - return $cell; - } - - /** - * Is a value set in the current PHPExcel\CachedObjectStorage_ICache for an indexed cell? - * - * @param string $pCoord Coordinate address of the cell to check - * @return boolean - */ - public function isDataSet($pCoord) - { - // Check if the requested entry is the current object, or exists in the cache - if (parent::isDataSet($pCoord)) { - if ($this->currentObjectID == $pCoord) { - return true; - } - // Check if the requested entry still exists in cache - $success = wincache_ucache_exists($this->cachePrefix.$pCoord.'.cache'); - if ($success === false) { - // Entry no longer exists in Wincache, so clear it from the cache array - parent::deleteCacheData($pCoord); - throw new Exception('Cell entry '.$pCoord.' no longer exists in WinCache'); - } - return true; - } - return false; - } - - /** - * Get cell at a specific coordinate - * - * @param string $pCoord Coordinate of the cell - * @throws PHPExcel\Exception - * @return PHPExcel\Cell Cell that was found, or null if not found - */ - public function getCacheData($pCoord) - { - if ($pCoord === $this->currentObjectID) { - return $this->currentObject; - } - $this->storeData(); - - // Check if the entry that has been requested actually exists - $obj = null; - if (parent::isDataSet($pCoord)) { - $success = false; - $obj = wincache_ucache_get($this->cachePrefix.$pCoord.'.cache', $success); - if ($success === false) { - // Entry no longer exists in WinCache, so clear it from the cache array - parent::deleteCacheData($pCoord); - throw new Exception('Cell entry '.$pCoord.' no longer exists in WinCache'); - } - } else { - // Return null if requested entry doesn't exist in cache - return null; - } - - // Set current entry to the requested entry - $this->currentObjectID = $pCoord; - $this->currentObject = unserialize($obj); - // Re-attach this as the cell's parent - $this->currentObject->attach($this); - - // Return requested entry - return $this->currentObject; - } - - /** - * Get a list of all cell addresses currently held in cache - * - * @return array of string - */ - public function getCellList() - { - if ($this->currentObjectID !== null) { - $this->storeData(); - } - - return parent::getCellList(); - } - - /** - * Delete a cell in cache identified by coordinate address - * - * @param string $pCoord Coordinate address of the cell to delete - * @throws PHPExcel\Exception - */ - public function deleteCacheData($pCoord) - { - // Delete the entry from Wincache - wincache_ucache_delete($this->cachePrefix.$pCoord.'.cache'); - - // Delete the entry from our cell address array - parent::deleteCacheData($pCoord); - } - - /** - * Clone the cell collection - * - * @param PHPExcel\Worksheet $parent The new worksheet - * @return void - */ - public function copyCellCollection(Worksheet $parent) - { - parent::copyCellCollection($parent); - // Get a new id for the new file name - $baseUnique = $this->getUniqueID(); - $newCachePrefix = substr(md5($baseUnique), 0, 8).'.'; - $cacheList = $this->getCellList(); - foreach($cacheList as $cellID) { - if ($cellID != $this->currentObjectID) { - $success = false; - $obj = wincache_ucache_get($this->cachePrefix.$cellID.'.cache', $success); - if ($success === false) { - // Entry no longer exists in WinCache, so clear it from the cache array - parent::deleteCacheData($cellID); - throw new Exception('Cell entry '.$cellID.' no longer exists in Wincache'); - } - if (!wincache_ucache_add($newCachePrefix.$cellID.'.cache', $obj, $this->cacheTime)) { - $this->__destruct(); - throw new Exception('Failed to store cell '.$cellID.' in Wincache'); - } - } - } - $this->cachePrefix = $newCachePrefix; - } - - /** - * Clear the cell collection and disconnect from our parent - * - * @return void - */ - public function unsetWorksheetCells() - { - if(!is_null($this->currentObject)) { - $this->currentObject->detach(); - $this->currentObject = $this->currentObjectID = null; - } - - // Flush the WinCache cache - $this->__destruct(); - - $this->cellCache = array(); - - // detach ourself from the worksheet, so that it can then delete this object successfully - $this->parent = null; - } - - /** - * Initialise this new cell collection - * - * @param PHPExcel\Worksheet $parent The worksheet for this cell collection - * @param array of mixed $arguments Additional initialisation arguments - */ - public function __construct(Worksheet $parent, $arguments) - { - $cacheTime = (isset($arguments['cacheTime'])) ? $arguments['cacheTime'] : 600; - - if (is_null($this->cachePrefix)) { - $baseUnique = $this->getUniqueID(); - $this->cachePrefix = substr(md5($baseUnique), 0, 8).'.'; - $this->cacheTime = $cacheTime; - - parent::__construct($parent); - } - } - - /** - * Destroy this cell collection - */ - public function __destruct() - { - $cacheList = $this->getCellList(); - foreach($cacheList as $cellID) { - wincache_ucache_delete($this->cachePrefix.$cellID.'.cache'); - } - } - - /** - * Identify whether the caching method is currently available - * Some methods are dependent on the availability of certain extensions being enabled in the PHP build - * - * @return boolean - */ - public static function cacheMethodIsAvailable() - { - return function_exists('wincache_ucache_add'); - } -} +currentCellIsDirty) { + $this->currentObject->detach(); + + $obj = serialize($this->currentObject); + if (wincache_ucache_exists($this->cachePrefix.$this->currentObjectID.'.cache')) { + if (!wincache_ucache_set($this->cachePrefix.$this->currentObjectID.'.cache', $obj, $this->cacheTime)) { + $this->__destruct(); + throw new Exception('Failed to store cell '.$this->currentObjectID.' in WinCache'); + } + } else { + if (!wincache_ucache_add($this->cachePrefix.$this->currentObjectID.'.cache', $obj, $this->cacheTime)) { + $this->__destruct(); + throw new Exception('Failed to store cell '.$this->currentObjectID.' in WinCache'); + } + } + $this->currentCellIsDirty = false; + } + + $this->currentObjectID = $this->currentObject = null; + } + + /** + * Add or Update a cell in cache identified by coordinate address + * + * @param string $pCoord Coordinate address of the cell to update + * @param PHPExcel\Cell $cell Cell to update + * @return void + * @throws PHPExcel\Exception + */ + public function addCacheData($pCoord, Cell $cell) + { + if (($pCoord !== $this->currentObjectID) && ($this->currentObjectID !== null)) { + $this->storeData(); + } + $this->cellCache[$pCoord] = true; + + $this->currentObjectID = $pCoord; + $this->currentObject = $cell; + $this->currentCellIsDirty = true; + + return $cell; + } + + /** + * Is a value set in the current PHPExcel\CachedObjectStorage_ICache for an indexed cell? + * + * @param string $pCoord Coordinate address of the cell to check + * @return boolean + */ + public function isDataSet($pCoord) + { + // Check if the requested entry is the current object, or exists in the cache + if (parent::isDataSet($pCoord)) { + if ($this->currentObjectID == $pCoord) { + return true; + } + // Check if the requested entry still exists in cache + $success = wincache_ucache_exists($this->cachePrefix.$pCoord.'.cache'); + if ($success === false) { + // Entry no longer exists in Wincache, so clear it from the cache array + parent::deleteCacheData($pCoord); + throw new Exception('Cell entry '.$pCoord.' no longer exists in WinCache'); + } + return true; + } + return false; + } + + /** + * Get cell at a specific coordinate + * + * @param string $pCoord Coordinate of the cell + * @throws PHPExcel\Exception + * @return PHPExcel\Cell Cell that was found, or null if not found + */ + public function getCacheData($pCoord) + { + if ($pCoord === $this->currentObjectID) { + return $this->currentObject; + } + $this->storeData(); + + // Check if the entry that has been requested actually exists + $obj = null; + if (parent::isDataSet($pCoord)) { + $success = false; + $obj = wincache_ucache_get($this->cachePrefix.$pCoord.'.cache', $success); + if ($success === false) { + // Entry no longer exists in WinCache, so clear it from the cache array + parent::deleteCacheData($pCoord); + throw new Exception('Cell entry '.$pCoord.' no longer exists in WinCache'); + } + } else { + // Return null if requested entry doesn't exist in cache + return null; + } + + // Set current entry to the requested entry + $this->currentObjectID = $pCoord; + $this->currentObject = unserialize($obj); + // Re-attach this as the cell's parent + $this->currentObject->attach($this); + + // Return requested entry + return $this->currentObject; + } + + /** + * Get a list of all cell addresses currently held in cache + * + * @return array of string + */ + public function getCellList() + { + if ($this->currentObjectID !== null) { + $this->storeData(); + } + + return parent::getCellList(); + } + + /** + * Delete a cell in cache identified by coordinate address + * + * @param string $pCoord Coordinate address of the cell to delete + * @throws PHPExcel\Exception + */ + public function deleteCacheData($pCoord) + { + // Delete the entry from Wincache + wincache_ucache_delete($this->cachePrefix.$pCoord.'.cache'); + + // Delete the entry from our cell address array + parent::deleteCacheData($pCoord); + } + + /** + * Clone the cell collection + * + * @param PHPExcel\Worksheet $parent The new worksheet + * @return void + */ + public function copyCellCollection(Worksheet $parent) + { + parent::copyCellCollection($parent); + // Get a new id for the new file name + $baseUnique = $this->getUniqueID(); + $newCachePrefix = substr(md5($baseUnique), 0, 8).'.'; + $cacheList = $this->getCellList(); + foreach ($cacheList as $cellID) { + if ($cellID != $this->currentObjectID) { + $success = false; + $obj = wincache_ucache_get($this->cachePrefix.$cellID.'.cache', $success); + if (!$success) { + // Entry no longer exists in WinCache, so clear it from the cache array + parent::deleteCacheData($cellID); + throw new Exception('Cell entry '.$cellID.' no longer exists in Wincache'); + } + if (!wincache_ucache_add($newCachePrefix.$cellID.'.cache', $obj, $this->cacheTime)) { + $this->__destruct(); + throw new Exception('Failed to store cell '.$cellID.' in Wincache'); + } + } + } + $this->cachePrefix = $newCachePrefix; + } + + /** + * Clear the cell collection and disconnect from our parent + * + * @return void + */ + public function unsetWorksheetCells() + { + if (!is_null($this->currentObject)) { + $this->currentObject->detach(); + $this->currentObject = $this->currentObjectID = null; + } + + // Flush the WinCache cache + $this->__destruct(); + + $this->cellCache = array(); + + // detach ourself from the worksheet, so that it can then delete this object successfully + $this->parent = null; + } + + /** + * Initialise this new cell collection + * + * @param PHPExcel\Worksheet $parent The worksheet for this cell collection + * @param array of mixed $arguments Additional initialisation arguments + */ + public function __construct(Worksheet $parent, $arguments) + { + $cacheTime = (isset($arguments['cacheTime'])) ? $arguments['cacheTime'] : 600; + + if (is_null($this->cachePrefix)) { + $baseUnique = $this->getUniqueID(); + $this->cachePrefix = substr(md5($baseUnique), 0, 8).'.'; + $this->cacheTime = $cacheTime; + + parent::__construct($parent); + } + } + + /** + * Destroy this cell collection + */ + public function __destruct() + { + $cacheList = $this->getCellList(); + foreach ($cacheList as $cellID) { + wincache_ucache_delete($this->cachePrefix.$cellID.'.cache'); + } + } + + /** + * Identify whether the caching method is currently available + * Some methods are dependent on the availability of certain extensions being enabled in the PHP build + * + * @return boolean + */ + public static function cacheMethodIsAvailable() + { + return function_exists('wincache_ucache_add'); + } +} diff --git a/Classes/PHPExcel/CalcEngine/CyclicReferenceStack.php b/Classes/PHPExcel/CalcEngine/CyclicReferenceStack.php index 8a4467b..ce8d297 100644 --- a/Classes/PHPExcel/CalcEngine/CyclicReferenceStack.php +++ b/Classes/PHPExcel/CalcEngine/CyclicReferenceStack.php @@ -35,7 +35,8 @@ namespace PHPExcel; * @package PHPExcel\Calculation * @copyright Copyright (c) 2006 - 2013 PHPExcel (http://www.codeplex.com/PHPExcel) */ -class CalcEngine_CyclicReferenceStack { +class CalcEngine_CyclicReferenceStack +{ /** * The call stack for calculated cells @@ -50,7 +51,8 @@ class CalcEngine_CyclicReferenceStack { * * @return integer */ - public function count() { + public function count() + { return count($this->stack); } @@ -59,7 +61,8 @@ class CalcEngine_CyclicReferenceStack { * * @param mixed $value */ - public function push($value) { + public function push($value) + { $this->stack[] = $value; } // function push() @@ -68,7 +71,8 @@ class CalcEngine_CyclicReferenceStack { * * @return mixed */ - public function pop() { + public function pop() + { return array_pop($this->stack); } // function pop() @@ -77,14 +81,16 @@ class CalcEngine_CyclicReferenceStack { * * @param mixed $value The value to test */ - public function onStack($value) { + public function onStack($value) + { return in_array($value, $this->stack); } /** * Clear the stack */ - public function clear() { + public function clear() + { $this->stack = array(); } // function push() @@ -93,7 +99,8 @@ class CalcEngine_CyclicReferenceStack { * * @return mixed[] */ - public function showStack() { + public function showStack() + { return $this->stack; } } diff --git a/Classes/PHPExcel/CalcEngine/Logger.php b/Classes/PHPExcel/CalcEngine/Logger.php index d66b2f2..01b6848 100644 --- a/Classes/PHPExcel/CalcEngine/Logger.php +++ b/Classes/PHPExcel/CalcEngine/Logger.php @@ -34,8 +34,8 @@ namespace PHPExcel; * @package PHPExcel\Calculation * @copyright Copyright (c) 2006 - 2013 PHPExcel (http://www.codeplex.com/PHPExcel) */ -class CalcEngine_Logger { - +class CalcEngine_Logger +{ /** * Flag to determine whether a debug log should be generated by the calculation engine * If true, then a debug log will be generated @@ -43,7 +43,7 @@ class CalcEngine_Logger { * * @var boolean */ - private $_writeDebugLog = false; + private $writeDebugLog = false; /** * Flag to determine whether a debug log should be echoed by the calculation engine @@ -53,21 +53,21 @@ class CalcEngine_Logger { * * @var boolean */ - private $_echoDebugLog = false; + private $echoDebugLog = false; /** * The debug log generated by the calculation engine * * @var string[] */ - private $_debugLog = array(); + private $debugLog = array(); /** * The calculation engine cell reference stack * * @var PHPExcel\CalcEngine_CyclicReferenceStack */ - private $_cellStack; + private $cellStack; /** @@ -75,8 +75,9 @@ class CalcEngine_Logger { * * @param PHPExcel\CalcEngine_CyclicReferenceStack $stack */ - public function __construct(CalcEngine_CyclicReferenceStack $stack) { - $this->_cellStack = $stack; + public function __construct(CalcEngine_CyclicReferenceStack $stack) + { + $this->cellStack = $stack; } /** @@ -84,8 +85,9 @@ class CalcEngine_Logger { * * @param boolean $pValue */ - public function setWriteDebugLog($pValue = false) { - $this->_writeDebugLog = $pValue; + public function setWriteDebugLog($pValue = false) + { + $this->writeDebugLog = $pValue; } /** @@ -93,8 +95,9 @@ class CalcEngine_Logger { * * @return boolean */ - public function getWriteDebugLog() { - return $this->_writeDebugLog; + public function getWriteDebugLog() + { + return $this->writeDebugLog; } /** @@ -102,8 +105,9 @@ class CalcEngine_Logger { * * @param boolean $pValue */ - public function setEchoDebugLog($pValue = false) { - $this->_echoDebugLog = $pValue; + public function setEchoDebugLog($pValue = false) + { + $this->echoDebugLog = $pValue; } /** @@ -111,26 +115,28 @@ class CalcEngine_Logger { * * @return boolean */ - public function getEchoDebugLog() { - return $this->_echoDebugLog; + public function getEchoDebugLog() + { + return $this->echoDebugLog; } /** * Write an entry to the calculation engine debug log */ - public function writeDebugLog() { + public function writeDebugLog() + { // Only write the debug log if logging is enabled - if ($this->_writeDebugLog) { + if ($this->writeDebugLog) { $message = implode(func_get_args()); - $cellReference = implode(' -> ', $this->_cellStack->showStack()); - if ($this->_echoDebugLog) { + $cellReference = implode(' -> ', $this->cellStack->showStack()); + if ($this->echoDebugLog) { echo $cellReference, - ($this->_cellStack->count() > 0 ? ' => ' : ''), + ($this->cellStack->count() > 0 ? ' => ' : ''), $message, PHP_EOL; } - $this->_debugLog[] = $cellReference . - ($this->_cellStack->count() > 0 ? ' => ' : '') . + $this->debugLog[] = $cellReference . + ($this->cellStack->count() > 0 ? ' => ' : '') . $message; } } @@ -138,8 +144,9 @@ class CalcEngine_Logger { /** * Clear the calculation engine debug log */ - public function clearLog() { - $this->_debugLog = array(); + public function clearLog() + { + $this->debugLog = array(); } /** @@ -147,7 +154,8 @@ class CalcEngine_Logger { * * @return string[] */ - public function getLog() { - return $this->_debugLog; + public function getLog() + { + return $this->debugLog; } } diff --git a/Classes/PHPExcel/Calculation/Exception.php b/Classes/PHPExcel/Calculation/Exception.php index 76e57c8..c33bccc 100644 --- a/Classes/PHPExcel/Calculation/Exception.php +++ b/Classes/PHPExcel/Calculation/Exception.php @@ -33,7 +33,8 @@ * @package PHPExcel\Calculation * @copyright Copyright (c) 2006 - 2013 PHPExcel (http://www.codeplex.com/PHPExcel) */ -class Calculation_Exception extends Exception { +class Calculation_Exception extends Exception +{ /** * Error handler callback * @@ -43,7 +44,8 @@ class Calculation_Exception extends Exception { * @param mixed $line * @param mixed $context */ - public static function errorHandlerCallback($code, $string, $file, $line, $context) { + public static function errorHandlerCallback($code, $string, $file, $line, $context) + { $e = new self($string, $code); $e->line = $line; $e->file = $file; diff --git a/Classes/PHPExcel/Exception.php b/Classes/PHPExcel/Exception.php index 7481afe..2208ed1 100644 --- a/Classes/PHPExcel/Exception.php +++ b/Classes/PHPExcel/Exception.php @@ -35,7 +35,8 @@ namespace PHPExcel; * @package PHPExcel * @copyright Copyright (c) 2006 - 2013 PHPExcel (http://www.codeplex.com/PHPExcel) */ -class Exception extends \Exception { +class Exception extends \Exception +{ /** * Error handler callback * @@ -45,7 +46,8 @@ class Exception extends \Exception { * @param mixed $line * @param mixed $context */ - public static function errorHandlerCallback($code, $string, $file, $line, $context) { + public static function errorHandlerCallback($code, $string, $file, $line, $context) + { $e = new self($string, $code); $e->line = $line; $e->file = $file; diff --git a/Classes/PHPExcel/HashTable.php b/Classes/PHPExcel/HashTable.php index 0526a10..b1ce095 100644 --- a/Classes/PHPExcel/HashTable.php +++ b/Classes/PHPExcel/HashTable.php @@ -42,14 +42,14 @@ class HashTable * * @var array */ - public $_items = array(); + public $items = array(); /** * HashTable key map * * @var array */ - public $_keyMap = array(); + public $keyMap = array(); /** * Create a new PHPExcel\HashTable @@ -71,7 +71,8 @@ class HashTable * @param PHPExcel\IComparable[] $pSource Source array to create HashTable from * @throws PHPExcel\Exception */ - public function addFromSource($pSource = null) { + public function addFromSource($pSource = null) + { // Check if an array was passed if ($pSource == null) { return; @@ -90,11 +91,12 @@ class HashTable * @param PHPExcel\IComparable $pSource Item to add * @throws PHPExcel\Exception */ - public function add(IComparable $pSource = null) { + public function add(IComparable $pSource = null) + { $hash = $pSource->getHashCode(); if (!isset($this->_items[$hash])) { - $this->_items[$hash] = $pSource; - $this->_keyMap[count($this->_items) - 1] = $hash; + $this->items[$hash] = $pSource; + $this->keyMap[count($this->items) - 1] = $hash; } } @@ -104,22 +106,23 @@ class HashTable * @param PHPExcel\IComparable $pSource Item to remove * @throws PHPExcel\Exception */ - public function remove(IComparable $pSource = null) { + public function remove(IComparable $pSource = null) + { $hash = $pSource->getHashCode(); - if (isset($this->_items[$hash])) { - unset($this->_items[$hash]); + if (isset($this->items[$hash])) { + unset($this->items[$hash]); $deleteKey = -1; - foreach ($this->_keyMap as $key => $value) { + foreach ($this->keyMap as $key => $value) { if ($deleteKey >= 0) { - $this->_keyMap[$key - 1] = $value; + $this->keyMap[$key - 1] = $value; } if ($value == $hash) { $deleteKey = $key; } } - unset($this->_keyMap[count($this->_keyMap) - 1]); + unset($this->keyMap[count($this->keyMap) - 1]); } } @@ -127,9 +130,10 @@ class HashTable * Clear HashTable * */ - public function clear() { - $this->_items = array(); - $this->_keyMap = array(); + public function clear() + { + $this->items = array(); + $this->keyMap = array(); } /** @@ -137,8 +141,9 @@ class HashTable * * @return int */ - public function count() { - return count($this->_items); + public function count() + { + return count($this->items); } /** @@ -147,8 +152,9 @@ class HashTable * @param string $pHashCode * @return int Index */ - public function getIndexForHashCode($pHashCode = '') { - return array_search($pHashCode, $this->_keyMap); + public function getIndexForHashCode($pHashCode = '') + { + return array_search($pHashCode, $this->keyMap); } /** @@ -158,9 +164,10 @@ class HashTable * @return PHPExcel\IComparable * */ - public function getByIndex($pIndex = 0) { - if (isset($this->_keyMap[$pIndex])) { - return $this->getByHashCode( $this->_keyMap[$pIndex] ); + public function getByIndex($pIndex = 0) + { + if (isset($this->keyMap[$pIndex])) { + return $this->getByHashCode( $this->keyMap[$pIndex] ); } return null; @@ -173,9 +180,10 @@ class HashTable * @return PHPExcel\IComparable * */ - public function getByHashCode($pHashCode = '') { - if (isset($this->_items[$pHashCode])) { - return $this->_items[$pHashCode]; + public function getByHashCode($pHashCode = '') + { + if (isset($this->items[$pHashCode])) { + return $this->items[$pHashCode]; } return null; @@ -186,14 +194,16 @@ class HashTable * * @return PHPExcel\IComparable[] */ - public function toArray() { - return $this->_items; + public function toArray() + { + return $this->items; } /** * Implement PHP __clone to create a deep clone, not just a shallow copy. */ - public function __clone() { + public function __clone() + { $vars = get_object_vars($this); foreach ($vars as $key => $value) { if (is_object($value)) { diff --git a/Classes/PHPExcel/Reader/Exception.php b/Classes/PHPExcel/Reader/Exception.php index 573fe10..633b415 100644 --- a/Classes/PHPExcel/Reader/Exception.php +++ b/Classes/PHPExcel/Reader/Exception.php @@ -35,7 +35,8 @@ namespace PHPExcel; * @package PHPExcel\Reader * @copyright Copyright (c) 2006 - 2013 PHPExcel (http://www.codeplex.com/PHPExcel) */ -class Reader_Exception extends Exception { +class Reader_Exception extends Exception +{ /** * Error handler callback * @@ -45,7 +46,8 @@ class Reader_Exception extends Exception { * @param mixed $line * @param mixed $context */ - public static function errorHandlerCallback($code, $string, $file, $line, $context) { + public static function errorHandlerCallback($code, $string, $file, $line, $context) + { $e = new self($string, $code); $e->line = $line; $e->file = $file; diff --git a/Classes/PHPExcel/Writer/Exception.php b/Classes/PHPExcel/Writer/Exception.php index 47dfd28..b37be54 100644 --- a/Classes/PHPExcel/Writer/Exception.php +++ b/Classes/PHPExcel/Writer/Exception.php @@ -35,7 +35,8 @@ namespace PHPExcel; * @package PHPExcel\Writer * @copyright Copyright (c) 2006 - 2013 PHPExcel (http://www.codeplex.com/PHPExcel) */ -class Writer_Exception extends Exception { +class Writer_Exception extends Exception +{ /** * Error handler callback * @@ -45,7 +46,8 @@ class Writer_Exception extends Exception { * @param mixed $line * @param mixed $context */ - public static function errorHandlerCallback($code, $string, $file, $line, $context) { + public static function errorHandlerCallback($code, $string, $file, $line, $context) + { $e = new self($string, $code); $e->line = $line; $e->file = $file; diff --git a/Classes/PHPExcel/Writer/HTML.php b/Classes/PHPExcel/Writer/HTML.php index aaa2a5a..2c1f377 100644 --- a/Classes/PHPExcel/Writer/HTML.php +++ b/Classes/PHPExcel/Writer/HTML.php @@ -35,113 +35,115 @@ namespace PHPExcel; * @package PHPExcel\Writer_HTML * @copyright Copyright (c) 2006 - 2013 PHPExcel (http://www.codeplex.com/PHPExcel) */ -class Writer_HTML extends Writer_Abstract implements Writer_IWriter { +class Writer_HTML extends Writer_Abstract implements Writer_IWriter +{ /** * PHPExcel object * * @var PHPExcel */ - protected $_phpExcel; + protected $phpExcel; /** * Sheet index to write * * @var int */ - private $_sheetIndex = 0; + private $sheetIndex = 0; /** * Images root * * @var string */ - private $_imagesRoot = '.'; + private $imagesRoot = '.'; /** * embed images, or link to images * * @var boolean */ - private $_embedImages = false; + private $embedImages = false; /** * Use inline CSS? * * @var boolean */ - private $_useInlineCss = false; + private $useInlineCss = false; /** * Array of CSS styles * * @var array */ - private $_cssStyles = null; + private $cssStyles = null; /** * Array of column widths in points * * @var array */ - private $_columnWidths = null; + private $columnWidths = null; /** * Default font * * @var PHPExcel\Style_Font */ - private $_defaultFont; + private $defaultFont; /** * Flag whether spans have been calculated * * @var boolean */ - private $_spansAreCalculated = false; + private $spansAreCalculated = false; /** * Excel cells that should not be written as HTML cells * * @var array */ - private $_isSpannedCell = array(); + private $isSpannedCell = array(); /** * Excel cells that are upper-left corner in a cell merge * * @var array */ - private $_isBaseCell = array(); + private $isBaseCell = array(); /** * Excel rows that should not be written as HTML rows * * @var array */ - private $_isSpannedRow = array(); + private $isSpannedRow = array(); /** * Is the current writer creating PDF? * * @var boolean */ - protected $_isPdf = false; + protected $isPdf = false; /** * Generate the Navigation block * * @var boolean */ - private $_generateSheetNavigationBlock = true; + private $generateSheetNavigationBlock = true; /** * Create a new PHPExcel\Writer_HTML * * @param PHPExcel\Workbook $phpExcel PHPExcel object */ - public function __construct(Workbook $phpExcel) { - $this->_phpExcel = $phpExcel; - $this->_defaultFont = $this->_phpExcel->getDefaultStyle()->getFont(); + public function __construct(Workbook $phpExcel) + { + $this->phpExcel = $phpExcel; + $this->defaultFont = $this->phpExcel->getDefaultStyle()->getFont(); } /** @@ -150,17 +152,18 @@ class Writer_HTML extends Writer_Abstract implements Writer_IWriter { * @param string $pFilename * @throws PHPExcel\Writer_Exception */ - public function save($pFilename = null) { + public function save($pFilename = null) + { // garbage collect - $this->_phpExcel->garbageCollect(); + $this->phpExcel->garbageCollect(); - $saveDebugLog = Calculation::getInstance($this->_phpExcel)->getDebugLog()->getWriteDebugLog(); - Calculation::getInstance($this->_phpExcel)->getDebugLog()->setWriteDebugLog(false); + $saveDebugLog = Calculation::getInstance($this->phpExcel)->getDebugLog()->getWriteDebugLog(); + Calculation::getInstance($this->phpExcel)->getDebugLog()->setWriteDebugLog(false); $saveArrayReturnType = Calculation::getArrayReturnType(); Calculation::setArrayReturnType(Calculation::RETURN_ARRAY_AS_VALUE); // Build CSS - $this->buildCSS(!$this->_useInlineCss); + $this->buildCSS(!$this->useInlineCss); // Open file $fileHandle = fopen($pFilename, 'wb+'); @@ -169,10 +172,10 @@ class Writer_HTML extends Writer_Abstract implements Writer_IWriter { } // Write headers - fwrite($fileHandle, $this->generateHTMLHeader(!$this->_useInlineCss)); + fwrite($fileHandle, $this->generateHTMLHeader(!$this->useInlineCss)); // Write navigation (tabs) - if ((!$this->_isPdf) && ($this->_generateSheetNavigationBlock)) { + if ((!$this->isPdf) && ($this->generateSheetNavigationBlock)) { fwrite($fileHandle, $this->generateNavigation()); } @@ -186,67 +189,73 @@ class Writer_HTML extends Writer_Abstract implements Writer_IWriter { fclose($fileHandle); Calculation::setArrayReturnType($saveArrayReturnType); - Calculation::getInstance($this->_phpExcel)->getDebugLog()->setWriteDebugLog($saveDebugLog); + Calculation::getInstance($this->phpExcel)->getDebugLog()->setWriteDebugLog($saveDebugLog); } + protected static $vAlignmentMap = array( + Style_Alignment::VERTICAL_BOTTOM => 'bottom', + Style_Alignment::VERTICAL_TOP => 'top', + Style_Alignment::VERTICAL_CENTER => 'middle', + Style_Alignment::VERTICAL_JUSTIFY => 'middle', + ); + /** * Map VAlign * * @param string $vAlign Vertical alignment * @return string */ - private function _mapVAlign($vAlign) { - switch ($vAlign) { - case Style_Alignment::VERTICAL_BOTTOM: return 'bottom'; - case Style_Alignment::VERTICAL_TOP: return 'top'; - case Style_Alignment::VERTICAL_CENTER: - case Style_Alignment::VERTICAL_JUSTIFY: return 'middle'; - default: return 'baseline'; - } + protected function mapVAlign($vAlign) + { + return (isset(self::$vAlignmentMap[$vAlign])) ? self::$vAlignmentMap[$vAlign] : 'baseline'; } + protected static $hAlignmentMap = array( + Style_Alignment::HORIZONTAL_GENERAL => false, + Style_Alignment::HORIZONTAL_LEFT => 'left', + Style_Alignment::HORIZONTAL_RIGHT => 'right', + Style_Alignment::HORIZONTAL_CENTER => 'center', + Style_Alignment::HORIZONTAL_CENTER_CONTINUOUS => 'center', + Style_Alignment::HORIZONTAL_JUSTIFY => 'justify', + ); + /** * Map HAlign * * @param string $hAlign Horizontal alignment * @return string|false */ - private function _mapHAlign($hAlign) { - switch ($hAlign) { - case Style_Alignment::HORIZONTAL_GENERAL: return false; - case Style_Alignment::HORIZONTAL_LEFT: return 'left'; - case Style_Alignment::HORIZONTAL_RIGHT: return 'right'; - case Style_Alignment::HORIZONTAL_CENTER: - case Style_Alignment::HORIZONTAL_CENTER_CONTINUOUS: return 'center'; - case Style_Alignment::HORIZONTAL_JUSTIFY: return 'justify'; - default: return false; - } + protected function mapHAlign($hAlign) + { + return (isset(self::$hAlignmentMap[$hAlign])) ? self::$hAlignmentMap[$hAlign] : false; } + protected static $borderStyleMap = array( + Style_Border::BORDER_NONE => 'none', + Style_Border::BORDER_DASHDOT => '1px dashed', + Style_Border::BORDER_DASHDOTDOT => '1px dotted', + Style_Border::BORDER_DASHED => '1px dashed', + Style_Border::BORDER_DOTTED => '1px dotted', + Style_Border::BORDER_DOUBLE => '3px double', + Style_Border::BORDER_HAIR => '1px solid', + Style_Border::BORDER_MEDIUM => '2px solid', + Style_Border::BORDER_MEDIUMDASHDOT => '2px dashed', + Style_Border::BORDER_MEDIUMDASHDOTDOT => '2px dotted', + Style_Border::BORDER_MEDIUMDASHED => '2px dashed', + Style_Border::BORDER_SLANTDASHDOT => '2px dashed', + Style_Border::BORDER_THICK => '3px solid', + Style_Border::BORDER_THIN => '1px solid', + ); + /** * Map border style * * @param int $borderStyle Sheet index * @return string */ - private function _mapBorderStyle($borderStyle) { - switch ($borderStyle) { - case Style_Border::BORDER_NONE: return 'none'; - case Style_Border::BORDER_DASHDOT: return '1px dashed'; - case Style_Border::BORDER_DASHDOTDOT: return '1px dotted'; - case Style_Border::BORDER_DASHED: return '1px dashed'; - case Style_Border::BORDER_DOTTED: return '1px dotted'; - case Style_Border::BORDER_DOUBLE: return '3px double'; - case Style_Border::BORDER_HAIR: return '1px solid'; - case Style_Border::BORDER_MEDIUM: return '2px solid'; - case Style_Border::BORDER_MEDIUMDASHDOT: return '2px dashed'; - case Style_Border::BORDER_MEDIUMDASHDOTDOT: return '2px dotted'; - case Style_Border::BORDER_MEDIUMDASHED: return '2px dashed'; - case Style_Border::BORDER_SLANTDASHDOT: return '2px dashed'; - case Style_Border::BORDER_THICK: return '3px solid'; - case Style_Border::BORDER_THIN: return '1px solid'; - default: return '1px solid'; // map others to thin - } + protected function mapBorderStyle($borderStyle) + { + return (isset(self::$borderStyleMap[$borderStyle])) ? self::$borderStyleMap[$borderStyle] : '1px solid'; } /** @@ -254,8 +263,9 @@ class Writer_HTML extends Writer_Abstract implements Writer_IWriter { * * @return int */ - public function getSheetIndex() { - return $this->_sheetIndex; + public function getSheetIndex() + { + return $this->sheetIndex; } /** @@ -264,8 +274,9 @@ class Writer_HTML extends Writer_Abstract implements Writer_IWriter { * @param int $pValue Sheet index * @return PHPExcel\Writer_HTML */ - public function setSheetIndex($pValue = 0) { - $this->_sheetIndex = $pValue; + public function setSheetIndex($pValue = 0) + { + $this->sheetIndex = $pValue; return $this; } @@ -274,26 +285,29 @@ class Writer_HTML extends Writer_Abstract implements Writer_IWriter { * * @return boolean */ - public function getGenerateSheetNavigationBlock() { - return $this->_generateSheetNavigationBlock; + public function getGenerateSheetNavigationBlock() + { + return $this->generateSheetNavigationBlock; } /** * Set sheet index * - * @param boolean $pValue Flag indicating whether the sheet navigation block should be generated or not - * @return PHPExcel\Writer_HTML + * @param boolean $pValue Flag indicating whether the sheet navigation block should be generated or not + * @return PHPExcel\Writer_HTML */ - public function setGenerateSheetNavigationBlock($pValue = true) { - $this->_generateSheetNavigationBlock = (bool) $pValue; + public function setGenerateSheetNavigationBlock($pValue = true) + { + $this->generateSheetNavigationBlock = (bool) $pValue; return $this; } /** * Write all sheets (resets sheetIndex to null) */ - public function writeAllSheets() { - $this->_sheetIndex = null; + public function writeAllSheets() + { + $this->sheetIndex = null; return $this; } @@ -304,46 +318,62 @@ class Writer_HTML extends Writer_Abstract implements Writer_IWriter { * @return string * @throws PHPExcel\Writer_Exception */ - public function generateHTMLHeader($pIncludeStyles = false) { + public function generateHTMLHeader($pIncludeStyles = false) + { // PHPExcel object known? - if (is_null($this->_phpExcel)) { + if (is_null($this->phpExcel)) { throw new Writer_Exception('Internal PHPExcel object not set to an instance of an object.'); } // Construct HTML - $properties = $this->_phpExcel->getProperties(); + $properties = $this->phpExcel->getProperties(); $html = '' . PHP_EOL; $html .= '' . PHP_EOL; $html .= '' . PHP_EOL; $html .= ' ' . PHP_EOL; $html .= ' ' . PHP_EOL; - if ($properties->getTitle() > '') + if ($properties->getTitle() > '') { $html .= ' ' . htmlspecialchars($properties->getTitle()) . '' . PHP_EOL; - - if ($properties->getCreator() > '') - $html .= ' ' . PHP_EOL; - if ($properties->getTitle() > '') - $html .= ' ' . PHP_EOL; - if ($properties->getDescription() > '') - $html .= ' ' . PHP_EOL; - if ($properties->getSubject() > '') - $html .= ' ' . PHP_EOL; - if ($properties->getKeywords() > '') - $html .= ' ' . PHP_EOL; - if ($properties->getCategory() > '') - $html .= ' ' . PHP_EOL; - if ($properties->getCompany() > '') - $html .= ' ' . PHP_EOL; - if ($properties->getManager() > '') - $html .= ' ' . PHP_EOL; + } + if ($properties->getCreator() > '') { + $html .= ' ' . PHP_EOL; + } + if ($properties->getTitle() > '') { + $html .= ' ' . PHP_EOL; + } + if ($properties->getDescription() > '') { + $html .= ' ' . PHP_EOL; + } + if ($properties->getSubject() > '') { + $html .= ' ' . PHP_EOL; + } + if ($properties->getKeywords() > '') { + $html .= ' ' . PHP_EOL; + } + if ($properties->getCategory() > '') { + $html .= ' ' . PHP_EOL; + } + if ($properties->getCompany() > '') { + $html .= ' ' . PHP_EOL; + } + if ($properties->getManager() > '') { + $html .= ' ' . PHP_EOL; + } if ($pIncludeStyles) { $html .= $this->generateStyles(true); } - $html .= ' ' . PHP_EOL; - $html .= '' . PHP_EOL; - $html .= ' ' . PHP_EOL; + $html .= ' ' . PHP_EOL . PHP_EOL . + '' . PHP_EOL; // Return return $html; @@ -355,23 +385,24 @@ class Writer_HTML extends Writer_Abstract implements Writer_IWriter { * @return string * @throws PHPExcel\Writer_Exception */ - public function generateSheetData() { + public function generateSheetData() + { // PHPExcel object known? - if (is_null($this->_phpExcel)) { + if (is_null($this->phpExcel)) { throw new Writer_Exception('Internal PHPExcel object not set to an instance of an object.'); } // Ensure that Spans have been calculated? - if (!$this->_spansAreCalculated) { - $this->_calculateSpans(); + if (!$this->spansAreCalculated) { + $this->calculateSpans(); } // Fetch sheets $sheets = array(); - if (is_null($this->_sheetIndex)) { - $sheets = $this->_phpExcel->getAllSheets(); + if (is_null($this->sheetIndex)) { + $sheets = $this->phpExcel->getAllSheets(); } else { - $sheets[] = $this->_phpExcel->getSheet($this->_sheetIndex); + $sheets[] = $this->phpExcel->getSheet($this->sheetIndex); } // Construct HTML @@ -381,7 +412,7 @@ class Writer_HTML extends Writer_Abstract implements Writer_IWriter { $sheetId = 0; foreach ($sheets as $sheet) { // Write table header - $html .= $this->_generateTableHeader($sheet); + $html .= $this->generateTableHeader($sheet); // Get worksheet dimension $dimension = explode(':', $sheet->calculateWorksheetDimension()); @@ -410,7 +441,7 @@ class Writer_HTML extends Writer_Abstract implements Writer_IWriter { // Loop through cells $row = $rowMin-1; - while($row++ < $rowMax) { + while ($row++ < $rowMax) { // ? if ($row == $theadStart) { $html .= ' ' . PHP_EOL; @@ -422,12 +453,12 @@ class Writer_HTML extends Writer_Abstract implements Writer_IWriter { } // Write row if there are HTML table cells in it - if ( !isset($this->_isSpannedRow[$sheet->getParent()->getIndex($sheet)][$row]) ) { + if (!isset($this->isSpannedRow[$sheet->getParent()->getIndex($sheet)][$row])) { // Start a new rowData $rowData = array(); // Loop through columns $column = $dimension[0][0] - 1; - while($column++ < $dimension[1][0]) { + while ($column++ < $dimension[1][0]) { // Cell exists? if ($sheet->cellExistsByColumnAndRow($column, $row)) { $rowData[$column] = $sheet->getCellByColumnAndRow($column, $row); @@ -435,7 +466,7 @@ class Writer_HTML extends Writer_Abstract implements Writer_IWriter { $rowData[$column] = ''; } } - $html .= $this->_generateRow($sheet, $rowData, $row - 1); + $html .= $this->generateRow($sheet, $rowData, $row - 1); } // ? @@ -443,17 +474,17 @@ class Writer_HTML extends Writer_Abstract implements Writer_IWriter { $html .= ' ' . PHP_EOL; } } - $html .= $this->_extendRowsForChartsAndImages($sheet, $row); + $html .= $this->extendRowsForChartsAndImages($sheet, $row); // Close table body. $html .= ' ' . PHP_EOL; // Write table footer - $html .= $this->_generateTableFooter(); + $html .= $this->generateTableFooter(); // Writing PDF? - if ($this->_isPdf) { - if (is_null($this->_sheetIndex) && $sheetId + 1 < $this->_phpExcel->getSheetCount()) { + if ($this->isPdf) { + if (is_null($this->sheetIndex) && $sheetId + 1 < $this->phpExcel->getSheetCount()) { $html .= '
'; } } @@ -475,16 +506,16 @@ class Writer_HTML extends Writer_Abstract implements Writer_IWriter { public function generateNavigation() { // PHPExcel object known? - if (is_null($this->_phpExcel)) { + if (is_null($this->phpExcel)) { throw new Writer_Exception('Internal PHPExcel object not set to an instance of an object.'); } // Fetch sheets $sheets = array(); - if (is_null($this->_sheetIndex)) { - $sheets = $this->_phpExcel->getAllSheets(); + if (is_null($this->sheetIndex)) { + $sheets = $this->phpExcel->getAllSheets(); } else { - $sheets[] = $this->_phpExcel->getSheet($this->_sheetIndex); + $sheets[] = $this->phpExcel->getSheet($this->sheetIndex); } // Construct HTML @@ -498,7 +529,8 @@ class Writer_HTML extends Writer_Abstract implements Writer_IWriter { $html .= '