X-Git-Url: https://git.mxchange.org/?p=core.git;a=blobdiff_plain;f=framework%2Fmain%2Fclasses%2Fdatabase%2Fbackend%2Flfdb_legacy%2Fclass_CachedLocalFileDatabase.php;h=d232db1b460bd42f320d97fd3e70427ddf1edd65;hp=e8a8555a03220a65b9db947efb7cd99938f86146;hb=b9bfbe86c031c9d83c3670602906df191a33ba2a;hpb=5da8f717122568335b8a8ab230fa0de17e983fab diff --git a/framework/main/classes/database/backend/lfdb_legacy/class_CachedLocalFileDatabase.php b/framework/main/classes/database/backend/lfdb_legacy/class_CachedLocalFileDatabase.php index e8a8555a..d232db1b 100644 --- a/framework/main/classes/database/backend/lfdb_legacy/class_CachedLocalFileDatabase.php +++ b/framework/main/classes/database/backend/lfdb_legacy/class_CachedLocalFileDatabase.php @@ -12,6 +12,9 @@ use CoreFramework\Factory\ObjectFactory; use CoreFramework\Filesystem\FileNotFoundException; use CoreFramework\Generic\FrameworkException; +// Import SPL stuff +use \SplFileInfo; + /** * Database backend class for storing objects in locally created files. * @@ -114,12 +117,12 @@ class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBac /** * 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; } /** @@ -172,20 +175,17 @@ class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBac /** * 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); @@ -193,7 +193,7 @@ class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBac $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 @@ -203,13 +203,13 @@ class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBac /** * 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 @@ -219,7 +219,7 @@ class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBac //* 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.'); @@ -236,11 +236,11 @@ class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBac $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 } @@ -250,18 +250,18 @@ class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBac } /** - * 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; } /** @@ -272,7 +272,7 @@ class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBac */ 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( @@ -282,7 +282,7 @@ class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBac ); // Write the data to the file - $this->writeDataArrayToFqfn($fqfn, $this->tableInfo[$dataSetInstance->getTableName()]); + $this->writeDataArrayToFqfn($infoInstance, $this->tableInfo[$dataSetInstance->getTableName()]); } /** @@ -296,14 +296,14 @@ class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBac $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]); } /** @@ -475,10 +475,10 @@ class CachedLocalFileDatabase extends BaseDatabaseBackend implements DatabaseBac // 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);