/**
  * Read a Marker File Template from FileSystem or from Persistent (Memory) Cache if exists
  * !!! This is intended for very special usage ... !!! This is used automatically by the render_file_template() and used in combination with render_mixed_template() may produce the same results ... it make non-sense using it with render_template() as this should be used for internal (php) templates as all external templates should be loaded with render_file_template()
  *
  * @access 		private
  * @internal
  *
  * @param 	STRING 		$y_file_path 					:: The relative path to the file markers template
  *
  * @return 	STRING										:: The template string
  *
  */
 public static function read_template_file($y_file_path)
 {
     //--
     $use_pcache = false;
     if ((string) SMART_FRAMEWORK_DEBUG_MODE != 'yes') {
         if (defined('SMART_SOFTWARE_MKTPL_PCACHETIME')) {
             if (is_int(SMART_SOFTWARE_MKTPL_PCACHETIME)) {
                 if ((int) SMART_SOFTWARE_MKTPL_PCACHETIME >= 0 and (int) SMART_SOFTWARE_MKTPL_PCACHETIME <= 31622400) {
                     // 0 unlimited ; 1 sec .. 366 days
                     $use_pcache = true;
                 }
                 //end if
             }
             //end if
         }
         //end if
     }
     //end if
     if ($use_pcache === true and SmartPersistentCache::isActive() and SmartPersistentCache::isMemoryBased()) {
         $the_cache_key = 'tpl__' . Smart::safe_pathname((string) Smart::base_name((string) $y_file_path)) . '__' . sha1((string) $y_file_path);
     } else {
         $the_cache_key = '';
     }
     //end if else
     //--
     $tpl = '';
     //--
     if ((string) $the_cache_key != '') {
         if (SmartPersistentCache::keyExists('smart-markers-templating', (string) $the_cache_key)) {
             $tpl = (string) SmartPersistentCache::getKey('smart-markers-templating', (string) $the_cache_key);
             if ((string) $tpl != '') {
                 //Smart::log_notice('TPL found in cache: '.$y_file_path);
                 return (string) $tpl;
                 // return from persistent cache
             }
             //end if
         }
         //end if
     }
     //end if
     //--
     $tpl = (string) SmartFileSystem::staticread((string) $y_file_path);
     if ((string) $the_cache_key != '') {
         if ((string) $tpl != '') {
             SmartPersistentCache::setKey('smart-markers-templating', (string) $the_cache_key . '__path', (string) $y_file_path, (int) SMART_SOFTWARE_MKTPL_PCACHETIME);
             // set to persistent cache
             SmartPersistentCache::setKey('smart-markers-templating', (string) $the_cache_key, (string) $tpl, (int) SMART_SOFTWARE_MKTPL_PCACHETIME);
             // set to persistent cache
         }
         //end if
     }
     //end if
     //--
     return (string) $tpl;
     // return from fs read
     //--
 }
 /**
  * Set a Page into the (Persistent) Cache.
  *
  * @param 	STRING 		$storage_namespace		:: the cache storage namespace, used to group keys
  * @param 	STRING 		$unique_key				:: the unique cache key
  * @param 	MIXED 		$content 				:: the cache content as a STRING or an ARRAY with Page Value(s) / Page Setting(s)
  * @param 	INTEGER 	$expiration 			:: The page cache expiration in seconds ; 0 will not expire
  *
  * @return 	BOOL								:: TRUE if the PersistentCache is active and value was set ; FALSE in the rest of the cases
  */
 public final function PageSetInCache($storage_namespace, $unique_key, $content, $expiration)
 {
     //--
     if (empty($content)) {
         return false;
     }
     //end if
     //--
     if (!SmartPersistentCache::isActive()) {
         return false;
     }
     //end if
     //--
     $cache = SmartUtils::cache_variable_archive($content);
     // mixed (number / string / array)
     if ((string) $cache == '') {
         return false;
     }
     //end if
     //--
     return SmartPersistentCache::setKey((string) $storage_namespace, (string) $unique_key, (string) $cache, (int) $expiration);
     //--
 }
    public static function test_redisserver()
    {
        global $configs;
        //--
        if (SMART_FRAMEWORK_TESTUNIT_ALLOW_REDIS_TESTS !== true) {
            return SmartComponents::operation_notice('Test Unit for Redis Server is DISABLED ...');
        }
        //end if
        //--
        //--
        if (SmartPersistentCache::isActive()) {
            //--
            $redis_big_content = self::pack_test_archive();
            // CREATE THE Test Archive (time not counted)
            //--
            $redis_test_key = 'redis-test-key_' . Smart::uuid_10_num() . '-' . Smart::uuid_36() . '-' . Smart::uuid_45();
            $redis_test_value = array('unicode-test' => '"Unicode78źź:ăĂîÎâÂșȘțȚşŞţŢグッド', 'big-key-test' => (string) $redis_big_content, 'random-key' => Smart::uuid_10_str() . '.' . Smart::random_number(1000, 9999));
            $redis_test_checkum = sha1(implode("\n", (array) $redis_test_value));
            $redis_test_arch_content = SmartUtils::cache_variable_archive($redis_test_value);
            $redis_test_arch_checksum = sha1($redis_test_arch_content);
            //--
            $tests = array();
            $tests[] = '##### Redis / TESTS (Persistent Cache) with a Variable Key-Size of ' . SmartUtils::pretty_print_bytes(strlen($redis_test_arch_content), 2) . ' : #####';
            //--
            $err = '';
            //--
            if ((string) $err == '') {
                $tests[] = 'Building a Test Archive file for Redis Tests (time not counted)';
                // archive was previous created, only test here
                if ((string) $redis_big_content == '') {
                    $err = 'Failed to build the Test Archive file for the Redis Test (see the error log for more details) ...';
                }
                //end if
            }
            //end if
            //--
            $time = microtime(true);
            $tests[] = '++ START Counter ...';
            //--
            if ((string) $err == '') {
                $tests[] = 'Building the Cache Archive';
                if ((string) $redis_test_arch_content == '') {
                    $err = 'Failed to build the Cache Variable(s) Archive file for the Redis Test (see the error log for more details) ...';
                }
                //end if
            }
            //end if
            //--
            if ((string) $err == '') {
                $tests[] = 'Set a short Redis Key (auto-expire in 3 seconds)';
                $redis_set_key = SmartPersistentCache::setKey('redis-server-tests', $redis_test_key, (string) $redis_test_value['unicode-test'], 3);
                if ($redis_set_key !== true) {
                    $err = 'Redis SetKey (short) returned a non-true result: ' . "\n" . $redis_test_key;
                }
                //end if
                if ((string) $err == '') {
                    $tests[] = 'Wait 5 seconds for Redis Key to expire, then check again if exists (time not counted)';
                    sleep(5);
                    // wait the Redis Key to Expire
                    $time = (double) $time + 5;
                    // ignore those 5 seconds (waiting time) to fix counter
                    $tests[] = '-- FIX Counter (substract the 5 seconds, waiting time) ...';
                    if (SmartPersistentCache::keyExists('redis-server-tests', $redis_test_key)) {
                        $err = 'Redis (short) Key does still exists (but should be expired after 5 seconds) and is not: ' . "\n" . $redis_test_key;
                    }
                    //end if
                }
                //end if
            }
            //end if
            //--
            if ((string) $err == '') {
                $tests[] = 'Set a long Redis Key (will not expire)';
                $redis_set_key = SmartPersistentCache::setKey('redis-server-tests', $redis_test_key, $redis_test_arch_content);
                if ($redis_set_key !== true) {
                    $err = 'Redis SetKey (long) returned a non-true result: ' . "\n" . $redis_test_key;
                }
                //end if
            }
            //end if
            //--
            if ((string) $err == '') {
                $tests[] = 'Check if Redis Key exists (after set)';
                if (!SmartPersistentCache::keyExists('redis-server-tests', $redis_test_key)) {
                    $err = 'Redis Key does not exists: ' . "\n" . $redis_test_key;
                }
                //end if
            }
            //end if
            //--
            if ((string) $err == '') {
                $tests[] = 'Get Redis Key';
                $redis_cached_value = SmartUtils::cache_variable_unarchive(SmartPersistentCache::getKey('redis-server-tests', $redis_test_key));
                if (Smart::array_size($redis_cached_value) > 0) {
                    $tests[] = 'Check if Redis Key is valid (array-keys)';
                    if ((string) $redis_cached_value['unicode-test'] != '' and (string) $redis_cached_value['big-key-test'] != '') {
                        $tests[] = 'Check if Redis Key is valid (checksum)';
                        if ((string) sha1(implode("\n", (array) $redis_cached_value)) == (string) $redis_test_checkum) {
                            if ($redis_test_value === $redis_cached_value) {
                                $tests[] = 'Unset Redis Key';
                                $redis_unset_key = SmartPersistentCache::unsetKey('redis-server-tests', $redis_test_key);
                                if ($redis_unset_key === true) {
                                    $tests[] = 'Check if Redis Key exists (after unset)';
                                    if (SmartPersistentCache::keyExists('redis-server-tests', $redis_test_key)) {
                                        $err = 'Redis Key does exists (after unset) and should not: ' . "\n" . $redis_test_key;
                                    } else {
                                        // OK
                                    }
                                    //end if
                                } else {
                                    $err = 'Redis UnSetKey returned a non-true result: ' . "\n" . $redis_test_key;
                                }
                                //end if else
                            } else {
                                $err = 'Redis Cached Value is broken: comparing stored value with original value failed on key: ' . "\n" . $redis_test_key;
                            }
                            //end if else
                        } else {
                            $err = 'Redis Cached Value is broken: checksum failed on key: ' . "\n" . $redis_test_key;
                        }
                        //end if else
                    } else {
                        $err = 'Redis Cached Value is broken: array-key is missing after Cache-Variable-Unarchive on key: ' . "\n" . $redis_test_key;
                    }
                    //end if
                } else {
                    $err = 'Redis Cached Value is broken: non-array value was returned after Cache-Variable-Unarchive on key: ' . "\n" . $redis_test_key;
                }
                //end if
            }
            //end if
            //--
            $title = 'SmartFramework Redis Server Tests: DONE ...';
            //--
            $time = 'TOTAL TIME (Except building the test archive) was: ' . (microtime(true) - $time);
            // substract the 3 seconds waiting time for Redis Key to expire
            //--
            $end_tests = '##### END TESTS ... ' . $time . ' sec. #####';
            //--
            if ((string) $err == '') {
                $img_sign = 'lib/core/img/sign_info.png';
                $img_check = 'lib/core/img/q_completed.png';
                $text_main = Smart::escape_js('<span style="color:#83B953;">Good ... Perfect &nbsp;&nbsp;&nbsp; :: &nbsp;&nbsp;&nbsp; グッド ... パーフェクト</span>');
                $text_info = Smart::escape_js('<h2><span style="color:#83B953;">All</span> the SmartFramework Redis Server Operations <span style="color:#83B953;">Tests PASSED on PHP</span><hr></h2><span style="font-size:14px;">' . Smart::nl_2_br(Smart::escape_html(implode("\n" . '* ', $tests) . "\n" . $end_tests)) . '</span>');
            } else {
                $img_sign = 'lib/core/img/sign_error.png';
                $img_check = 'lib/core/img/q_warning.png';
                $text_main = Smart::escape_js('<span style="color:#FF5500;">An ERROR occured ... &nbsp;&nbsp;&nbsp; :: &nbsp;&nbsp;&nbsp; エラーが発生しました ...</span>');
                $text_info = Smart::escape_js('<h2><span style="color:#FF5500;">A test FAILED</span> when testing Redis Server Operations.<span style="color:#FF5500;"><hr>FAILED Test Details</span>:</h2><br><span style="font-size:14px;"><pre>' . Smart::escape_html($err) . '</pre></span>');
            }
            //end if else
            //--
        } else {
            //--
            $title = 'SmartFramework Redis Server Tests - Redis Server was NOT SET ...';
            //--
            $img_sign = 'lib/core/img/sign_info.png';
            $img_check = 'lib/core/img/q_warning.png';
            $text_main = Smart::escape_js('<span style="color:#778899;">No Redis Server Tests performed ...</span>');
            $text_info = '<h2>The current configuration have not set the Redis Server ...</h2>';
            //--
        }
        //end if
        //--
        //--
        $html = <<<HTML
<h1>{$title}</h1>
<script type="text/javascript">
\tSmartJS_BrowserUtils.alert_Dialog(
\t\t'<img src="{$img_sign}" align="right"><h1>{$text_main}</h1><hr><span style="color:#333333;"><img src="{$img_check}" align="right">{$text_info}<br>',
\t\t'',
\t\t'Redis Server Test Suite for SmartFramework: PHP',
\t\t'725',
\t\t'480'
\t);
</script>
HTML;
        //--
        //--
        return $html;
        //--
    }
 private static function setInPersistentCache($the_cache_key, $y_data_arr)
 {
     //--
     if (SmartPersistentCache::isActive() and SmartPersistentCache::isMemoryBased()) {
         if (Smart::array_size($y_data_arr) > 0) {
             SmartPersistentCache::setKey('smart-regional-texts', (string) $the_cache_key, (string) Smart::seryalize((array) $y_data_arr));
         }
         //end if
     }
     //end if
     //--
 }