3 * Database backend class for storing objects in locally created files.
5 * This class serializes objects and saves them to local files.
7 * @author Roland Haeder <roland __NOSPAM__ [at] __REMOVE_ME__ mxchange [dot] org>
10 class LocalFileDatabase extends BaseDatabaseFrontend implements DatabaseFrontendInterface {
12 * Save path for "file database"
14 private $savePath = "";
17 * The file's extension
19 private $fileExtension = "serialized";
22 * The IO handler for file handling which should be FileIOHandler.
24 private $ioInstance = null;
27 * The last read file's name
29 private $lastFile = "";
32 * The last read file's content including header information
34 private $lastContents = array();
37 * The private constructor. Do never instance from outside!
38 * You need to set a local file path. The class will then validate it.
42 private function __construct() {
43 // Call parent constructor
44 parent::constructor(__CLASS__);
47 $this->setPartDescr("Dateidatenbankschicht");
50 $this->createUniqueID();
53 $this->removeSystemArray();
57 * Create an object of LocalFileDatabase and set the save path for local files.
58 * This method also validates the given file path.
60 * @param $savePath The local file path string
61 * @param $ioInstance The input/output handler. This
62 * should be FileIOHandler
63 * @return $dbInstance An instance of LocalFileDatabase
64 * @throws SavePathIsEmptyException If the given save path is an
66 * @throws SavePathIsNoDirectoryException If the save path is no
68 * @throws SavePathReadProtectedException If the save path is read-
70 * @throws SavePathWriteProtectedException If the save path is write-
73 public final static function createLocalFileDatabase ($savePath, FileIOHandler $ioInstance) {
75 $dbInstance = new LocalFileDatabase();
77 if (empty($savePath)) {
79 throw new SavePathIsEmptyException($dbInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
80 } elseif (!is_dir($savePath)) {
82 throw new SavePathIsNoDirectoryException($savePath, self::EXCEPTION_INVALID_PATH_NAME);
83 } elseif (!is_readable($savePath)) {
85 throw new SavePathReadProtectedException($savePath, self::EXCEPTION_READ_PROTECED_PATH);
86 } elseif (!is_writeable($savePath)) {
88 throw new SavePathWriteProtectedException($savePath, self::EXCEPTION_WRITE_PROTECED_PATH);
92 if (defined('DEBUG_DATABASE')) $dbInstance->getDebugInstance()->output(sprintf("[%s:] Es werden lokale Dateien zum Speichern von Objekten verwendet.<br />\n",
93 $dbInstance->__toString()
96 // Set save path and IO instance
97 $dbInstance->setSavePath($savePath);
98 $dbInstance->setIOInstance($ioInstance);
100 // Return database instance
105 * Setter for save path
107 * @param $savePath The local save path where we shall put our serialized classes
110 public final function setSavePath ($savePath) {
112 $savePath = (string) $savePath;
115 if ((defined('DEBUG_DATABASE')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:] Lokaler Speicherpfad <strong>%s</strong> wird verwendet.<br />\n",
121 $this->savePath = $savePath;
125 * Getter for save path
127 * @return $savePath The local save path where we shall put our serialized classes
129 public final function getSavePath () {
130 return $this->savePath;
134 * Getter for file extension
136 * @return $fileExtension The file extension for all file names
138 public final function getFileExtension () {
139 return $this->fileExtension;
143 * Saves a given object to the local file system by serializing and
144 * transparently compressing it
146 * @param $object The object we shall save to the local file system
148 * @throws NullPointerException If the object instance is null
149 * @throws NoObjectException If the parameter $object is not
152 public final function saveObject ($object) {
153 // Some tests on the parameter...
154 if (is_null($object)) {
155 // Is null, throw exception
156 throw new NullPointerException($object, self::EXCEPTION_IS_NULL_POINTER);
157 } elseif (!is_object($object)) {
158 // Is not an object, throw exception
159 throw new NoObjectException($object, self::EXCEPTION_IS_NO_OBJECT);
160 } elseif (!method_exists($object, '__toString')) {
161 // A highly required method was not found... :-(
162 throw new MissingMethodException(array($object, '__toString'), self::EXCEPTION_MISSING_METHOD);
166 if ((defined('DEBUG_DATABASE')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:] Das Objekt <strong>%s</strong> soll in eine lokale Datei gespeichert werden.<br />\n",
168 $object->__toString()
171 // Get a string containing the serialized object. We cannot exchange
172 // $this and $object here because $object does not need to worry
173 // about it's limitations... ;-)
174 $serialized = $this->serializeObject($object);
177 if ((defined('DEBUG_DATABASE')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:] Das Objekt <strong>%s</strong> ist nach der Serialisierung <strong>%s</strong> Byte gross.<br />\n",
179 $object->__toString(),
183 // Get a path name plus file name and append the extension
184 $fqfn = $this->getSavePath() . $object->getPathFileNameFromObject() . "." . $this->getFileExtension();
186 // Save the file to disc we don't care here if the path is there,
187 // this must be done in later methods.
188 $this->getIOInstance()->saveFile($fqfn, array($this->getCompressorChannel()->getCompressorExtension(), $serialized));
192 * Get a serialized string from the given object
194 * @param $object The object we want to serialize and transparently
196 * @return $serialized A string containing the serialzed/compressed object
197 * @see ObjectLimits An object holding limition information
198 * @see SerializationContainer A special container class for e.g.
199 * attributes from limited objects
201 private function serializeObject ($object) {
202 // If there is no limiter instance we serialize the whole object
203 // otherwise only in the limiter object (ObjectLimits) specified
204 // attributes summarized in a special container class
205 if ($this->getLimitInstance() === null) {
206 // Serialize the whole object. This tribble call is the reason
207 // why we need a fall-back implementation in CompressorChannel
208 // of the methods compressStream() and decompressStream().
209 $serialized = $this->getCompressorChannel()->getCompressor()->compressStream(serialize($object));
211 // Serialize only given attributes in a special container
212 $container = SerializationContainer::createSerializationContainer($this->getLimitInstance(), $object);
214 // Serialize the container
215 $serialized = $this->getCompressorChannel()->getCompressor()->compressStream(serialize($container));
218 // Return the serialized object string
223 * Analyses if a unique ID has already been used or not by search in the
224 * local database folder.
226 * @param $uniqueID A unique ID number which shall be checked
227 * before it will be used
228 * @param $inConstructor If we got called in a de/con-structor or
229 * from somewhere else
230 * @return $isUnused true = The unique ID was not found in the database,
231 * false = It is already in use by an other object
232 * @throws NoArrayCreatedException If explode() fails to create an array
233 * @throws InvalidArrayCountException If the array contains less or
234 * more than two elements
236 public function isUniqueIdUsed ($uniqueID, $inConstructor = false) {
237 // Currently not used... ;-)
240 // Split the unique ID up in path and file name
241 $pathFile = explode("@", $uniqueID);
243 // Are there two elements? Index 0 is the path, 1 the file name + global extension
244 if (!is_array($pathFile)) {
246 if ($inConstructor) {
249 throw new NoArrayCreatedException(array($this, "pathFile"), self::EXCEPTION_ARRAY_EXPECTED);
251 } elseif (count($pathFile) != 2) {
252 // Invalid ID returned!
253 if ($inConstructor) {
256 throw new InvalidArrayCountException(array($this, "pathFile", count($pathFile), 2), self::EXCEPTION_ARRAY_HAS_INVALID_COUNT);
260 // Create full path name
261 $pathName = $this->getSavePath() . $pathFile[0];
263 // Check if the file is there with a file handler
264 if ($inConstructor) {
265 // No exceptions in constructors and destructors!
266 $dirInstance = FrameworkDirectoryPointer::createFrameworkDirectoryPointer($pathName, true);
268 // Has an object being created?
269 if (!is_object($dirInstance)) return false;
271 // Outside a constructor
273 $dirInstance = FrameworkDirectoryPointer::createFrameworkDirectoryPointer($pathName);
274 } catch (PathIsNoDirectoryException $e) {
275 // Okay, path not found
280 // Initialize the search loop
282 while ($dataFile = $dirInstance->readDirectoryExcept(array(".", ".."))) {
283 // Generate FQFN for testing
284 $fqfn = sprintf("%s/%s", $pathName, $dataFile);
285 $this->setLastFile($fqfn);
287 // Get instance for file handler
288 $inputHandler = $this->getIOInstance();
290 // Try to read from it. This makes it sure that the file is
291 // readable and a valid database file
292 $this->setLastFileContents($inputHandler->loadFileContents($fqfn));
294 // Extract filename (= unique ID) from it
295 $ID = substr(basename($fqfn), 0, -(strlen($this->getFileExtension()) + 1));
297 // Is this the required unique ID?
298 if ($ID == $pathFile[1]) {
299 // Okay, already in use!
304 // Close the directory handler
305 $dirInstance->closeDirectory();
307 // Now the same for the file...
312 * Getter for the file IO instance
314 *Â @return $ioInstance An instance for IO operations
315 * @see FileIOHandler The concrete handler for IO operations
317 public final function getIOInstance () {
318 return $this->ioInstance;
322 * Setter for the file IO instance
324 * @param $ioInstance An instance for IO operations (should be
328 public final function setIOInstance (FileIOHandler $ioInstance) {
329 $this->ioInstance = $ioInstance;
333 * Setter for the last read file
335 * @param $fqfn The FQFN of the last read file
338 private function setLastFile ($fqfn) {
340 $fqfn = (string) $fqfn;
341 $this->lastFile = $fqfn;
345 * Getter for last read file
347 * @return $lastFile The last read file's name with full path
349 public final function getLastFile () {
350 return $this->lastFile;
354 * Setter for contents of the last read file
356 * @param $contents An array with header and data elements
359 private function setLastFileContents ($contents) {
361 $contents = (array) $contents;
362 $this->lastContents = $contents;
366 * Getter for last read file's content as an array
368 * @return $lastContent The array with elements 'header' and 'data'.
370 public final function getLastContents () {
371 return $this->lastContents;
375 * Get cached (last fetched) data from the local file database
377 * @param $uniqueID The ID number for looking up the data
378 * @return $object The restored object from the maybe compressed
380 * @throws MismatchingCompressorsException If the compressor from
382 * mismatches with the
384 * @throws NullPointerException If the restored object
386 * @throws NoObjectException If the restored "object"
387 * is not an object instance
388 * @throws MissingMethodException If the required method
389 * toString() is missing
391 public function getObjectFromCachedData ($uniqueID) {
392 // Get instance for file handler
393 $inputHandler = $this->getIOInstance();
395 // Get last file's name and contents
396 $fqfn = $this->repairFQFN($this->getLastFile(), $uniqueID);
397 $contents = $this->repairContents($this->getLastContents(), $fqfn);
399 // Let's decompress it. First we need the instance
400 $compressInstance = $this->getCompressorChannel();
402 // Is the compressor's extension the same as the one from the data?
403 if ($compressInstance->getCompressorExtension() != $contents['header'][0]) {
405 * @todo For now we abort here but later we need to make this a little more dynamic.
407 throw new MismatchingCompressorsException(array($this, $contents['header'][0], $fqfn, $compressInstance->getCompressorExtension()), self::EXCEPTION_MISMATCHING_COMPRESSORS);
410 // Decompress the data now
411 $serialized = $compressInstance->getCompressor()->decompressStream($contents['data']);
413 // And unserialize it...
414 $object = unserialize($serialized);
416 // This must become a valid object, so let's check it...
417 if (is_null($object)) {
418 // Is null, throw exception
419 throw new NullPointerException($object, self::EXCEPTION_IS_NULL_POINTER);
420 } elseif (!is_object($object)) {
421 // Is not an object, throw exception
422 throw new NoObjectException($object, self::EXCEPTION_IS_NO_OBJECT);
423 } elseif (!method_exists($object, '__toString')) {
424 // A highly required method was not found... :-(
425 throw new MissingMethodException(array($object, '__toString'), self::EXCEPTION_MISSING_METHOD);
428 // And return the object
433 * Private method for re-gathering (repairing) the FQFN
435 * @param $fqfn The current FQFN we shall validate
436 * @param $uniqueID The unique ID number
437 * @return $fqfn The repaired FQFN when it is empty
438 * @throws NoArrayCreatedException If explode() has not
440 * @throws InvalidArrayCountException If the array count is not
443 private function repairFQFN ($fqfn, $uniqueID) {
445 $fqfn = (string) $fqfn;
446 $uniqueID = (string) $uniqueID;
448 // Is there pre-cached data available?
450 // Split the unique ID up in path and file name
451 $pathFile = explode("@", $uniqueID);
453 // Are there two elements? Index 0 is the path, 1 the file name + global extension
454 if (!is_array($pathFile)) {
456 throw new NoArrayCreatedException(array($this, "pathFile"), self::EXCEPTION_ARRAY_EXPECTED);
457 } elseif (count($pathFile) != 2) {
458 // Invalid ID returned!
459 throw new InvalidArrayCountException(array($this, "pathFile", count($pathFile), 2), self::EXCEPTION_ARRAY_HAS_INVALID_COUNT);
462 // Create full path name
463 $pathName = $this->getSavePath() . $pathFile[0];
465 // Nothing cached, so let's create a FQFN first
466 $fqfn = sprintf("%s/%s.%s", $pathName, $pathFile[1], $this->getFileExtension());
467 $this->setLastFile($fqfn);
470 // Return repaired FQFN
475 * Private method for re-gathering the contents of a given file
477 * @param $contents The (maybe) already cached contents as an array
478 * @param $fqfn The current FQFN we shall validate
479 * @return $contents The repaired contents from the given file
481 private function repairContents ($contents, $fqfn) {
482 // Is there some content and header (2 indexes) in?
483 if ((!is_array($contents)) || (count($contents) != 2) || (!isset($contents['header'])) || (!isset($contents['data']))) {
484 // No content found so load the file again
485 $contents = $inputHandler->loadFileContents($fqfn);
487 // And remember all data for later usage
488 $this->setLastContents($contents);
491 // Return the repaired contents
495 /* DUMMY */ public final function loadObject () {}