]> git.mxchange.org Git - mailer.git/blobdiff - inc/classes/cachesystem.class.php
Introduced some wrapper functions, the extension for cache files is now .cache.php...
[mailer.git] / inc / classes / cachesystem.class.php
index 729197a03dd48d8d7d6e542c6d67010acc53e6b3..a5ea7261d17157cb62921364226888ab37f7c4fe 100644 (file)
@@ -16,7 +16,7 @@
  * $Author::                                                          $ *
  * -------------------------------------------------------------------- *
  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
- * Copyright (c) 2009 - 2012 by Mailer Developer Team                   *
+ * Copyright (c) 2009 - 2013 by Mailer Developer Team                   *
  * For more information visit: http://mxchange.org                      *
  *                                                                      *
  * This program is free software; you can redistribute it and/or modify *
@@ -49,7 +49,7 @@ class CacheSystem {
        var $fqfn = '';
 
        // Resource to cache file
-       var $pointer = false;
+       var $pointer = FALSE;
 
        // Data array from cache
        var $data = array();
@@ -62,13 +62,16 @@ class CacheSystem {
        var $rebuilt = array();
 
        // File extension
-       var $extension = '.cache';
+       var $extension = '';
        var $status = array();
        var $readable = array();
        var $fullPath = '';
 
        // Constructor
        function CacheSystem () {
+               // Set extension
+               $this->extension = getCacheExtension();
+
                // Construct full path
                $this->fullPath = getPath() . getCachePath();
 
@@ -138,7 +141,7 @@ class CacheSystem {
                // Is the pointer a valid resource?
                if (is_resource($this->pointer)) {
                        // Write the line
-                       fwrite($this->pointer, $line . chr(10));
+                       fwrite($this->pointer, $line . PHP_EOL);
                } else {
                        // Something bad happened
                        reportBug(__METHOD__, __LINE__, 'Pointer type is ' . gettype($this->pointer) . ', expected is resource.');
@@ -152,6 +155,7 @@ class CacheSystem {
                unset($this->status[$this->name]);
        }
 
+       // Adds a data row (array) to cache file and global cache array
        function addRow ($data) {
                // Is the pointe rvalid?
                if (is_resource($this->pointer)) {
@@ -172,9 +176,9 @@ class CacheSystem {
                                        $GLOBALS['cache_array']['config'][$data['config']][$k] = $v;
                                } elseif ($this->name == 'filter') {
                                        // Filter
-                                       $GLOBALS['cache_array']['filter']['chains'][$data['filter_name']][$data['filter_function']] = $data['filter_active'];
+                                       $GLOBALS['cache_array']['filter']['chains'][$data['filter_name']][$data['filter_function']]  = $data['filter_active'];
                                        $GLOBALS['cache_array']['filter']['counter'][$data['filter_name']][$data['filter_function']] = $data['filter_counter'];
-                                       $GLOBALS['cache_array']['filter']['loaded'][$data['filter_name']][$data['filter_function']] = true;
+                                       $GLOBALS['cache_array']['filter']['loaded'][$data['filter_name']][$data['filter_function']]  = TRUE;
                                } elseif ($this->name == 'modules') {
                                        // Modules
                                        $GLOBALS['cache_array']['modules'][$k][$data['module']] = $v;
@@ -213,6 +217,9 @@ class CacheSystem {
                                } elseif ($this->name == 'earning') {
                                        // Table 'earning'
                                        $GLOBALS['cache_array']['earning'][$k][$data['earning_id']] = $v;
+                               } elseif ($this->name == 'payments') {
+                                       // Table 'payments'
+                                       $GLOBALS['cache_array']['payments'][$k][$data['id']] = $v;
                                } elseif (is_array($v)) {
                                        // Serialize and BASE64-encode the array
                                        $v = base64_encode(serialize($v));
@@ -221,10 +228,10 @@ class CacheSystem {
                                        $this->finalize();
 
                                        // Remove cache
-                                       $this->removeCacheFile(true);
+                                       $this->removeCacheFile(TRUE);
 
                                        // Unsupported/unhandled cache detected
-                                       reportBug(__METHOD__, __LINE__, 'Unsupported cache ' . $this->name . ' detected.');
+                                       reportBug(__METHOD__, __LINE__, 'Unsupported cache ' . $this->name . ' detected, data=' . print_r($data, TRUE) . ',k=' . $k . ',v=' . $v);
                                }
 
                                // Write cache line to file
@@ -236,6 +243,7 @@ class CacheSystem {
                }
        }
 
+       // Closes cache file with closing PHP tag
        function finalize () {
                // Quit function when no pointer is set
                if (is_resource($this->pointer)) {
@@ -256,11 +264,12 @@ class CacheSystem {
 
                        // Remove pointer and status
                        unset($this->status[$this->name]);
-                       $this->pointer = false;
+                       $this->pointer = FALSE;
                        //* DEBUG: */ logDebugMessage(__METHOD__, __LINE__, 'name=' . $this->name . ' - FINALIZED!');
                } // END - if
        }
 
+       // Loads cache file and returns an array of the cached data
        function getArrayFromCache () {
                // Is the cache already loaded?
                if (isset($this->data[$this->name])) {
@@ -307,17 +316,17 @@ class CacheSystem {
        }
 
        // Destroy an existing cache file
-       function removeCacheFile ($force = false) {
+       function removeCacheFile ($force = FALSE) {
                // Reset read status
                $this->resetCacheReadStatus();
 
                // Debug message
-               //* DEBUG: */ logDebugMessage(__METHOD__, __LINE__, sprintf("%s should be removed.", $this->name));
+               //* DEBUG: */ logDebugMessage(__METHOD__, __LINE__, sprintf('%s should be removed.', $this->name));
 
                // Is the cache file not yet rebuilt?
                if ((!isset($this->rebuilt[$this->name])) && ($this->isCacheReadable())) {
                        // Only run in regular output mode
-                       if ((!isHtmlOutputMode()) && ($force === false)) {
+                       if ((!isHtmlOutputMode()) && ($force === FALSE)) {
                                // Debug message if allowed
                                if (isDebugModeEnabled()) {
                                        // Debug message
@@ -349,7 +358,7 @@ class CacheSystem {
                                // The cache does no longer exist so kill the content
                                unset($this->data[$this->name]);
                                unset($this->version[$this->name]);
-                               $this->rebuilt[$this->name] = true;
+                               $this->rebuilt[$this->name] = TRUE;
                        } else {
                                // Not removed!
                                reportBug(__METHOD__, __LINE__, '{%message,CACHE_CANNOT_UNLINK=' . $this->name . '%}');
@@ -421,7 +430,7 @@ class CacheSystem {
                        if ((is_array($dummy)) && (isset($dummy[$search])) && (is_array($dummy[$search]))) {
                                // Search for key in array
                                $key_found = array_key_exists($search_key, $dummy[$search]);
-                               if ($key_found == true) {
+                               if ($key_found == TRUE) {
                                        $key = $search_key;
                                        // Key (hopefully) found?
                                        foreach ($dummy as $a => $v) {
@@ -461,7 +470,7 @@ class CacheSystem {
                                $this->version[$this->name][$ext_name] = $ext_ver;
 
                                // Write cache line to file
-                               $this->writeLine($this->rewriteEntry($ext_name, $ext_ver, 'version', true));
+                               $this->writeLine($this->rewriteEntry($ext_name, $ext_ver, 'version', TRUE));
                        } // END - if
                        //* DEBUG: */ logDebugMessage(__METHOD__, __LINE__, 'name=' . $this->name . ',ext_name=' . $ext_name . ',ext_ver=' . $ext_ver);
                } else {
@@ -475,7 +484,7 @@ class CacheSystem {
                // Check cache
                if (!isset($GLOBALS[__METHOD__][$ext_name])) {
                        // Does never match by default
-                       $GLOBALS[__METHOD__][$ext_name] = false;
+                       $GLOBALS[__METHOD__][$ext_name] = FALSE;
 
                        // Compare only if installed
                        if (isExtensionInstalled($ext_name)) {
@@ -485,17 +494,17 @@ class CacheSystem {
                                // Debug messages
                                if (isset($this->version[$this->name][$ext_name])) {
                                        // Does it match?
-                                       $GLOBALS[__METHOD__][$ext_name] = ((isset($this->version[$this->name][$ext_name])) && ($this->version[$this->name][$ext_name] == $ext_ver));
+                                       $GLOBALS[__METHOD__][$ext_name] = ($this->version[$this->name][$ext_name] == $ext_ver);
                                } elseif ($this->isCacheReadable()) {
                                        // No cache version found
-                                       logDebugMessage(__METHOD__, __LINE__, 'Cache ' . $this->name . ' has missing version entry for extension ' . $ext_name . '! Purging cache...');
+                                       logDebugMessage(__METHOD__, __LINE__, 'Cache ' . $this->name . ' has missing version entry for extension ext-' . $ext_name . '! Purging cache...');
        
                                        // Remove the cache file
-                                       $this->removeCacheFile(true);
+                                       $this->removeCacheFile(TRUE);
                                }
                        } else {
                                // Not installed, does always match
-                               $GLOBALS[__METHOD__][$ext_name] = true;
+                               $GLOBALS[__METHOD__][$ext_name] = TRUE;
                        }
                } else {
                        // Cache entry found, log debug message
@@ -508,12 +517,12 @@ class CacheSystem {
 
        // Rewrit the entry so it can be stored in cache file
        // @TODO Add support for more types which break in last else-block
-       function rewriteEntry ($key, $value, $prefix = 'data', $single = false) {
+       function rewriteEntry ($key, $value, $prefix = 'data', $single = FALSE) {
                // Default is not single entry
                $extender = '[]';
 
                // Add only for single array entry?
-               if ($single === true) {
+               if ($single === TRUE) {
                        $extender = '';
                } // END - if
 
@@ -529,12 +538,12 @@ class CacheSystem {
                        $line = '$this->' . $prefix . "['" . $this->name . "']['" . $key . "']" . $extender . ' = NULL;';
                } elseif (is_bool($value)) {
                        // Boolean value
-                       if ($value === true) {
+                       if ($value === TRUE) {
                                // True
-                               $line = '$this->' . $prefix . "['" . $this->name . "']['" . $key . "']" . $extender . ' = true;';
+                               $line = '$this->' . $prefix . "['" . $this->name . "']['" . $key . "']" . $extender . ' = TRUE;';
                        } else {
                                // False
-                               $line = '$this->' . $prefix . "['" . $this->name . "']['" . $key . "']" . $extender . ' = false;';
+                               $line = '$this->' . $prefix . "['" . $this->name . "']['" . $key . "']" . $extender . ' = FALSE;';
                        }
                } elseif (isset($value[0])) {
                        // These lines needs fixing