From e83c359c7c8e5798feb0a7790e90a5efa896f00f Mon Sep 17 00:00:00 2001 From: MarkBaker Date: Sun, 3 May 2015 23:37:32 +0100 Subject: [PATCH] Move toward PSR-2 coding standards --- Classes/PHPExcel.php | 769 +++++++++--------- Classes/PHPExcel/Autoloader.php | 62 +- Classes/PHPExcel/CachedObjectStorage/APC.php | 145 ++-- .../CachedObjectStorage/CacheBase.php | 744 +++++++++-------- .../PHPExcel/CachedObjectStorage/DiscISAM.php | 427 +++++----- .../PHPExcel/CachedObjectStorage/ICache.php | 113 ++- .../PHPExcel/CachedObjectStorage/Igbinary.php | 301 ++++--- .../PHPExcel/CachedObjectStorage/Memcache.php | 620 +++++++------- .../PHPExcel/CachedObjectStorage/Memory.php | 243 +++--- .../CachedObjectStorage/MemoryGZip.php | 270 +++--- .../CachedObjectStorage/MemorySerialized.php | 266 +++--- .../PHPExcel/CachedObjectStorage/PHPTemp.php | 406 +++++---- .../PHPExcel/CachedObjectStorage/SQLite.php | 613 +++++++------- .../PHPExcel/CachedObjectStorage/SQLite3.php | 691 ++++++++-------- .../PHPExcel/CachedObjectStorage/Wincache.php | 583 +++++++------ .../PHPExcel/CachedObjectStorageFactory.php | 86 +- .../CalcEngine/CyclicReferenceStack.php | 132 ++- Classes/PHPExcel/CalcEngine/Logger.php | 236 +++--- Classes/PHPExcel/Calculation/Token/Stack.php | 167 ++-- 19 files changed, 3390 insertions(+), 3484 deletions(-) diff --git a/Classes/PHPExcel.php b/Classes/PHPExcel.php index c94fa975..c3ec0519 100644 --- a/Classes/PHPExcel.php +++ b/Classes/PHPExcel.php @@ -1,4 +1,11 @@ _hasMacros; - } + /** + * The workbook has macros ? + * + * @return true if workbook has macros, false if not + */ + public function hasMacros() + { + return $this->hasMacros; + } - /** - * Define if a workbook has macros - * - * @param true|false - */ - public function setHasMacros($hasMacros=false){ - $this->_hasMacros=(bool)$hasMacros; - } + /** + * Define if a workbook has macros + * + * @param boolean $hasMacros true|false + */ + public function setHasMacros($hasMacros = false) + { + $this->hasMacros = (bool) $hasMacros; + } - /** - * Set the macros code - * - * @param binary string|null - */ - public function setMacrosCode($MacrosCode){ - $this->_macrosCode=$MacrosCode; - $this->setHasMacros(!is_null($MacrosCode)); - } + /** + * Set the macros code + * + * @param string $MacrosCode string|null + */ + public function setMacrosCode($MacrosCode = null) + { + $this->macrosCode=$MacrosCode; + $this->setHasMacros(!is_null($MacrosCode)); + } - /** - * Return the macros code - * - * @return binary|null - */ - public function getMacrosCode(){ - return $this->_macrosCode; - } + /** + * Return the macros code + * + * @return string|null + */ + public function getMacrosCode() + { + return $this->macrosCode; + } - /** - * Set the macros certificate - * - * @param binary|null - */ - public function setMacrosCertificate($Certificate=NULL){ - $this->_macrosCertificate=$Certificate; - } + /** + * Set the macros certificate + * + * @param string|null $Certificate + */ + public function setMacrosCertificate($Certificate = null) + { + $this->macrosCertificate=$Certificate; + } - /** - * Is the project signed ? - * - * @return true|false - */ - public function hasMacrosCertificate(){ - return !is_null($this->_macrosCertificate); - } + /** + * Is the project signed ? + * + * @return boolean true|false + */ + public function hasMacrosCertificate() + { + return !is_null($this->macrosCertificate); + } - /** - * Return the macros certificate - * - * @return binary|null - */ - public function getMacrosCertificate(){ - return $this->_macrosCertificate; - } + /** + * Return the macros certificate + * + * @return string|null + */ + public function getMacrosCertificate() + { + return $this->macrosCertificate; + } - /** - * Remove all macros, certificate from spreadsheet - * - * @param none - * @return void - */ - public function discardMacros(){ - $this->_hasMacros=false; - $this->_macrosCode=NULL; - $this->_macrosCertificate=NULL; - } + /** + * Remove all macros, certificate from spreadsheet + * + */ + public function discardMacros() + { + $this->hasMacros=false; + $this->macrosCode=null; + $this->macrosCertificate=null; + } - /** - * set ribbon XML data - * - */ - public function setRibbonXMLData($Target=NULL, $XMLData=NULL){ - if(!is_null($Target) && !is_null($XMLData)){ - $this->_ribbonXMLData=array('target'=>$Target, 'data'=>$XMLData); - }else{ - $this->_ribbonXMLData=NULL; - } - } + /** + * set ribbon XML data + * + */ + public function setRibbonXMLData($Target = null, $XMLData = null) + { + if (!is_null($Target) && !is_null($XMLData)) { + $this->ribbonXMLData = array('target' => $Target, 'data' => $XMLData); + } else { + $this->ribbonXMLData = null; + } + } - /** - * retrieve ribbon XML Data - * - * return string|null|array - */ - public function getRibbonXMLData($What='all'){//we need some constants here... - $ReturnData=NULL; - $What=strtolower($What); - switch($What){ - case 'all': - $ReturnData=$this->_ribbonXMLData; - break; - case 'target': - case 'data': - if(is_array($this->_ribbonXMLData) && array_key_exists($What,$this->_ribbonXMLData)){ - $ReturnData=$this->_ribbonXMLData[$What]; - }//else $ReturnData stay at null - break; - }//default: $ReturnData at null - return $ReturnData; - } + /** + * retrieve ribbon XML Data + * + * return string|null|array + */ + public function getRibbonXMLData($What = 'all') //we need some constants here... + { + $ReturnData = null; + $What = strtolower($What); + switch ($What){ + case 'all': + $ReturnData = $this->ribbonXMLData; + break; + case 'target': + case 'data': + if (is_array($this->ribbonXMLData) && array_key_exists($What, $this->ribbonXMLData)) { + $ReturnData = $this->ribbonXMLData[$What]; + } + break; + } - /** - * store binaries ribbon objects (pictures) - * - */ - public function setRibbonBinObjects($BinObjectsNames=NULL, $BinObjectsData=NULL){ - if(!is_null($BinObjectsNames) && !is_null($BinObjectsData)){ - $this->_ribbonBinObjects=array('names'=>$BinObjectsNames, 'data'=>$BinObjectsData); - }else{ - $this->_ribbonBinObjects=NULL; - } - } - /** - * return the extension of a filename. Internal use for a array_map callback (php<5.3 don't like lambda function) - * - */ - private function _getExtensionOnly($ThePath){ - return pathinfo($ThePath, PATHINFO_EXTENSION); - } + return $ReturnData; + } - /** - * retrieve Binaries Ribbon Objects - * - */ - public function getRibbonBinObjects($What='all'){ - $ReturnData=NULL; - $What=strtolower($What); - switch($What){ - case 'all': - return $this->_ribbonBinObjects; - break; - case 'names': - case 'data': - if(is_array($this->_ribbonBinObjects) && array_key_exists($What, $this->_ribbonBinObjects)){ - $ReturnData=$this->_ribbonBinObjects[$What]; - } - break; - case 'types': - if(is_array($this->_ribbonBinObjects) && array_key_exists('data', $this->_ribbonBinObjects) && is_array($this->_ribbonBinObjects['data'])){ - $tmpTypes=array_keys($this->_ribbonBinObjects['data']); - $ReturnData=array_unique(array_map(array($this,'_getExtensionOnly'), $tmpTypes)); - }else - $ReturnData=array();//the caller want an array... not null if empty - break; - } - return $ReturnData; - } + /** + * store binaries ribbon objects (pictures) + * + */ + public function setRibbonBinObjects($BinObjectsNames = null, $BinObjectsData = null) + { + if (!is_null($BinObjectsNames) && !is_null($BinObjectsData)) { + $this->ribbonBinObjects = array('names' => $BinObjectsNames, 'data' => $BinObjectsData); + } else { + $this->ribbonBinObjects = null; + } + } + /** + * return the extension of a filename. Internal use for a array_map callback (php<5.3 don't like lambda function) + * + */ + private function getExtensionOnly($ThePath) + { + return pathinfo($ThePath, PATHINFO_EXTENSION); + } - /** - * This workbook have a custom UI ? - * - * @return true|false - */ - public function hasRibbon(){ - return !is_null($this->_ribbonXMLData); - } + /** + * retrieve Binaries Ribbon Objects + * + */ + public function getRibbonBinObjects($What = 'all') + { + $ReturnData = null; + $What = strtolower($What); + switch($What) { + case 'all': + return $this->ribbonBinObjects; + break; + case 'names': + case 'data': + if (is_array($this->ribbonBinObjects) && array_key_exists($What, $this->ribbonBinObjects)) { + $ReturnData=$this->ribbonBinObjects[$What]; + } + break; + case 'types': + if (is_array($this->ribbonBinObjects) && + array_key_exists('data', $this->ribbonBinObjects) && is_array($this->ribbonBinObjects['data'])) { + $tmpTypes=array_keys($this->ribbonBinObjects['data']); + $ReturnData = array_unique(array_map(array($this, 'getExtensionOnly'), $tmpTypes)); + } else { + $ReturnData=array(); // the caller want an array... not null if empty + } + break; + } + return $ReturnData; + } - /** - * This workbook have additionnal object for the ribbon ? - * - * @return true|false - */ - public function hasRibbonBinObjects(){ - return !is_null($this->_ribbonBinObjects); - } + /** + * This workbook have a custom UI ? + * + * @return true|false + */ + public function hasRibbon() + { + return !is_null($this->ribbonXMLData); + } - /** + /** + * This workbook have additionnal object for the ribbon ? + * + * @return true|false + */ + public function hasRibbonBinObjects() + { + return !is_null($this->ribbonBinObjects); + } + + /** * Check if a sheet with a specified code name already exists * * @param string $pSheetCodeName Name of the worksheet to check @@ -330,52 +337,52 @@ class PHPExcel */ public function sheetCodeNameExists($pSheetCodeName) { - return ($this->getSheetByCodeName($pSheetCodeName) !== NULL); + return ($this->getSheetByCodeName($pSheetCodeName) !== null); } - /** - * Get sheet by code name. Warning : sheet don't have always a code name ! - * - * @param string $pName Sheet name - * @return PHPExcel_Worksheet - */ - public function getSheetByCodeName($pName = '') - { - $worksheetCount = count($this->_workSheetCollection); - for ($i = 0; $i < $worksheetCount; ++$i) { - if ($this->_workSheetCollection[$i]->getCodeName() == $pName) { - return $this->_workSheetCollection[$i]; - } - } + /** + * Get sheet by code name. Warning : sheet don't have always a code name ! + * + * @param string $pName Sheet name + * @return PHPExcel_Worksheet + */ + public function getSheetByCodeName($pName = '') + { + $worksheetCount = count($this->workSheetCollection); + for ($i = 0; $i < $worksheetCount; ++$i) { + if ($this->workSheetCollection[$i]->getCodeName() == $pName) { + return $this->workSheetCollection[$i]; + } + } - return null; - } + return null; + } - /** - * Create a new PHPExcel with one Worksheet - */ - public function __construct() - { - $this->_uniqueID = uniqid(); - $this->_calculationEngine = PHPExcel_Calculation::getInstance($this); + /** + * Create a new PHPExcel with one Worksheet + */ + public function __construct() + { + $this->uniqueID = uniqid(); + $this->calculationEngine = PHPExcel_Calculation::getInstance($this); - // Initialise worksheet collection and add one worksheet - $this->_workSheetCollection = array(); - $this->_workSheetCollection[] = new PHPExcel_Worksheet($this); - $this->_activeSheetIndex = 0; + // Initialise worksheet collection and add one worksheet + $this->workSheetCollection = array(); + $this->workSheetCollection[] = new PHPExcel_Worksheet($this); + $this->activeSheetIndex = 0; // Create document properties - $this->_properties = new PHPExcel_DocumentProperties(); + $this->properties = new PHPExcel_DocumentProperties(); // Create document security - $this->_security = new PHPExcel_DocumentSecurity(); + $this->security = new PHPExcel_DocumentSecurity(); // Set named ranges - $this->_namedRanges = array(); + $this->namedRanges = array(); // Create the cellXf supervisor - $this->_cellXfSupervisor = new PHPExcel_Style(true); - $this->_cellXfSupervisor->bindParent($this); + $this->cellXfSupervisor = new PHPExcel_Style(true); + $this->cellXfSupervisor->bindParent($this); // Create the default style $this->addCellXf(new PHPExcel_Style); @@ -386,10 +393,11 @@ class PHPExcel * Code to execute when this worksheet is unset() * */ - public function __destruct() { + public function __destruct() + { PHPExcel_Calculation::unsetInstance($this); $this->disconnectWorksheets(); - } // function __destruct() + } /** * Disconnect all worksheets from this PHPExcel workbook object, @@ -398,24 +406,24 @@ class PHPExcel */ public function disconnectWorksheets() { - $worksheet = NULL; - foreach($this->_workSheetCollection as $k => &$worksheet) { + $worksheet = null; + foreach ($this->workSheetCollection as $k => &$worksheet) { $worksheet->disconnectCells(); - $this->_workSheetCollection[$k] = null; + $this->workSheetCollection[$k] = null; } unset($worksheet); - $this->_workSheetCollection = array(); + $this->workSheetCollection = array(); } - /** - * Return the calculation engine for this worksheet - * - * @return PHPExcel_Calculation - */ - public function getCalculationEngine() - { - return $this->_calculationEngine; - } // function getCellCacheController() + /** + * Return the calculation engine for this worksheet + * + * @return PHPExcel_Calculation + */ + public function getCalculationEngine() + { + return $this->calculationEngine; + } // function getCellCacheController() /** * Get properties @@ -424,7 +432,7 @@ class PHPExcel */ public function getProperties() { - return $this->_properties; + return $this->properties; } /** @@ -434,7 +442,7 @@ class PHPExcel */ public function setProperties(PHPExcel_DocumentProperties $pValue) { - $this->_properties = $pValue; + $this->properties = $pValue; } /** @@ -444,7 +452,7 @@ class PHPExcel */ public function getSecurity() { - return $this->_security; + return $this->security; } /** @@ -454,7 +462,7 @@ class PHPExcel */ public function setSecurity(PHPExcel_DocumentSecurity $pValue) { - $this->_security = $pValue; + $this->security = $pValue; } /** @@ -466,7 +474,7 @@ class PHPExcel */ public function getActiveSheet() { - return $this->getSheet($this->_activeSheetIndex); + return $this->getSheet($this->activeSheetIndex); } /** @@ -476,7 +484,7 @@ class PHPExcel * @return PHPExcel_Worksheet * @throws PHPExcel_Exception */ - public function createSheet($iSheetIndex = NULL) + public function createSheet($iSheetIndex = null) { $newSheet = new PHPExcel_Worksheet($this); $this->addSheet($newSheet, $iSheetIndex); @@ -491,7 +499,7 @@ class PHPExcel */ public function sheetNameExists($pSheetName) { - return ($this->getSheetByName($pSheetName) !== NULL); + return ($this->getSheetByName($pSheetName) !== null); } /** @@ -502,31 +510,31 @@ class PHPExcel * @return PHPExcel_Worksheet * @throws PHPExcel_Exception */ - public function addSheet(PHPExcel_Worksheet $pSheet, $iSheetIndex = NULL) + public function addSheet(PHPExcel_Worksheet $pSheet, $iSheetIndex = null) { if ($this->sheetNameExists($pSheet->getTitle())) { throw new PHPExcel_Exception( - "Workbook already contains a worksheet named '{$pSheet->getTitle()}'. Rename this worksheet first." + "Workbook already contains a worksheet named '{$pSheet->getTitle()}'. Rename this worksheet first." ); } - if($iSheetIndex === NULL) { - if ($this->_activeSheetIndex < 0) { - $this->_activeSheetIndex = 0; + if ($iSheetIndex === null) { + if ($this->activeSheetIndex < 0) { + $this->activeSheetIndex = 0; } - $this->_workSheetCollection[] = $pSheet; + $this->workSheetCollection[] = $pSheet; } else { // Insert the sheet at the requested index array_splice( - $this->_workSheetCollection, + $this->workSheetCollection, $iSheetIndex, 0, array($pSheet) - ); + ); // Adjust active sheet index if necessary - if ($this->_activeSheetIndex >= $iSheetIndex) { - ++$this->_activeSheetIndex; + if ($this->activeSheetIndex >= $iSheetIndex) { + ++$this->activeSheetIndex; } } @@ -546,19 +554,18 @@ class PHPExcel public function removeSheetByIndex($pIndex = 0) { - $numSheets = count($this->_workSheetCollection); - + $numSheets = count($this->workSheetCollection); if ($pIndex > $numSheets - 1) { throw new PHPExcel_Exception( - "You tried to remove a sheet by the out of bounds index: {$pIndex}. The actual number of sheets is {$numSheets}." + "You tried to remove a sheet by the out of bounds index: {$pIndex}. The actual number of sheets is {$numSheets}." ); } else { - array_splice($this->_workSheetCollection, $pIndex, 1); + array_splice($this->workSheetCollection, $pIndex, 1); } // Adjust active sheet index if necessary - if (($this->_activeSheetIndex >= $pIndex) && - ($pIndex > count($this->_workSheetCollection) - 1)) { - --$this->_activeSheetIndex; + if (($this->activeSheetIndex >= $pIndex) && + ($pIndex > count($this->workSheetCollection) - 1)) { + --$this->activeSheetIndex; } } @@ -572,14 +579,14 @@ class PHPExcel */ public function getSheet($pIndex = 0) { - if (!isset($this->_workSheetCollection[$pIndex])) { + if (!isset($this->workSheetCollection[$pIndex])) { $numSheets = $this->getSheetCount(); throw new PHPExcel_Exception( "Your requested sheet index: {$pIndex} is out of bounds. The actual number of sheets is {$numSheets}." ); } - return $this->_workSheetCollection[$pIndex]; + return $this->workSheetCollection[$pIndex]; } /** @@ -589,7 +596,7 @@ class PHPExcel */ public function getAllSheets() { - return $this->_workSheetCollection; + return $this->workSheetCollection; } /** @@ -600,14 +607,14 @@ class PHPExcel */ public function getSheetByName($pName = '') { - $worksheetCount = count($this->_workSheetCollection); + $worksheetCount = count($this->workSheetCollection); for ($i = 0; $i < $worksheetCount; ++$i) { - if ($this->_workSheetCollection[$i]->getTitle() === $pName) { - return $this->_workSheetCollection[$i]; + if ($this->workSheetCollection[$i]->getTitle() === $pName) { + return $this->workSheetCollection[$i]; } } - return NULL; + return null; } /** @@ -619,7 +626,7 @@ class PHPExcel */ public function getIndex(PHPExcel_Worksheet $pSheet) { - foreach ($this->_workSheetCollection as $key => $value) { + foreach ($this->workSheetCollection as $key => $value) { if ($value->getHashCode() == $pSheet->getHashCode()) { return $key; } @@ -640,12 +647,12 @@ class PHPExcel { $oldIndex = $this->getIndex($this->getSheetByName($sheetName)); $pSheet = array_splice( - $this->_workSheetCollection, + $this->workSheetCollection, $oldIndex, 1 ); array_splice( - $this->_workSheetCollection, + $this->workSheetCollection, $newIndex, 0, $pSheet @@ -660,7 +667,7 @@ class PHPExcel */ public function getSheetCount() { - return count($this->_workSheetCollection); + return count($this->workSheetCollection); } /** @@ -670,7 +677,7 @@ class PHPExcel */ public function getActiveSheetIndex() { - return $this->_activeSheetIndex; + return $this->activeSheetIndex; } /** @@ -682,14 +689,14 @@ class PHPExcel */ public function setActiveSheetIndex($pIndex = 0) { - $numSheets = count($this->_workSheetCollection); + $numSheets = count($this->workSheetCollection); if ($pIndex > $numSheets - 1) { throw new PHPExcel_Exception( - "You tried to set a sheet active by the out of bounds index: {$pIndex}. The actual number of sheets is {$numSheets}." + "You tried to set a sheet active by the out of bounds index: {$pIndex}. The actual number of sheets is {$numSheets}." ); } else { - $this->_activeSheetIndex = $pIndex; + $this->activeSheetIndex = $pIndex; } return $this->getActiveSheet(); } @@ -735,13 +742,14 @@ class PHPExcel * @throws PHPExcel_Exception * @return PHPExcel_Worksheet */ - public function addExternalSheet(PHPExcel_Worksheet $pSheet, $iSheetIndex = null) { + public function addExternalSheet(PHPExcel_Worksheet $pSheet, $iSheetIndex = null) + { if ($this->sheetNameExists($pSheet->getTitle())) { throw new PHPExcel_Exception("Workbook already contains a worksheet named '{$pSheet->getTitle()}'. Rename the external sheet first."); } // count how many cellXfs there are in this workbook currently, we will need this below - $countCellXfs = count($this->_cellXfCollection); + $countCellXfs = count($this->cellXfCollection); // copy all the shared cellXfs from the external workbook and append them to the current foreach ($pSheet->getParent()->getCellXfCollection() as $cellXf) { @@ -754,7 +762,7 @@ class PHPExcel // update the cellXfs foreach ($pSheet->getCellCollection(false) as $cellID) { $cell = $pSheet->getCell($cellID); - $cell->setXfIndex( $cell->getXfIndex() + $countCellXfs ); + $cell->setXfIndex($cell->getXfIndex() + $countCellXfs); } return $this->addSheet($pSheet, $iSheetIndex); @@ -765,8 +773,9 @@ class PHPExcel * * @return PHPExcel_NamedRange[] */ - public function getNamedRanges() { - return $this->_namedRanges; + public function getNamedRanges() + { + return $this->namedRanges; } /** @@ -775,13 +784,14 @@ class PHPExcel * @param PHPExcel_NamedRange $namedRange * @return PHPExcel */ - public function addNamedRange(PHPExcel_NamedRange $namedRange) { + public function addNamedRange(PHPExcel_NamedRange $namedRange) + { if ($namedRange->getScope() == null) { // global scope - $this->_namedRanges[$namedRange->getName()] = $namedRange; + $this->namedRanges[$namedRange->getName()] = $namedRange; } else { // local scope - $this->_namedRanges[$namedRange->getScope()->getTitle().'!'.$namedRange->getName()] = $namedRange; + $this->namedRanges[$namedRange->getScope()->getTitle().'!'.$namedRange->getName()] = $namedRange; } return true; } @@ -793,18 +803,19 @@ class PHPExcel * @param PHPExcel_Worksheet|null $pSheet Scope. Use null for global scope * @return PHPExcel_NamedRange|null */ - public function getNamedRange($namedRange, PHPExcel_Worksheet $pSheet = null) { + public function getNamedRange($namedRange, PHPExcel_Worksheet $pSheet = null) + { $returnValue = null; - if ($namedRange != '' && ($namedRange !== NULL)) { + if ($namedRange != '' && ($namedRange !== null)) { // first look for global defined name - if (isset($this->_namedRanges[$namedRange])) { - $returnValue = $this->_namedRanges[$namedRange]; + if (isset($this->namedRanges[$namedRange])) { + $returnValue = $this->namedRanges[$namedRange]; } // then look for local defined name (has priority over global defined name if both names exist) - if (($pSheet !== NULL) && isset($this->_namedRanges[$pSheet->getTitle() . '!' . $namedRange])) { - $returnValue = $this->_namedRanges[$pSheet->getTitle() . '!' . $namedRange]; + if (($pSheet !== null) && isset($this->namedRanges[$pSheet->getTitle() . '!' . $namedRange])) { + $returnValue = $this->namedRanges[$pSheet->getTitle() . '!' . $namedRange]; } } @@ -818,14 +829,15 @@ class PHPExcel * @param PHPExcel_Worksheet|null $pSheet Scope: use null for global scope. * @return PHPExcel */ - public function removeNamedRange($namedRange, PHPExcel_Worksheet $pSheet = null) { - if ($pSheet === NULL) { - if (isset($this->_namedRanges[$namedRange])) { - unset($this->_namedRanges[$namedRange]); + public function removeNamedRange($namedRange, PHPExcel_Worksheet $pSheet = null) + { + if ($pSheet === null) { + if (isset($this->namedRanges[$namedRange])) { + unset($this->namedRanges[$namedRange]); } } else { - if (isset($this->_namedRanges[$pSheet->getTitle() . '!' . $namedRange])) { - unset($this->_namedRanges[$pSheet->getTitle() . '!' . $namedRange]); + if (isset($this->namedRanges[$pSheet->getTitle() . '!' . $namedRange])) { + unset($this->namedRanges[$pSheet->getTitle() . '!' . $namedRange]); } } return $this; @@ -836,7 +848,8 @@ class PHPExcel * * @return PHPExcel_WorksheetIterator */ - public function getWorksheetIterator() { + public function getWorksheetIterator() + { return new PHPExcel_WorksheetIterator($this); } @@ -845,13 +858,14 @@ class PHPExcel * * @return PHPExcel */ - public function copy() { + public function copy() + { $copied = clone $this; - $worksheetCount = count($this->_workSheetCollection); + $worksheetCount = count($this->workSheetCollection); for ($i = 0; $i < $worksheetCount; ++$i) { - $this->_workSheetCollection[$i] = $this->_workSheetCollection[$i]->copy(); - $this->_workSheetCollection[$i]->rebindParent($this); + $this->workSheetCollection[$i] = $this->workSheetCollection[$i]->copy(); + $this->workSheetCollection[$i]->rebindParent($this); } return $copied; @@ -860,8 +874,9 @@ class PHPExcel /** * Implement PHP __clone to create a deep clone, not just a shallow copy. */ - public function __clone() { - foreach($this as $key => $val) { + public function __clone() + { + foreach ($this as $key => $val) { if (is_object($val) || (is_array($val))) { $this->{$key} = unserialize(serialize($val)); } @@ -875,7 +890,7 @@ class PHPExcel */ public function getCellXfCollection() { - return $this->_cellXfCollection; + return $this->cellXfCollection; } /** @@ -886,7 +901,7 @@ class PHPExcel */ public function getCellXfByIndex($pIndex = 0) { - return $this->_cellXfCollection[$pIndex]; + return $this->cellXfCollection[$pIndex]; } /** @@ -897,7 +912,7 @@ class PHPExcel */ public function getCellXfByHashCode($pValue = '') { - foreach ($this->_cellXfCollection as $cellXf) { + foreach ($this->cellXfCollection as $cellXf) { if ($cellXf->getHashCode() == $pValue) { return $cellXf; } @@ -913,7 +928,7 @@ class PHPExcel */ public function cellXfExists($pCellStyle = null) { - return in_array($pCellStyle, $this->_cellXfCollection, true); + return in_array($pCellStyle, $this->cellXfCollection, true); } /** @@ -924,8 +939,8 @@ class PHPExcel */ public function getDefaultStyle() { - if (isset($this->_cellXfCollection[0])) { - return $this->_cellXfCollection[0]; + if (isset($this->cellXfCollection[0])) { + return $this->cellXfCollection[0]; } throw new PHPExcel_Exception('No default style found for this workbook'); } @@ -937,33 +952,33 @@ class PHPExcel */ public function addCellXf(PHPExcel_Style $style) { - $this->_cellXfCollection[] = $style; - $style->setIndex(count($this->_cellXfCollection) - 1); + $this->cellXfCollection[] = $style; + $style->setIndex(count($this->cellXfCollection) - 1); } /** * Remove cellXf by index. It is ensured that all cells get their xf index updated. * - * @param int $pIndex Index to cellXf + * @param integer $pIndex Index to cellXf * @throws PHPExcel_Exception */ public function removeCellXfByIndex($pIndex = 0) { - if ($pIndex > count($this->_cellXfCollection) - 1) { + if ($pIndex > count($this->cellXfCollection) - 1) { throw new PHPExcel_Exception("CellXf index is out of bounds."); } else { // first remove the cellXf - array_splice($this->_cellXfCollection, $pIndex, 1); + array_splice($this->cellXfCollection, $pIndex, 1); // then update cellXf indexes for cells - foreach ($this->_workSheetCollection as $worksheet) { + foreach ($this->workSheetCollection as $worksheet) { foreach ($worksheet->getCellCollection(false) as $cellID) { $cell = $worksheet->getCell($cellID); $xfIndex = $cell->getXfIndex(); - if ($xfIndex > $pIndex ) { + if ($xfIndex > $pIndex) { // decrease xf index by 1 $cell->setXfIndex($xfIndex - 1); - } else if ($xfIndex == $pIndex) { + } elseif ($xfIndex == $pIndex) { // set to default xf index 0 $cell->setXfIndex(0); } @@ -979,7 +994,7 @@ class PHPExcel */ public function getCellXfSupervisor() { - return $this->_cellXfSupervisor; + return $this->cellXfSupervisor; } /** @@ -989,18 +1004,18 @@ class PHPExcel */ public function getCellStyleXfCollection() { - return $this->_cellStyleXfCollection; + return $this->cellStyleXfCollection; } /** * Get cellStyleXf by index * - * @param int $pIndex + * @param integer $pIndex Index to cellXf * @return PHPExcel_Style */ public function getCellStyleXfByIndex($pIndex = 0) { - return $this->_cellStyleXfCollection[$pIndex]; + return $this->cellStyleXfCollection[$pIndex]; } /** @@ -1011,7 +1026,7 @@ class PHPExcel */ public function getCellStyleXfByHashCode($pValue = '') { - foreach ($this->_cellStyleXfCollection as $cellStyleXf) { + foreach ($this->cellStyleXfCollection as $cellStyleXf) { if ($cellStyleXf->getHashCode() == $pValue) { return $cellStyleXf; } @@ -1026,22 +1041,22 @@ class PHPExcel */ public function addCellStyleXf(PHPExcel_Style $pStyle) { - $this->_cellStyleXfCollection[] = $pStyle; - $pStyle->setIndex(count($this->_cellStyleXfCollection) - 1); + $this->cellStyleXfCollection[] = $pStyle; + $pStyle->setIndex(count($this->cellStyleXfCollection) - 1); } /** * Remove cellStyleXf by index * - * @param int $pIndex + * @param integer $pIndex Index to cellXf * @throws PHPExcel_Exception */ public function removeCellStyleXfByIndex($pIndex = 0) { - if ($pIndex > count($this->_cellStyleXfCollection) - 1) { + if ($pIndex > count($this->cellStyleXfCollection) - 1) { throw new PHPExcel_Exception("CellStyleXf index is out of bounds."); } else { - array_splice($this->_cellStyleXfCollection, $pIndex, 1); + array_splice($this->cellStyleXfCollection, $pIndex, 1); } } @@ -1053,12 +1068,11 @@ class PHPExcel { // how many references are there to each cellXf ? $countReferencesCellXf = array(); - foreach ($this->_cellXfCollection as $index => $cellXf) { + foreach ($this->cellXfCollection as $index => $cellXf) { $countReferencesCellXf[$index] = 0; } foreach ($this->getWorksheetIterator() as $sheet) { - // from cells foreach ($sheet->getCellCollection(false) as $cellID) { $cell = $sheet->getCell($cellID); @@ -1081,48 +1095,47 @@ class PHPExcel // remove cellXfs without references and create mapping so we can update xfIndex // for all cells and columns $countNeededCellXfs = 0; - foreach ($this->_cellXfCollection as $index => $cellXf) { + foreach ($this->cellXfCollection as $index => $cellXf) { if ($countReferencesCellXf[$index] > 0 || $index == 0) { // we must never remove the first cellXf ++$countNeededCellXfs; } else { - unset($this->_cellXfCollection[$index]); + unset($this->cellXfCollection[$index]); } $map[$index] = $countNeededCellXfs - 1; } - $this->_cellXfCollection = array_values($this->_cellXfCollection); + $this->cellXfCollection = array_values($this->cellXfCollection); // update the index for all cellXfs - foreach ($this->_cellXfCollection as $i => $cellXf) { + foreach ($this->cellXfCollection as $i => $cellXf) { $cellXf->setIndex($i); } // make sure there is always at least one cellXf (there should be) - if (empty($this->_cellXfCollection)) { - $this->_cellXfCollection[] = new PHPExcel_Style(); + if (empty($this->cellXfCollection)) { + $this->cellXfCollection[] = new PHPExcel_Style(); } // update the xfIndex for all cells, row dimensions, column dimensions foreach ($this->getWorksheetIterator() as $sheet) { - // for all cells foreach ($sheet->getCellCollection(false) as $cellID) { $cell = $sheet->getCell($cellID); - $cell->setXfIndex( $map[$cell->getXfIndex()] ); + $cell->setXfIndex($map[$cell->getXfIndex()]); } // for all row dimensions foreach ($sheet->getRowDimensions() as $rowDimension) { if ($rowDimension->getXfIndex() !== null) { - $rowDimension->setXfIndex( $map[$rowDimension->getXfIndex()] ); + $rowDimension->setXfIndex($map[$rowDimension->getXfIndex()]); } } // for all column dimensions foreach ($sheet->getColumnDimensions() as $columnDimension) { - $columnDimension->setXfIndex( $map[$columnDimension->getXfIndex()] ); + $columnDimension->setXfIndex($map[$columnDimension->getXfIndex()]); } - // also do garbage collection for all the sheets + // also do garbage collection for all the sheets $sheet->garbageCollect(); } } @@ -1132,8 +1145,8 @@ class PHPExcel * * @return string */ - public function getID() { - return $this->_uniqueID; + public function getID() + { + return $this->uniqueID; } - } diff --git a/Classes/PHPExcel/Autoloader.php b/Classes/PHPExcel/Autoloader.php index 90e5f6cb..3557e865 100644 --- a/Classes/PHPExcel/Autoloader.php +++ b/Classes/PHPExcel/Autoloader.php @@ -1,4 +1,15 @@ = 0) { return spl_autoload_register(array('PHPExcel_Autoloader', 'Load'), true, true); } else { return spl_autoload_register(array('PHPExcel_Autoloader', 'Load')); } - } // function Register() - + } /** * Autoload a class identified by name * * @param string $pClassName Name of the object to load */ - public static function Load($pClassName){ - if ((class_exists($pClassName,FALSE)) || (strpos($pClassName, 'PHPExcel') !== 0)) { - // Either already loaded, or not a PHPExcel class request - return FALSE; + public static function Load($pClassName) + { + if ((class_exists($pClassName, false)) || (strpos($pClassName, 'PHPExcel') !== 0)) { + // Either already loaded, or not a PHPExcel class request + return false; } $pClassFilePath = PHPEXCEL_ROOT . - str_replace('_',DIRECTORY_SEPARATOR,$pClassName) . - '.php'; + str_replace('_', DIRECTORY_SEPARATOR, $pClassName) . + '.php'; - if ((file_exists($pClassFilePath) === FALSE) || (is_readable($pClassFilePath) === FALSE)) { - // Can't load - return FALSE; + if ((file_exists($pClassFilePath) === false) || (is_readable($pClassFilePath) === false)) { + // Can't load + return false; } require($pClassFilePath); - } // function Load() - + } } diff --git a/Classes/PHPExcel/CachedObjectStorage/APC.php b/Classes/PHPExcel/CachedObjectStorage/APC.php index 9bdeab43..d2ad6e37 100644 --- a/Classes/PHPExcel/CachedObjectStorage/APC.php +++ b/Classes/PHPExcel/CachedObjectStorage/APC.php @@ -1,6 +1,7 @@ _currentCellIsDirty && !empty($this->_currentObjectID)) { $this->_currentObject->detach(); - if (!apc_store($this->_cachePrefix.$this->_currentObjectID.'.cache',serialize($this->_currentObject),$this->_cacheTime)) { + if (!apc_store( + $this->_cachePrefix . $this->_currentObjectID . '.cache', + serialize($this->_currentObject), + $this->_cacheTime + )) { $this->__destruct(); - throw new PHPExcel_Exception('Failed to store cell '.$this->_currentObjectID.' in APC'); + throw new PHPExcel_Exception('Failed to store cell ' . $this->_currentObjectID . ' in APC'); } $this->_currentCellIsDirty = false; } $this->_currentObjectID = $this->_currentObject = null; - } // function _storeData() - + } /** * Add or Update a cell in cache identified by coordinate address @@ -83,7 +78,8 @@ class PHPExcel_CachedObjectStorage_APC extends PHPExcel_CachedObjectStorage_Cach * @return PHPExcel_Cell * @throws PHPExcel_Exception */ - public function addCacheData($pCoord, PHPExcel_Cell $cell) { + public function addCacheData($pCoord, PHPExcel_Cell $cell) + { if (($pCoord !== $this->_currentObjectID) && ($this->_currentObjectID !== null)) { $this->_storeData(); } @@ -94,8 +90,7 @@ class PHPExcel_CachedObjectStorage_APC extends PHPExcel_CachedObjectStorage_Cach $this->_currentCellIsDirty = true; return $cell; - } // function addCacheData() - + } /** * Is a value set in the current PHPExcel_CachedObjectStorage_ICache for an indexed cell? @@ -105,7 +100,8 @@ class PHPExcel_CachedObjectStorage_APC extends PHPExcel_CachedObjectStorage_Cach * @throws PHPExcel_Exception * @return boolean */ - public function isDataSet($pCoord) { + 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) { @@ -113,7 +109,7 @@ class PHPExcel_CachedObjectStorage_APC extends PHPExcel_CachedObjectStorage_Cach } // Check if the requested entry still exists in apc $success = apc_fetch($this->_cachePrefix.$pCoord.'.cache'); - if ($success === FALSE) { + if ($success === false) { // Entry no longer exists in APC, so clear it from the cache array parent::deleteCacheData($pCoord); throw new PHPExcel_Exception('Cell entry '.$pCoord.' no longer exists in APC cache'); @@ -121,8 +117,7 @@ class PHPExcel_CachedObjectStorage_APC extends PHPExcel_CachedObjectStorage_Cach return true; } return false; - } // function isDataSet() - + } /** * Get cell at a specific coordinate @@ -132,7 +127,8 @@ class PHPExcel_CachedObjectStorage_APC extends PHPExcel_CachedObjectStorage_Cach * @throws PHPExcel_Exception * @return PHPExcel_Cell Cell that was found, or null if not found */ - public function getCacheData($pCoord) { + public function getCacheData($pCoord) + { if ($pCoord === $this->_currentObjectID) { return $this->_currentObject; } @@ -140,8 +136,8 @@ class PHPExcel_CachedObjectStorage_APC extends PHPExcel_CachedObjectStorage_Cach // Check if the entry that has been requested actually exists if (parent::isDataSet($pCoord)) { - $obj = apc_fetch($this->_cachePrefix.$pCoord.'.cache'); - if ($obj === FALSE) { + $obj = apc_fetch($this->_cachePrefix . $pCoord . '.cache'); + if ($obj === false) { // Entry no longer exists in APC, so clear it from the cache array parent::deleteCacheData($pCoord); throw new PHPExcel_Exception('Cell entry '.$pCoord.' no longer exists in APC cache'); @@ -159,22 +155,21 @@ class PHPExcel_CachedObjectStorage_APC extends PHPExcel_CachedObjectStorage_Cach // Return requested entry return $this->_currentObject; - } // function getCacheData() + } + /** + * Get a list of all cell addresses currently held in cache + * + * @return string[] + */ + public function getCellList() + { + if ($this->_currentObjectID !== null) { + $this->_storeData(); + } - /** - * Get a list of all cell addresses currently held in cache - * - * @return string[] - */ - public function getCellList() { - if ($this->_currentObjectID !== null) { - $this->_storeData(); - } - - return parent::getCellList(); - } - + return parent::getCellList(); + } /** * Delete a cell in cache identified by coordinate address @@ -183,14 +178,14 @@ class PHPExcel_CachedObjectStorage_APC extends PHPExcel_CachedObjectStorage_Cach * @param string $pCoord Coordinate address of the cell to delete * @throws PHPExcel_Exception */ - public function deleteCacheData($pCoord) { + public function deleteCacheData($pCoord) + { // Delete the entry from APC apc_delete($this->_cachePrefix.$pCoord.'.cache'); // Delete the entry from our cell address array parent::deleteCacheData($pCoord); - } // function deleteCacheData() - + } /** * Clone the cell collection @@ -200,37 +195,38 @@ class PHPExcel_CachedObjectStorage_APC extends PHPExcel_CachedObjectStorage_Cach * @throws PHPExcel_Exception * @return void */ - public function copyCellCollection(PHPExcel_Worksheet $parent) { + public function copyCellCollection(PHPExcel_Worksheet $parent) + { parent::copyCellCollection($parent); // Get a new id for the new file name $baseUnique = $this->_getUniqueID(); - $newCachePrefix = substr(md5($baseUnique),0,8).'.'; + $newCachePrefix = substr(md5($baseUnique), 0, 8) . '.'; $cacheList = $this->getCellList(); - foreach($cacheList as $cellID) { + foreach ($cacheList as $cellID) { if ($cellID != $this->_currentObjectID) { - $obj = apc_fetch($this->_cachePrefix.$cellID.'.cache'); - if ($obj === FALSE) { + $obj = apc_fetch($this->_cachePrefix . $cellID . '.cache'); + if ($obj === false) { // Entry no longer exists in APC, so clear it from the cache array parent::deleteCacheData($cellID); - throw new PHPExcel_Exception('Cell entry '.$cellID.' no longer exists in APC'); + throw new PHPExcel_Exception('Cell entry ' . $cellID . ' no longer exists in APC'); } - if (!apc_store($newCachePrefix.$cellID.'.cache',$obj,$this->_cacheTime)) { + if (!apc_store($newCachePrefix . $cellID . '.cache', $obj, $this->_cacheTime)) { $this->__destruct(); - throw new PHPExcel_Exception('Failed to store cell '.$cellID.' in APC'); + throw new PHPExcel_Exception('Failed to store cell ' . $cellID . ' in APC'); } } } $this->_cachePrefix = $newCachePrefix; - } // function copyCellCollection() - + } /** * Clear the cell collection and disconnect from our parent * * @return void */ - public function unsetWorksheetCells() { - if ($this->_currentObject !== NULL) { + public function unsetWorksheetCells() + { + if ($this->_currentObject !== null) { $this->_currentObject->detach(); $this->_currentObject = $this->_currentObjectID = null; } @@ -242,8 +238,7 @@ class PHPExcel_CachedObjectStorage_APC extends PHPExcel_CachedObjectStorage_Cach // detach ourself from the worksheet, so that it can then delete this object successfully $this->_parent = null; - } // function unsetWorksheetCells() - + } /** * Initialise this new cell collection @@ -251,29 +246,29 @@ class PHPExcel_CachedObjectStorage_APC extends PHPExcel_CachedObjectStorage_Cach * @param PHPExcel_Worksheet $parent The worksheet for this cell collection * @param array of mixed $arguments Additional initialisation arguments */ - public function __construct(PHPExcel_Worksheet $parent, $arguments) { + public function __construct(PHPExcel_Worksheet $parent, $arguments) + { $cacheTime = (isset($arguments['cacheTime'])) ? $arguments['cacheTime'] : 600; - if ($this->_cachePrefix === NULL) { + if ($this->_cachePrefix === null) { $baseUnique = $this->_getUniqueID(); - $this->_cachePrefix = substr(md5($baseUnique),0,8).'.'; + $this->_cachePrefix = substr(md5($baseUnique), 0, 8) . '.'; $this->_cacheTime = $cacheTime; parent::__construct($parent); } - } // function __construct() - + } /** * Destroy this cell collection */ - public function __destruct() { + public function __destruct() + { $cacheList = $this->getCellList(); - foreach($cacheList as $cellID) { - apc_delete($this->_cachePrefix.$cellID.'.cache'); + foreach ($cacheList as $cellID) { + apc_delete($this->_cachePrefix . $cellID . '.cache'); } - } // function __destruct() - + } /** * Identify whether the caching method is currently available @@ -281,15 +276,15 @@ class PHPExcel_CachedObjectStorage_APC extends PHPExcel_CachedObjectStorage_Cach * * @return boolean */ - public static function cacheMethodIsAvailable() { + public static function cacheMethodIsAvailable() + { if (!function_exists('apc_store')) { - return FALSE; + return false; } - if (apc_sma_info() === FALSE) { - return FALSE; + if (apc_sma_info() === false) { + return false; } - return TRUE; + return true; } - } diff --git a/Classes/PHPExcel/CachedObjectStorage/CacheBase.php b/Classes/PHPExcel/CachedObjectStorage/CacheBase.php index 1d83600a..a7c25ed2 100644 --- a/Classes/PHPExcel/CachedObjectStorage/CacheBase.php +++ b/Classes/PHPExcel/CachedObjectStorage/CacheBase.php @@ -1,376 +1,368 @@ -_parent = $parent; - } // function __construct() - - - /** - * 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]); - } // function isDataSet() - - - /** - * 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; - } // function moveCell() - - - /** - * Add or Update a cell in cache - * - * @param PHPExcel_Cell $cell Cell to update - * @return PHPExcel_Cell - * @throws PHPExcel_Exception - */ - public function updateCacheData(PHPExcel_Cell $cell) { - return $this->addCacheData($cell->getCoordinate(),$cell); - } // function updateCacheData() - - - /** - * 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 && !is_null($this->_currentObject)) { - $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; - } // function deleteCacheData() - - - /** - * Get a list of all cell addresses currently held in cache - * - * @return string[] - */ - public function getCellList() { - return array_keys($this->_cellCache); - } // function getCellList() - - - /** - * Sort the list of all cell addresses currently held in cache by row and column - * - * @return string[] - */ - 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); - } // function sortCellList() - - - - /** - * 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 integer - */ - public function getCurrentRow() - { - sscanf($this->_currentObjectID, '%[A-Z]%d', $column, $row); - return (integer) $row; - } - - /** - * Get highest worksheet column - * - * @param string $row Return the highest column for the specified row, - * or the highest column of any row if no row number is passed - * @return string Highest column name - */ - public function getHighestColumn($row = null) - { - if ($row == null) { - $colRow = $this->getHighestRowAndColumn(); - return $colRow['column']; - } - - $columnList = array(1); - foreach ($this->getCellList() as $coord) { - sscanf($coord,'%[A-Z]%d', $c, $r); - if ($r != $row) { - continue; - } - $columnList[] = PHPExcel_Cell::columnIndexFromString($c); - } - return PHPExcel_Cell::stringFromColumnIndex(max($columnList) - 1); - } - - /** - * Get highest worksheet row - * - * @param string $column Return the highest row for the specified column, - * or the highest row of any column if no column letter is passed - * @return int Highest row number - */ - public function getHighestRow($column = null) - { - if ($column == null) { - $colRow = $this->getHighestRowAndColumn(); - return $colRow['row']; - } - - $rowList = array(0); - foreach ($this->getCellList() as $coord) { - sscanf($coord,'%[A-Z]%d', $c, $r); - if ($c != $column) { - continue; - } - $rowList[] = $r; - } - - return max($rowList); - } - - - /** - * 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(PHPExcel_Worksheet $parent) { - $this->_currentCellIsDirty; - $this->_storeData(); - - $this->_parent = $parent; - if (($this->_currentObject !== NULL) && (is_object($this->_currentObject))) { - $this->_currentObject->attach($this); - } - } // function copyCellCollection() - - /** - * Remove a row, deleting all cells in that row - * - * @param string $row Row number to remove - * @return void - */ - public function removeRow($row) { - foreach ($this->getCellList() as $coord) { - sscanf($coord,'%[A-Z]%d', $c, $r); - if ($r == $row) { - $this->deleteCacheData($coord); - } - } - } - - /** - * Remove a column, deleting all cells in that column - * - * @param string $column Column ID to remove - * @return void - */ - public function removeColumn($column) { - foreach ($this->getCellList() as $coord) { - sscanf($coord,'%[A-Z]%d', $c, $r); - if ($c == $column) { - $this->deleteCacheData($coord); - } - } - } - - /** - * 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 PHPExcel_Cell + * @throws PHPExcel_Exception + */ + public function updateCacheData(PHPExcel_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 && !is_null($this->_currentObject)) { + $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 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 string[] + */ + 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 integer + */ + public function getCurrentRow() + { + sscanf($this->_currentObjectID, '%[A-Z]%d', $column, $row); + return (integer) $row; + } + + /** + * Get highest worksheet column + * + * @param string $row Return the highest column for the specified row, + * or the highest column of any row if no row number is passed + * @return string Highest column name + */ + public function getHighestColumn($row = null) + { + if ($row == null) { + $colRow = $this->getHighestRowAndColumn(); + return $colRow['column']; + } + + $columnList = array(1); + foreach ($this->getCellList() as $coord) { + sscanf($coord, '%[A-Z]%d', $c, $r); + if ($r != $row) { + continue; + } + $columnList[] = PHPExcel_Cell::columnIndexFromString($c); + } + return PHPExcel_Cell::stringFromColumnIndex(max($columnList) - 1); + } + + /** + * Get highest worksheet row + * + * @param string $column Return the highest row for the specified column, + * or the highest row of any column if no column letter is passed + * @return int Highest row number + */ + public function getHighestRow($column = null) + { + if ($column == null) { + $colRow = $this->getHighestRowAndColumn(); + return $colRow['row']; + } + + $rowList = array(0); + foreach ($this->getCellList() as $coord) { + sscanf($coord, '%[A-Z]%d', $c, $r); + if ($c != $column) { + continue; + } + $rowList[] = $r; + } + + return max($rowList); + } + + /** + * 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(PHPExcel_Worksheet $parent) + { + $this->_currentCellIsDirty; + $this->_storeData(); + + $this->_parent = $parent; + if (($this->_currentObject !== null) && (is_object($this->_currentObject))) { + $this->_currentObject->attach($this); + } + } // function copyCellCollection() + + /** + * Remove a row, deleting all cells in that row + * + * @param string $row Row number to remove + * @return void + */ + public function removeRow($row) + { + foreach ($this->getCellList() as $coord) { + sscanf($coord, '%[A-Z]%d', $c, $r); + if ($r == $row) { + $this->deleteCacheData($coord); + } + } + } + + /** + * Remove a column, deleting all cells in that column + * + * @param string $column Column ID to remove + * @return void + */ + public function removeColumn($column) + { + foreach ($this->getCellList() as $coord) { + sscanf($coord, '%[A-Z]%d', $c, $r); + if ($c == $column) { + $this->deleteCacheData($coord); + } + } + } + + /** + * 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 05b57ed2..65afa3cf 100644 --- a/Classes/PHPExcel/CachedObjectStorage/DiscISAM.php +++ b/Classes/PHPExcel/CachedObjectStorage/DiscISAM.php @@ -1,219 +1,208 @@ -_currentCellIsDirty && !empty($this->_currentObjectID)) { - $this->_currentObject->detach(); - - fseek($this->_fileHandle,0,SEEK_END); - - $this->_cellCache[$this->_currentObjectID] = array( - 'ptr' => ftell($this->_fileHandle), - 'sz' => fwrite($this->_fileHandle, serialize($this->_currentObject)) - ); - $this->_currentCellIsDirty = false; - } - $this->_currentObjectID = $this->_currentObject = null; - } // function _storeData() - - - /** - * 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 PHPExcel_Cell - * @throws PHPExcel_Exception - */ - public function addCacheData($pCoord, PHPExcel_Cell $cell) { - if (($pCoord !== $this->_currentObjectID) && ($this->_currentObjectID !== null)) { - $this->_storeData(); - } - - $this->_currentObjectID = $pCoord; - $this->_currentObject = $cell; - $this->_currentCellIsDirty = true; - - return $cell; - } // function addCacheData() - - - /** - * 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; - } // function getCacheData() - - - /** - * Get a list of all cell addresses currently held in cache - * - * @return 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(PHPExcel_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+'); - } // function copyCellCollection() - - - /** - * 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(); - } // function unsetWorksheetCells() - - - /** - * 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(PHPExcel_Worksheet $parent, $arguments) { - $this->_cacheDirectory = ((isset($arguments['dir'])) && ($arguments['dir'] !== NULL)) - ? $arguments['dir'] - : PHPExcel_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+'); - } - } // function __construct() - - - /** - * Destroy this cell collection - */ - public function __destruct() { - if (!is_null($this->_fileHandle)) { - fclose($this->_fileHandle); - unlink($this->_fileName); - } - $this->_fileHandle = null; - } // function __destruct() - -} +_currentCellIsDirty && !empty($this->_currentObjectID)) { + $this->_currentObject->detach(); + + fseek($this->_fileHandle, 0, SEEK_END); + + $this->_cellCache[$this->_currentObjectID] = array( + 'ptr' => ftell($this->_fileHandle), + 'sz' => fwrite($this->_fileHandle, 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 PHPExcel_Cell + * @throws PHPExcel_Exception + */ + public function addCacheData($pCoord, PHPExcel_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 string[] + */ + public function getCellList() + { + if ($this->_currentObjectID !== null) { + $this->_storeData(); + } + + return parent::getCellList(); + } + + /** + * Clone the cell collection + * + * @param PHPExcel_Worksheet $parent The new worksheet + */ + public function copyCellCollection(PHPExcel_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 + * + */ + 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(PHPExcel_Worksheet $parent, $arguments) + { + $this->_cacheDirectory = ((isset($arguments['dir'])) && ($arguments['dir'] !== null)) + ? $arguments['dir'] + : PHPExcel_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/ICache.php b/Classes/PHPExcel/CachedObjectStorage/ICache.php index 97ea223a..aafef6e9 100644 --- a/Classes/PHPExcel/CachedObjectStorage/ICache.php +++ b/Classes/PHPExcel/CachedObjectStorage/ICache.php @@ -1,6 +1,7 @@ _currentCellIsDirty && !empty($this->_currentObjectID)) { - $this->_currentObject->detach(); - - $this->_cellCache[$this->_currentObjectID] = igbinary_serialize($this->_currentObject); - $this->_currentCellIsDirty = false; - } - $this->_currentObjectID = $this->_currentObject = null; - } // function _storeData() - - - /** - * 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 PHPExcel_Cell - * @throws PHPExcel_Exception - */ - public function addCacheData($pCoord, PHPExcel_Cell $cell) { - if (($pCoord !== $this->_currentObjectID) && ($this->_currentObjectID !== null)) { - $this->_storeData(); - } - - $this->_currentObjectID = $pCoord; - $this->_currentObject = $cell; - $this->_currentCellIsDirty = true; - - return $cell; - } // function addCacheData() - - - /** - * 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; - } // function getCacheData() - - - /** - * Get a list of all cell addresses currently held in cache - * - * @return 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; - } // function unsetWorksheetCells() - - - /** - * 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() { - if (!function_exists('igbinary_serialize')) { - return false; - } - - return true; - } - -} +_currentCellIsDirty && !empty($this->_currentObjectID)) { + $this->_currentObject->detach(); + + $this->_cellCache[$this->_currentObjectID] = igbinary_serialize($this->_currentObject); + $this->_currentCellIsDirty = false; + } + $this->_currentObjectID = $this->_currentObject = null; + } // function _storeData() + + + /** + * 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 PHPExcel_Cell + * @throws PHPExcel_Exception + */ + public function addCacheData($pCoord, PHPExcel_Cell $cell) + { + if (($pCoord !== $this->_currentObjectID) && ($this->_currentObjectID !== null)) { + $this->_storeData(); + } + + $this->_currentObjectID = $pCoord; + $this->_currentObject = $cell; + $this->_currentCellIsDirty = true; + + return $cell; + } // function addCacheData() + + + /** + * 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; + } // function getCacheData() + + + /** + * Get a list of all cell addresses currently held in cache + * + * @return 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; + } // function unsetWorksheetCells() + + + /** + * 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() + { + if (!function_exists('igbinary_serialize')) { + return false; + } + + return true; + } +} diff --git a/Classes/PHPExcel/CachedObjectStorage/Memcache.php b/Classes/PHPExcel/CachedObjectStorage/Memcache.php index 7e3659d7..1ffcf47a 100644 --- a/Classes/PHPExcel/CachedObjectStorage/Memcache.php +++ b/Classes/PHPExcel/CachedObjectStorage/Memcache.php @@ -1,312 +1,308 @@ -_currentCellIsDirty && !empty($this->_currentObjectID)) { - $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 PHPExcel_Exception('Failed to store cell '.$this->_currentObjectID.' in MemCache'); - } - } - $this->_currentCellIsDirty = false; - } - $this->_currentObjectID = $this->_currentObject = null; - } // function _storeData() - - - /** - * 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 PHPExcel_Cell - * @throws PHPExcel_Exception - */ - public function addCacheData($pCoord, PHPExcel_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; - } // function addCacheData() - - - /** - * 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 - * @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 PHPExcel_Exception('Cell entry '.$pCoord.' no longer exists in MemCache'); - } - return true; - } - return false; - } // function isDataSet() - - - /** - * 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 PHPExcel_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; - } // function getCacheData() - - - /** - * Get a list of all cell addresses currently held in cache - * - * @return 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); - } // function deleteCacheData() - - - /** - * Clone the cell collection - * - * @param PHPExcel_Worksheet $parent The new worksheet - * @return void - */ - public function copyCellCollection(PHPExcel_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 PHPExcel_Exception('Cell entry '.$cellID.' no longer exists in MemCache'); - } - if (!$this->_memcache->add($newCachePrefix.$cellID.'.cache',$obj,NULL,$this->_cacheTime)) { - $this->__destruct(); - throw new PHPExcel_Exception('Failed to store cell '.$cellID.' in MemCache'); - } - } - } - $this->_cachePrefix = $newCachePrefix; - } // function copyCellCollection() - - - /** - * 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; - } // function unsetWorksheetCells() - - - /** - * 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(PHPExcel_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 PHPExcel_Exception('Could not connect to MemCache server at '.$memcacheServer.':'.$memcachePort); - } - $this->_cacheTime = $cacheTime; - - parent::__construct($parent); - } - } // function __construct() - - - /** - * Memcache error handler - * - * @param string $host Memcache server - * @param integer $port Memcache port - * @throws PHPExcel_Exception - */ - public function failureCallback($host, $port) { - throw new PHPExcel_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'); - } - } // function __destruct() - - /** - * 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() { - if (!function_exists('memcache_add')) { - return false; - } - - return true; - } - -} +_currentCellIsDirty && !empty($this->_currentObjectID)) { + $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 PHPExcel_Exception("Failed to store cell {$this->_currentObjectID} in MemCache"); + } + } + $this->_currentCellIsDirty = false; + } + $this->_currentObjectID = $this->_currentObject = null; + } // function _storeData() + + + /** + * 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 PHPExcel_Cell + * @throws PHPExcel_Exception + */ + public function addCacheData($pCoord, PHPExcel_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; + } // function addCacheData() + + + /** + * 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 + * @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 PHPExcel_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 PHPExcel_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 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 + * @return void + */ + public function copyCellCollection(PHPExcel_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 PHPExcel_Exception("Cell entry {$cellID} no longer exists in MemCache"); + } + if (!$this->_memcache->add($newCachePrefix . $cellID . '.cache', $obj, null, $this->_cacheTime)) { + $this->__destruct(); + throw new PHPExcel_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(PHPExcel_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 PHPExcel_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 PHPExcel_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() + { + if (!function_exists('memcache_add')) { + return false; + } + + return true; + } +} diff --git a/Classes/PHPExcel/CachedObjectStorage/Memory.php b/Classes/PHPExcel/CachedObjectStorage/Memory.php index e1132147..aa3e1bb3 100644 --- a/Classes/PHPExcel/CachedObjectStorage/Memory.php +++ b/Classes/PHPExcel/CachedObjectStorage/Memory.php @@ -1,125 +1,118 @@ -_cellCache[$pCoord] = $cell; - - // Set current entry to the new/updated entry - $this->_currentObjectID = $pCoord; - - return $cell; - } // function addCacheData() - - - /** - * 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]; - } // function getCacheData() - - - /** - * Clone the cell collection - * - * @param PHPExcel_Worksheet $parent The new worksheet - * @return void - */ - public function copyCellCollection(PHPExcel_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; - } // function unsetWorksheetCells() - -} +_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 + */ + public function copyCellCollection(PHPExcel_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 + * + */ + 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 3c1b7564..16f01051 100644 --- a/Classes/PHPExcel/CachedObjectStorage/MemoryGZip.php +++ b/Classes/PHPExcel/CachedObjectStorage/MemoryGZip.php @@ -1,137 +1,133 @@ -_currentCellIsDirty && !empty($this->_currentObjectID)) { - $this->_currentObject->detach(); - - $this->_cellCache[$this->_currentObjectID] = gzdeflate(serialize($this->_currentObject)); - $this->_currentCellIsDirty = false; - } - $this->_currentObjectID = $this->_currentObject = null; - } // function _storeData() - - - /** - * 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 PHPExcel_Cell - * @throws PHPExcel_Exception - */ - public function addCacheData($pCoord, PHPExcel_Cell $cell) { - if (($pCoord !== $this->_currentObjectID) && ($this->_currentObjectID !== null)) { - $this->_storeData(); - } - - $this->_currentObjectID = $pCoord; - $this->_currentObject = $cell; - $this->_currentCellIsDirty = true; - - return $cell; - } // function addCacheData() - - - /** - * 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; - } // function getCacheData() - - - /** - * Get a list of all cell addresses currently held in cache - * - * @return 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; - } // function unsetWorksheetCells() - -} +_currentCellIsDirty && !empty($this->_currentObjectID)) { + $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 PHPExcel_Cell + * @throws PHPExcel_Exception + */ + public function addCacheData($pCoord, PHPExcel_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 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 aad8db87..285d0d8f 100644 --- a/Classes/PHPExcel/CachedObjectStorage/MemorySerialized.php +++ b/Classes/PHPExcel/CachedObjectStorage/MemorySerialized.php @@ -1,137 +1,129 @@ -_currentCellIsDirty && !empty($this->_currentObjectID)) { - $this->_currentObject->detach(); - - $this->_cellCache[$this->_currentObjectID] = serialize($this->_currentObject); - $this->_currentCellIsDirty = false; - } - $this->_currentObjectID = $this->_currentObject = null; - } // function _storeData() - - - /** - * 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 PHPExcel_Cell - * @throws PHPExcel_Exception - */ - public function addCacheData($pCoord, PHPExcel_Cell $cell) { - if (($pCoord !== $this->_currentObjectID) && ($this->_currentObjectID !== null)) { - $this->_storeData(); - } - - $this->_currentObjectID = $pCoord; - $this->_currentObject = $cell; - $this->_currentCellIsDirty = true; - - return $cell; - } // function addCacheData() - - - /** - * 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; - } // function getCacheData() - - - /** - * Get a list of all cell addresses currently held in cache - * - * @return 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; - } // function unsetWorksheetCells() - -} +_currentCellIsDirty && !empty($this->_currentObjectID)) { + $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 PHPExcel_Cell + * @throws PHPExcel_Exception + */ + public function addCacheData($pCoord, PHPExcel_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 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 04d30999..6176bf1e 100644 --- a/Classes/PHPExcel/CachedObjectStorage/PHPTemp.php +++ b/Classes/PHPExcel/CachedObjectStorage/PHPTemp.php @@ -1,206 +1,200 @@ -_currentCellIsDirty && !empty($this->_currentObjectID)) { - $this->_currentObject->detach(); - - fseek($this->_fileHandle,0,SEEK_END); - - $this->_cellCache[$this->_currentObjectID] = array( - 'ptr' => ftell($this->_fileHandle), - 'sz' => fwrite($this->_fileHandle, serialize($this->_currentObject)) - ); - $this->_currentCellIsDirty = false; - } - $this->_currentObjectID = $this->_currentObject = null; - } // function _storeData() - - - /** - * 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 PHPExcel_Cell - * @throws PHPExcel_Exception - */ - public function addCacheData($pCoord, PHPExcel_Cell $cell) { - if (($pCoord !== $this->_currentObjectID) && ($this->_currentObjectID !== null)) { - $this->_storeData(); - } - - $this->_currentObjectID = $pCoord; - $this->_currentObject = $cell; - $this->_currentCellIsDirty = true; - - return $cell; - } // function addCacheData() - - - /** - * 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; - } // function getCacheData() - - - /** - * Get a list of all cell addresses currently held in cache - * - * @return 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(PHPExcel_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; - } // function copyCellCollection() - - - /** - * 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(); - } // function unsetWorksheetCells() - - - /** - * 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(PHPExcel_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+'); - } - } // function __construct() - - - /** - * Destroy this cell collection - */ - public function __destruct() { - if (!is_null($this->_fileHandle)) { - fclose($this->_fileHandle); - } - $this->_fileHandle = null; - } // function __destruct() - -} +_currentCellIsDirty && !empty($this->_currentObjectID)) { + $this->_currentObject->detach(); + + fseek($this->_fileHandle, 0, SEEK_END); + + $this->_cellCache[$this->_currentObjectID] = array( + 'ptr' => ftell($this->_fileHandle), + 'sz' => fwrite($this->_fileHandle, 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 PHPExcel_Cell + * @throws PHPExcel_Exception + */ + public function addCacheData($pCoord, PHPExcel_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 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(PHPExcel_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(PHPExcel_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 92179a28..0a63cd2d 100644 --- a/Classes/PHPExcel/CachedObjectStorage/SQLite.php +++ b/Classes/PHPExcel/CachedObjectStorage/SQLite.php @@ -1,306 +1,307 @@ -_currentCellIsDirty && !empty($this->_currentObjectID)) { - $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 PHPExcel_Exception(sqlite_error_string($this->_DBHandle->lastError())); - $this->_currentCellIsDirty = false; - } - $this->_currentObjectID = $this->_currentObject = null; - } // function _storeData() - - - /** - * 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 PHPExcel_Cell - * @throws PHPExcel_Exception - */ - public function addCacheData($pCoord, PHPExcel_Cell $cell) { - if (($pCoord !== $this->_currentObjectID) && ($this->_currentObjectID !== null)) { - $this->_storeData(); - } - - $this->_currentObjectID = $pCoord; - $this->_currentObject = $cell; - $this->_currentCellIsDirty = true; - - return $cell; - } // function addCacheData() - - - /** - * 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 PHPExcel_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; - } // function getCacheData() - - - /** - * 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 PHPExcel_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; - } // function isDataSet() - - - /** - * 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 PHPExcel_Exception(sqlite_error_string($this->_DBHandle->lastError())); - - $this->_currentCellIsDirty = false; - } // function deleteCacheData() - - - /** - * 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 PHPExcel_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 PHPExcel_Exception($this->_DBHandle->lastErrorMsg()); - - return TRUE; - } // function moveCell() - - - /** - * Get a list of all cell addresses currently held in cache - * - * @return 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 PHPExcel_Exception(sqlite_error_string($this->_DBHandle->lastError())); - - $cellKeys = array(); - foreach($cellIdsResult as $row) { - $cellKeys[] = $row['id']; - } - - return $cellKeys; - } // function getCellList() - - - /** - * Clone the cell collection - * - * @param PHPExcel_Worksheet $parent The new worksheet - * @return void - */ - public function copyCellCollection(PHPExcel_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 PHPExcel_Exception(sqlite_error_string($this->_DBHandle->lastError())); - - // Copy the existing cell cache file - $this->_TableName = $tableName; - } // function copyCellCollection() - - - /** - * 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(); - } // function unsetWorksheetCells() - - - /** - * Initialise this new cell collection - * - * @param PHPExcel_Worksheet $parent The worksheet for this cell collection - */ - public function __construct(PHPExcel_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 PHPExcel_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 PHPExcel_Exception(sqlite_error_string($this->_DBHandle->lastError())); - } - } // function __construct() - - - /** - * Destroy this cell collection - */ - public function __destruct() { - if (!is_null($this->_DBHandle)) { - $this->_DBHandle->queryExec('DROP TABLE kvp_'.$this->_TableName); - } - $this->_DBHandle = null; - } // function __destruct() - - - /** - * 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() { - if (!function_exists('sqlite_open')) { - return false; - } - - return true; - } - -} +_currentCellIsDirty && !empty($this->_currentObjectID)) { + $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 PHPExcel_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 PHPExcel_Cell + * @throws PHPExcel_Exception + */ + public function addCacheData($pCoord, PHPExcel_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 PHPExcel_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 PHPExcel_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 PHPExcel_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 PHPExcel_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 PHPExcel_Exception($this->_DBHandle->lastErrorMsg()); + } + + return true; + } + + /** + * Get a list of all cell addresses currently held in cache + * + * @return 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 PHPExcel_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(PHPExcel_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 PHPExcel_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(PHPExcel_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 PHPExcel_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 PHPExcel_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() + { + if (!function_exists('sqlite_open')) { + return false; + } + + return true; + } +} diff --git a/Classes/PHPExcel/CachedObjectStorage/SQLite3.php b/Classes/PHPExcel/CachedObjectStorage/SQLite3.php index c54adb4e..4d013eff 100644 --- a/Classes/PHPExcel/CachedObjectStorage/SQLite3.php +++ b/Classes/PHPExcel/CachedObjectStorage/SQLite3.php @@ -1,345 +1,346 @@ -_currentCellIsDirty && !empty($this->_currentObjectID)) { - $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 PHPExcel_Exception($this->_DBHandle->lastErrorMsg()); - $this->_currentCellIsDirty = false; - } - $this->_currentObjectID = $this->_currentObject = null; - } // function _storeData() - - - /** - * 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 PHPExcel_Cell - * @throws PHPExcel_Exception - */ - public function addCacheData($pCoord, PHPExcel_Cell $cell) { - if (($pCoord !== $this->_currentObjectID) && ($this->_currentObjectID !== null)) { - $this->_storeData(); - } - - $this->_currentObjectID = $pCoord; - $this->_currentObject = $cell; - $this->_currentCellIsDirty = true; - - return $cell; - } // function addCacheData() - - - /** - * 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 PHPExcel_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; - } // function getCacheData() - - - /** - * 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 PHPExcel_Exception($this->_DBHandle->lastErrorMsg()); - } - $cellData = $cellResult->fetchArray(SQLITE3_ASSOC); - - return ($cellData === FALSE) ? FALSE : TRUE; - } // function isDataSet() - - - /** - * 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 PHPExcel_Exception($this->_DBHandle->lastErrorMsg()); - - $this->_currentCellIsDirty = FALSE; - } // function deleteCacheData() - - - /** - * 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 PHPExcel_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 PHPExcel_Exception($this->_DBHandle->lastErrorMsg()); - - return TRUE; - } // function moveCell() - - - /** - * Get a list of all cell addresses currently held in cache - * - * @return 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 PHPExcel_Exception($this->_DBHandle->lastErrorMsg()); - - $cellKeys = array(); - while ($row = $cellIdsResult->fetchArray(SQLITE3_ASSOC)) { - $cellKeys[] = $row['id']; - } - - return $cellKeys; - } // function getCellList() - - - /** - * Clone the cell collection - * - * @param PHPExcel_Worksheet $parent The new worksheet - * @return void - */ - public function copyCellCollection(PHPExcel_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 PHPExcel_Exception($this->_DBHandle->lastErrorMsg()); - - // Copy the existing cell cache file - $this->_TableName = $tableName; - } // function copyCellCollection() - - - /** - * 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(); - } // function unsetWorksheetCells() - - - /** - * Initialise this new cell collection - * - * @param PHPExcel_Worksheet $parent The worksheet for this cell collection - */ - public function __construct(PHPExcel_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 PHPExcel_Exception($this->_DBHandle->lastErrorMsg()); - if (!$this->_DBHandle->exec('CREATE TABLE kvp_'.$this->_TableName.' (id VARCHAR(12) PRIMARY KEY, value BLOB)')) - throw new PHPExcel_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"); - } // function __construct() - - - /** - * 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; - } // function __destruct() - - - /** - * 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() { - if (!class_exists('SQLite3',FALSE)) { - return false; - } - - return true; - } - -} +_currentCellIsDirty && !empty($this->_currentObjectID)) { + $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 PHPExcel_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 PHPExcel_Cell + * @throws PHPExcel_Exception + */ + public function addCacheData($pCoord, PHPExcel_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 PHPExcel_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 PHPExcel_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 PHPExcel_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 PHPExcel_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 PHPExcel_Exception($this->_DBHandle->lastErrorMsg()); + } + + return true; + } + + /** + * Get a list of all cell addresses currently held in cache + * + * @return 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 PHPExcel_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(PHPExcel_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 PHPExcel_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(PHPExcel_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 PHPExcel_Exception($this->_DBHandle->lastErrorMsg()); + } + if (!$this->_DBHandle->exec('CREATE TABLE kvp_'.$this->_TableName.' (id VARCHAR(12) PRIMARY KEY, value BLOB)')) { + throw new PHPExcel_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() + { + if (!class_exists('SQLite3', false)) { + return false; + } + + return true; + } +} diff --git a/Classes/PHPExcel/CachedObjectStorage/Wincache.php b/Classes/PHPExcel/CachedObjectStorage/Wincache.php index 6eeeef60..5b1fc43d 100644 --- a/Classes/PHPExcel/CachedObjectStorage/Wincache.php +++ b/Classes/PHPExcel/CachedObjectStorage/Wincache.php @@ -1,294 +1,289 @@ -_currentCellIsDirty && !empty($this->_currentObjectID)) { - $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 PHPExcel_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 PHPExcel_Exception('Failed to store cell '.$this->_currentObjectID.' in WinCache'); - } - } - $this->_currentCellIsDirty = false; - } - - $this->_currentObjectID = $this->_currentObject = null; - } // function _storeData() - - - /** - * 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 PHPExcel_Cell - * @throws PHPExcel_Exception - */ - public function addCacheData($pCoord, PHPExcel_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; - } // function addCacheData() - - - /** - * 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 PHPExcel_Exception('Cell entry '.$pCoord.' no longer exists in WinCache'); - } - return true; - } - return false; - } // function isDataSet() - - - /** - * 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 PHPExcel_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; - } // function getCacheData() - - - /** - * Get a list of all cell addresses currently held in cache - * - * @return 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); - } // function deleteCacheData() - - - /** - * Clone the cell collection - * - * @param PHPExcel_Worksheet $parent The new worksheet - * @return void - */ - public function copyCellCollection(PHPExcel_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 PHPExcel_Exception('Cell entry '.$cellID.' no longer exists in Wincache'); - } - if (!wincache_ucache_add($newCachePrefix.$cellID.'.cache', $obj, $this->_cacheTime)) { - $this->__destruct(); - throw new PHPExcel_Exception('Failed to store cell '.$cellID.' in Wincache'); - } - } - } - $this->_cachePrefix = $newCachePrefix; - } // function copyCellCollection() - - - /** - * 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; - } // function unsetWorksheetCells() - - - /** - * 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(PHPExcel_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); - } - } // function __construct() - - - /** - * Destroy this cell collection - */ - public function __destruct() { - $cacheList = $this->getCellList(); - foreach($cacheList as $cellID) { - wincache_ucache_delete($this->_cachePrefix.$cellID.'.cache'); - } - } // function __destruct() - - - /** - * 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() { - if (!function_exists('wincache_ucache_add')) { - return false; - } - - return true; - } - -} +_currentCellIsDirty && !empty($this->_currentObjectID)) { + $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 PHPExcel_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 PHPExcel_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 PHPExcel_Cell + * @throws PHPExcel_Exception + */ + public function addCacheData($pCoord, PHPExcel_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 PHPExcel_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 PHPExcel_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 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(PHPExcel_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 PHPExcel_Exception('Cell entry '.$cellID.' no longer exists in Wincache'); + } + if (!wincache_ucache_add($newCachePrefix.$cellID.'.cache', $obj, $this->_cacheTime)) { + $this->__destruct(); + throw new PHPExcel_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(PHPExcel_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() + { + if (!function_exists('wincache_ucache_add')) { + return false; + } + + return true; + } +} diff --git a/Classes/PHPExcel/CachedObjectStorageFactory.php b/Classes/PHPExcel/CachedObjectStorageFactory.php index 81baa2fd..251c3ef8 100644 --- a/Classes/PHPExcel/CachedObjectStorageFactory.php +++ b/Classes/PHPExcel/CachedObjectStorageFactory.php @@ -1,7 +1,7 @@ array( 'memoryCacheSize' => '1MB' ), - self::cache_to_discISAM => array( 'dir' => NULL + self::cache_to_discISAM => array( 'dir' => null ), self::cache_to_apc => array( 'cacheTime' => 600 ), @@ -116,7 +104,6 @@ class PHPExcel_CachedObjectStorageFactory ), ); - /** * Arguments for the active cache storage method * @@ -124,28 +111,25 @@ class PHPExcel_CachedObjectStorageFactory */ private static $_storageMethodParameters = array(); - /** * Return the current cache storage method * - * @return string|NULL + * @return string|null **/ public static function getCacheStorageMethod() { return self::$_cacheStorageMethod; - } // function getCacheStorageMethod() - + } /** * Return the current cache storage class * - * @return PHPExcel_CachedObjectStorage_ICache|NULL + * @return PHPExcel_CachedObjectStorage_ICache|null **/ public static function getCacheStorageClass() { return self::$_cacheStorageClass; - } // function getCacheStorageClass() - + } /** * Return the list of all possible cache storage methods @@ -155,8 +139,7 @@ class PHPExcel_CachedObjectStorageFactory public static function getAllCacheStorageMethods() { return self::$_storageMethods; - } // function getCacheStorageMethods() - + } /** * Return the list of all available cache storage methods @@ -166,15 +149,14 @@ class PHPExcel_CachedObjectStorageFactory public static function getCacheStorageMethods() { $activeMethods = array(); - foreach(self::$_storageMethods as $storageMethod) { + foreach (self::$_storageMethods as $storageMethod) { $cacheStorageClass = 'PHPExcel_CachedObjectStorage_' . $storageMethod; if (call_user_func(array($cacheStorageClass, 'cacheMethodIsAvailable'))) { $activeMethods[] = $storageMethod; } } return $activeMethods; - } // function getCacheStorageMethods() - + } /** * Identify the cache storage method to use @@ -186,30 +168,29 @@ class PHPExcel_CachedObjectStorageFactory **/ public static function initialize($method = self::cache_in_memory, $arguments = array()) { - if (!in_array($method,self::$_storageMethods)) { - return FALSE; + if (!in_array($method, self::$_storageMethods)) { + return false; } $cacheStorageClass = 'PHPExcel_CachedObjectStorage_'.$method; if (!call_user_func(array( $cacheStorageClass, 'cacheMethodIsAvailable'))) { - return FALSE; + return false; } self::$_storageMethodParameters[$method] = self::$_storageMethodDefaultParameters[$method]; - foreach($arguments as $k => $v) { + foreach ($arguments as $k => $v) { if (array_key_exists($k, self::$_storageMethodParameters[$method])) { self::$_storageMethodParameters[$method][$k] = $v; } } - if (self::$_cacheStorageMethod === NULL) { + if (self::$_cacheStorageMethod === null) { self::$_cacheStorageClass = 'PHPExcel_CachedObjectStorage_' . $method; self::$_cacheStorageMethod = $method; } - return TRUE; - } // function initialize() - + return true; + } /** * Initialise the cache storage @@ -219,33 +200,32 @@ class PHPExcel_CachedObjectStorageFactory **/ public static function getInstance(PHPExcel_Worksheet $parent) { - $cacheMethodIsAvailable = TRUE; - if (self::$_cacheStorageMethod === NULL) { + $cacheMethodIsAvailable = true; + if (self::$_cacheStorageMethod === null) { $cacheMethodIsAvailable = self::initialize(); } if ($cacheMethodIsAvailable) { - $instance = new self::$_cacheStorageClass( $parent, - self::$_storageMethodParameters[self::$_cacheStorageMethod] - ); - if ($instance !== NULL) { + $instance = new self::$_cacheStorageClass( + $parent, + self::$_storageMethodParameters[self::$_cacheStorageMethod] + ); + if ($instance !== null) { return $instance; } } - return FALSE; - } // function getInstance() - + return false; + } /** * Clear the cache storage * **/ - public static function finalize() - { - self::$_cacheStorageMethod = NULL; - self::$_cacheStorageClass = NULL; - self::$_storageMethodParameters = array(); - } - + public static function finalize() + { + self::$_cacheStorageMethod = null; + self::$_cacheStorageClass = null; + self::$_storageMethodParameters = array(); + } } diff --git a/Classes/PHPExcel/CalcEngine/CyclicReferenceStack.php b/Classes/PHPExcel/CalcEngine/CyclicReferenceStack.php index 7cf3f24d..b3bb2bf1 100644 --- a/Classes/PHPExcel/CalcEngine/CyclicReferenceStack.php +++ b/Classes/PHPExcel/CalcEngine/CyclicReferenceStack.php @@ -1,6 +1,7 @@ _stack); + } -/** - * PHPExcel_CalcEngine_CyclicReferenceStack - * - * @category PHPExcel_CalcEngine_CyclicReferenceStack - * @package PHPExcel_Calculation - * @copyright Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel) - */ -class PHPExcel_CalcEngine_CyclicReferenceStack { + /** + * Push a new entry onto the stack + * + * @param mixed $value + */ + public function push($value) + { + $this->_stack[$value] = $value; + } - /** - * The call stack for calculated cells - * - * @var mixed[] - */ - private $_stack = array(); + /** + * Pop the last entry from the stack + * + * @return mixed + */ + public function pop() + { + return array_pop($this->_stack); + } + /** + * Test to see if a specified entry exists on the stack + * + * @param mixed $value The value to test + */ + public function onStack($value) + { + return isset($this->_stack[$value]); + } - /** - * Return the number of entries on the stack - * - * @return integer - */ - public function count() { - return count($this->_stack); - } - - /** - * Push a new entry onto the stack - * - * @param mixed $value - */ - public function push($value) { - $this->_stack[$value] = $value; - } - - /** - * Pop the last entry from the stack - * - * @return mixed - */ - public function pop() { - return array_pop($this->_stack); - } - - /** - * Test to see if a specified entry exists on the stack - * - * @param mixed $value The value to test - */ - public function onStack($value) { - return isset($this->_stack[$value]); - } - - /** - * Clear the stack - */ - public function clear() { - $this->_stack = array(); - } - - /** - * Return an array of all entries on the stack - * - * @return mixed[] - */ - public function showStack() { - return $this->_stack; - } + /** + * Clear the stack + */ + public function clear() + { + $this->_stack = array(); + } + /** + * Return an array of all entries on the stack + * + * @return mixed[] + */ + public function showStack() + { + return $this->_stack; + } } diff --git a/Classes/PHPExcel/CalcEngine/Logger.php b/Classes/PHPExcel/CalcEngine/Logger.php index 3ff9746e..9fd1d7d7 100644 --- a/Classes/PHPExcel/CalcEngine/Logger.php +++ b/Classes/PHPExcel/CalcEngine/Logger.php @@ -1,6 +1,7 @@ _cellStack = $stack; + } - /** - * The calculation engine cell reference stack - * - * @var PHPExcel_CalcEngine_CyclicReferenceStack - */ - private $_cellStack; + /** + * Enable/Disable Calculation engine logging + * + * @param boolean $pValue + */ + public function setWriteDebugLog($pValue = false) + { + $this->_writeDebugLog = $pValue; + } - - /** - * Instantiate a Calculation engine logger - * - * @param PHPExcel_CalcEngine_CyclicReferenceStack $stack - */ - public function __construct(PHPExcel_CalcEngine_CyclicReferenceStack $stack) { - $this->_cellStack = $stack; - } + /** + * Return whether calculation engine logging is enabled or disabled + * + * @return boolean + */ + public function getWriteDebugLog() + { + return $this->_writeDebugLog; + } - /** - * Enable/Disable Calculation engine logging - * - * @param boolean $pValue - */ - public function setWriteDebugLog($pValue = FALSE) { - $this->_writeDebugLog = $pValue; - } + /** + * Enable/Disable echoing of debug log information + * + * @param boolean $pValue + */ + public function setEchoDebugLog($pValue = false) + { + $this->_echoDebugLog = $pValue; + } - /** - * Return whether calculation engine logging is enabled or disabled - * - * @return boolean - */ - public function getWriteDebugLog() { - return $this->_writeDebugLog; - } + /** + * Return whether echoing of debug log information is enabled or disabled + * + * @return boolean + */ + public function getEchoDebugLog() + { + return $this->_echoDebugLog; + } - /** - * Enable/Disable echoing of debug log information - * - * @param boolean $pValue - */ - public function setEchoDebugLog($pValue = FALSE) { - $this->_echoDebugLog = $pValue; - } + /** + * Write an entry to the calculation engine debug log + */ + public function writeDebugLog() + { + // Only write the debug log if logging is enabled + if ($this->_writeDebugLog) { + $message = implode(func_get_args()); + $cellReference = implode(' -> ', $this->_cellStack->showStack()); + if ($this->_echoDebugLog) { + echo $cellReference, + ($this->_cellStack->count() > 0 ? ' => ' : ''), + $message, + PHP_EOL; + } + $this->_debugLog[] = $cellReference . + ($this->_cellStack->count() > 0 ? ' => ' : '') . + $message; + } + } - /** - * Return whether echoing of debug log information is enabled or disabled - * - * @return boolean - */ - public function getEchoDebugLog() { - return $this->_echoDebugLog; - } - - /** - * Write an entry to the calculation engine debug log - */ - public function writeDebugLog() { - // Only write the debug log if logging is enabled - if ($this->_writeDebugLog) { - $message = implode(func_get_args()); - $cellReference = implode(' -> ', $this->_cellStack->showStack()); - if ($this->_echoDebugLog) { - echo $cellReference, - ($this->_cellStack->count() > 0 ? ' => ' : ''), - $message, - PHP_EOL; - } - $this->_debugLog[] = $cellReference . - ($this->_cellStack->count() > 0 ? ' => ' : '') . - $message; - } - } // function _writeDebug() - - /** - * Clear the calculation engine debug log - */ - public function clearLog() { - $this->_debugLog = array(); - } // function flushLogger() - - /** - * Return the calculation engine debug log - * - * @return string[] - */ - public function getLog() { - return $this->_debugLog; - } // function flushLogger() - -} // class PHPExcel_CalcEngine_Logger + /** + * Clear the calculation engine debug log + */ + public function clearLog() + { + $this->_debugLog = array(); + } + /** + * Return the calculation engine debug log + * + * @return string[] + */ + public function getLog() + { + return $this->_debugLog; + } +} diff --git a/Classes/PHPExcel/Calculation/Token/Stack.php b/Classes/PHPExcel/Calculation/Token/Stack.php index b91794e4..2b27bb8e 100644 --- a/Classes/PHPExcel/Calculation/Token/Stack.php +++ b/Classes/PHPExcel/Calculation/Token/Stack.php @@ -1,6 +1,6 @@ _count; + } - /** - * The parser stack for formulae - * - * @var mixed[] - */ - private $_stack = array(); + /** + * Push a new entry onto the stack + * + * @param mixed $type + * @param mixed $value + * @param mixed $reference + */ + public function push($type, $value, $reference = null) + { + $this->_stack[$this->_count++] = array( + 'type' => $type, + 'value' => $value, + 'reference' => $reference + ); + if ($type == 'Function') { + $localeFunction = PHPExcel_Calculation::_localeFunc($value); + if ($localeFunction != $value) { + $this->_stack[($this->_count - 1)]['localeValue'] = $localeFunction; + } + } + } - /** - * Count of entries in the parser stack - * - * @var integer - */ - private $_count = 0; + /** + * Pop the last entry from the stack + * + * @return mixed + */ + public function pop() + { + if ($this->_count > 0) { + return $this->_stack[--$this->_count]; + } + return null; + } + /** + * Return an entry from the stack without removing it + * + * @param integer $n number indicating how far back in the stack we want to look + * @return mixed + */ + public function last($n = 1) + { + if ($this->_count - $n < 0) { + return null; + } + return $this->_stack[$this->_count - $n]; + } - /** - * Return the number of entries on the stack - * - * @return integer - */ - public function count() { - return $this->_count; - } // function count() - - /** - * Push a new entry onto the stack - * - * @param mixed $type - * @param mixed $value - * @param mixed $reference - */ - public function push($type, $value, $reference = NULL) { - $this->_stack[$this->_count++] = array('type' => $type, - 'value' => $value, - 'reference' => $reference - ); - if ($type == 'Function') { - $localeFunction = PHPExcel_Calculation::_localeFunc($value); - if ($localeFunction != $value) { - $this->_stack[($this->_count - 1)]['localeValue'] = $localeFunction; - } - } - } // function push() - - /** - * Pop the last entry from the stack - * - * @return mixed - */ - public function pop() { - if ($this->_count > 0) { - return $this->_stack[--$this->_count]; - } - return NULL; - } // function pop() - - /** - * Return an entry from the stack without removing it - * - * @param integer $n number indicating how far back in the stack we want to look - * @return mixed - */ - public function last($n = 1) { - if ($this->_count - $n < 0) { - return NULL; - } - return $this->_stack[$this->_count - $n]; - } // function last() - - /** - * Clear the stack - */ - function clear() { - $this->_stack = array(); - $this->_count = 0; - } - -} // class PHPExcel_Calculation_Token_Stack + /** + * Clear the stack + */ + function clear() + { + $this->_stack = array(); + $this->_count = 0; + } +}