use CoreFramework\Filesystem\FileNotFoundException;
use CoreFramework\Generic\FrameworkException;
+// Import SPL stuff
+use \SplFileInfo;
+
/**
* Database backend class for storing objects in locally created files.
*
/**
* Setter for the last read file
*
- * @param $fqfn The FQFN of the last read file
+ * @param $infoInstance The FQFN of the last read file
* @return void
*/
- private final function setLastFile ($fqfn) {
+ private final function setLastFile (SplFileInfo $infoInstance) {
// Cast string and set it
- $this->lastFile = (string) $fqfn;
+ $this->lastFile = $infoInstance;
}
/**
/**
* Reads a local data file and returns it's contents in an array
*
- * @param $fqfn The FQFN for the requested file
+ * @param $infoInstance An instance of a SplFileInfo class
* @return $dataArray
*/
- private function getDataArrayFromFile ($fqfn) {
+ private function getDataArrayFromFile (SplFileInfo $infoInstance) {
// Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: Reading elements from database file ' . $fqfn . ' ...');
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: Reading elements from database file ' . $infoInstance . ' ...');
// Init compressed data
- $compressedData = $this->getFileIoInstance()->loadFileContents($fqfn);
+ $compressedData = $this->getFileIoInstance()->loadFileContents($infoInstance);
$compressedData = $compressedData['data'];
- // Close the file and throw the instance away
- unset($fileInstance);
-
// Decompress it
$serializedData = $this->getCompressorChannel()->getCompressor()->decompressStream($compressedData);
$dataArray = json_decode($serializedData, true);
// Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: Read ' . count($dataArray) . ' elements from database file ' . $fqfn . '.');
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: Read ' . count($dataArray) . ' elements from database file ' . $infoInstance . '.');
//* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: dataArray=' . print_r($dataArray, true));
// Finally return it
/**
* Writes data array to local file
*
- * @param $fqfn The FQFN of the local file
+ * @param $infoInstance An instance of a SplFileInfo class
* @param $dataArray An array with all the data we shall write
* @return void
*/
- private function writeDataArrayToFqfn ($fqfn, array $dataArray) {
+ private function writeDataArrayToFqfn (SplFileInfo $infoInstance, array $dataArray) {
// Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: Flushing ' . count($dataArray) . ' elements to database file ' . $fqfn . ' ...');
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: Flushing ' . count($dataArray) . ' elements to database file ' . $infoInstance . ' ...');
//* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: dataArray=' . print_r($dataArray, true));
// Serialize and compress it
//* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: Writing ' . strlen($compressedData) . ' bytes ...');
// Write this data BASE64 encoded to the file
- $this->getFileIoInstance()->saveStreamToFile($fqfn, $compressedData, $this);
+ $this->getFileIoInstance()->saveStreamToFile($infoInstance, $compressedData, $this);
// Debug message
//* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DATABASE: Flushing ' . count($dataArray) . ' elements to database file completed.');
$infoArray = array();
// Create FQFN for getting the table information file
- $fqfn = $this->generateFqfnFromDataSet($dataSetInstance, 'info');
+ $infoInstance = $this->generateFileFromDataSet($dataSetInstance, 'info');
// Get the file contents
try {
- $infoArray = $this->getDataArrayFromFile($fqfn);
+ $infoArray = $this->getDataArrayFromFile($infoInstance);
} catch (FileNotFoundException $e) {
// Not found, so ignore it here
}
}
/**
- * Generates an FQFN from given dataset instance and string
+ * Generates a file info class from given dataset instance and string
*
* @param $dataSetInstance An instance of a database set class
* @param $rowName Name of the row
- * @return $fqfn The FQFN for this row
+ * @return $infoInstance An instance of a SplFileInfo class
*/
- private function generateFqfnFromDataSet (Criteria $dataSetInstance, $rowName) {
- // This is the FQFN
- $fqfn = $this->getConfigInstance()->getConfigEntry('local_database_path') . $dataSetInstance->getTableName() . DIRECTORY_SEPARATOR . $rowName . '.' . $this->getFileExtension();
+ private function generateFileFromDataSet (Criteria $dataSetInstance, $rowName) {
+ // Instanciate new file object
+ $infoInstance = new SplFileInfo($this->getConfigInstance()->getConfigEntry('local_database_path') . $dataSetInstance->getTableName() . DIRECTORY_SEPARATOR . $rowName . '.' . $this->getFileExtension());
// Return it
- return $fqfn;
+ return $infoInstance;
}
/**
*/
private function createTableInfoFile (StoreableCriteria $dataSetInstance) {
// Create FQFN for creating the table information file
- $fqfn = $this->generateFqfnFromDataSet($dataSetInstance, 'info');
+ $infoInstance = $this->generateFileFromDataSet($dataSetInstance, 'info');
// Get the data out from dataset in a local array
$this->tableInfo[$dataSetInstance->getTableName()] = array(
);
// Write the data to the file
- $this->writeDataArrayToFqfn($fqfn, $this->tableInfo[$dataSetInstance->getTableName()]);
+ $this->writeDataArrayToFqfn($infoInstance, $this->tableInfo[$dataSetInstance->getTableName()]);
}
/**
$tableName = $dataSetInstance->getTableName();
// Create FQFN for creating the table information file
- $fqfn = $this->generateFqfnFromDataSet($dataSetInstance, 'info');
+ $infoInstance = $this->generateFileFromDataSet($dataSetInstance, 'info');
// Get the data out from dataset in a local array
$this->tableInfo[$tableName]['primary'] = $dataSetInstance->getPrimaryKey();
$this->tableInfo[$tableName]['last_updated'] = time();
// Write the data to the file
- $this->writeDataArrayToFqfn($fqfn, $this->tableInfo[$tableName]);
+ $this->writeDataArrayToFqfn($infoInstance, $this->tableInfo[$tableName]);
}
/**
// Try to save the request away
try {
// Create full path name
- $fqfn = $this->generateFqfnFromDataSet($dataSetInstance, md5($dataSetInstance->getUniqueValue()));
+ $infoInstance = $this->generateFileFromDataSet($dataSetInstance, md5($dataSetInstance->getUniqueValue()));
// Write the data away
- $this->writeDataArrayToFqfn($fqfn, $dataSetInstance->getCriteriaArray());
+ $this->writeDataArrayToFqfn($infoInstance, $dataSetInstance->getCriteriaArray());
// Update the primary key
$this->updatePrimaryKey($dataSetInstance);