root/trunk/lib/AkActiveRecord/AkDbSchemaCache.php

Revision 1399, 4.9 kB (checked in by bermi, 6 months ago)

When loading associations using load(), it will always return false is no associates are found.

Converting most Active record files to vanilla PHP5

Line 
1 <?php
2
3 // +----------------------------------------------------------------------+
4 // | Akelos Framework - http://www.akelos.org                             |
5 // +----------------------------------------------------------------------+
6 // | Released under the GNU Lesser General Public License, see LICENSE.txt|
7 // +----------------------------------------------------------------------+
8
9 /**
10  * @package ActiveRecord
11  * @subpackage Base
12  * @component DbSchemaCache
13  * @author Arno Schneider <arno a.t bermilabs c.om>
14  * @author Bermi Ferrer <bermi a.t bermilabs c.om>
15  * @copyright Copyright (c) 2002-2009, The Akelos Team http://www.akelos.org
16  * @license GNU Lesser General Public License <http://www.gnu.org/copyleft/lesser.html>
17  */
18
19 class AkDbSchemaCache
20 {
21     public function shouldRefresh($set = null)
22     {
23         static $refresh;
24         if(!isset($refresh)){
25             $refresh = !AK_ACTIVE_RECORD_CACHE_DATABASE_SCHEMA;
26         }
27         $refresh = is_null($set) ? $refresh : $set;
28         return $refresh;
29     }
30
31     public function getCacheFileName($environment = AK_ENVIRONMENT)
32     {
33         return AkDbSchemaCache::getCacheDir().DS.$environment.'.serialized';
34     }
35
36     public function getCacheDir()
37     {
38         $cache_dir = AK_CONFIG_DIR;
39         if (defined('AK_CONFIG_CACHE_TMP') && AK_CONFIG_CACHE_TMP) {
40             $cache_dir  = AK_TMP_DIR.DS.'ak_config';
41         }
42         return $cache_dir.DS.'cache'.DS.'activerecord';
43     }
44
45     public function clear($table, $environment = AK_ENVIRONMENT)
46     {
47         AkDbSchemaCache::_config($table, null, $environment, true);
48         AkDbSchemaCache::_config('database_table_internals_'.$table, null, $environment, true);
49         AkDbSchemaCache::_updateCacheFileAfterExecution($environment);
50         if(AK_LOG_EVENTS){
51             $Logger =& Ak::getLogger();
52             $Logger->message('Clearing database settings cache for '.$table);
53         }
54     }
55
56     public function clearAll()
57     {
58         if(AK_LOG_EVENTS){
59             $Logger =& Ak::getLogger();
60             $Logger->message('Clearing all database settings from cache');
61         }
62         Ak::directory_delete(AkDbSchemaCache::getCacheDir());
63     }
64
65     public function get($key, $environment = AK_ENVIRONMENT)
66     {
67         return AkDbSchemaCache::_config($key, null, $environment, false);
68     }
69
70     public function set($key, $value, $environment = AK_ENVIRONMENT)
71     {
72         AkDbSchemaCache::_updateCacheFileAfterExecution($environment);
73         return AkDbSchemaCache::_config($key, $value, $environment, !is_null($value));
74     }
75
76     public function _updateCacheFileAfterExecution($environment = null)
77     {
78         static $called = false, $_environment;
79         if($called == false && !AkDbSchemaCache::shouldRefresh()){
80             register_shutdown_function(array('AkDbSchemaCache','_updateCacheFileAfterExecution'));
81             $called =  !empty($environment) ? $environment : AK_ENVIRONMENT;
82         }elseif(empty($environment)){
83             $config = AkDbSchemaCache::_config(null, null, $called);
84             $file_name = AkDbSchemaCache::getCacheFileName($called);
85
86             /**
87             * @todo On PHP5 var_export requires objects that implement the __set_state magic method.
88             *       As see on stangelanda at arrowquick dot benchmarks at comhttp://php.net/var_export
89             *       serialize works faster without opcode caches. We should do our benchmarks with
90             *       var_export VS serialize using APC once we fix the __set_state magic on phpAdoDB
91             */
92             if(AK_LOG_EVENTS){
93                     $Logger =& Ak::getLogger();
94             }
95             if(!AK_CLI) {
96                 if(AK_LOG_EVENTS){
97                     $Logger->message('Updating database settings on '.$file_name);
98                 }
99                 Ak::file_put_contents($file_name, serialize($config));
100             } else if(AK_LOG_EVENTS){
101                 $Logger->message('Skipping writing of cache file: '.$file_name);
102             }
103         }
104     }
105
106     public function _config($key = null, $value = null, $environment = AK_ENVIRONMENT, $unset = false)
107     {
108         if(AkDbSchemaCache::shouldRefresh()){
109             return false;
110         }
111         static $config;
112         if(!isset($config[$environment])){
113             $file_name = AkDbSchemaCache::getCacheFileName($environment);
114             $config[$environment] = file_exists($file_name) ? unserialize(Ak::file_get_contents($file_name)) : array();
115             if(AK_LOG_EVENTS){
116                 $Logger =& Ak::getLogger();
117                 $Logger->message('Loading cached database settings');
118             }
119         }
120         if(!is_null($key)){
121             if(!is_null($value)){
122                 $config[$environment][$key] = $value;
123             }elseif($unset){
124                 unset($config[$environment][$key]);
125             }
126             return isset($config[$environment][$key]) ? $config[$environment][$key] : false;
127         }
128         return $config[$environment];
129     }
130 }
131
132 ?>
Note: See TracBrowser for help on using the browser.