Helly's system class

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
/*
=================================
H E L L Y
---------------------------------
@author : Fesor
@email : fesors@gmail.com
@homepage: http://helly.fesor.ru
@version : 0.8 RC1
@date : 07.08.2009
=================================
*/
$time = microtime(1);
define('ROOT', dirname(__FILE__).'/');
define('SYSTEM', ROOT.'system/');
define('APPS', ROOT.'apps/');
define('CACHE', ROOT.'cache/');
function __autoload($name)
{
require SYSTEM.system::$components[strtolower($name)];
}
//Initialisation of components
new cache;
system::$components['db'] = 'db/drivers/'.system::getConfig('dbDriver').'.php';
class system
{
private static $isAjax,
$_app,
$rebuildDataCache=false,
$rebuildMessagesCache=false,
$_messages,
$_data;
//Initialisation of system
function __construct()
{
//is site in offline mode?
if(!self::getConfig('offline'))
die(self::message('Site is offline!'));
//Define URI string and match route
new router;
//Load and dispatch application
self::$_app = new application( router::getAppName(), router::getRoute() );
//Return page
view::renderLayout();
}
function __destruct()
{
//Rebuild Cache
self::app()->clear();
if(self::$rebuildDataCache)
cache::set('data', self::$_data);
if(self::$rebuildMessagesCache)
cache::set('messages-'.self::$_data['configs']['_system']['local'], self::$_messages);
}
//return's singleton of application
public function app()
{
return self::$_app;
}
//return's configs
public function getConfig($name, $app="_system")
{
//Load configs if it isn't defined
if(!is_null(self::$_data))
{
if(cache::isCached('data'))
self::$_data = cache::get('data');
else
{
self::$_data['configs']['_system'] = require ROOT.'data/configs.php';
self::$rebuildDataCache = true;
}
}
if(!isset(self::$_data['configs'][$app]))
{
if($app == '_system')
self::$_data['configs']['_system'] = require ROOT.'data/configs.php';
else
self::$_data['configs'][$app] = require APPS.$app.'/data/configs.php';
self::$rebuildDataCache = true;
}
return self::$_data['configs'][$app][$name];
}
//return's params
public function getParams($name, $app="_system")
{
//Load params if it isn't defined
if(!is_null(self::$_data))
{
if(cache::isCached('data'))
self::$_data = cache::get('data');
else
{
self::$_data['params']['_system'] = require ROOT.'data/params.php';
self::$rebuildDataCache = true;
}
}
if(!isset(self::$_data['params'][$app]))
{
if($app == '_system')
self::$_data['params']['_system'] = require ROOT.'data/params.php';
else
self::$_data['params'][$app] = require APPS.$app.'/data/params.php';
self::$rebuildDataCache = true;
}
return self::$_data['params'][$app][$name];
}
public function message($msg, $app="_system")
{
if(is_null(self::$_messages) AND cache::isCached('messages-'.system::getConfig('locale')))
self::$_messages = cache::get('messages-'.system::getConfig('locale'));
if(!isset(self::$_messages[$app]))
{
if($app == '_system')
self::$_data = require ROOT.'data/messages/'.system::getConfig('locale').'.php';
else
self::$_data = require APPS.$app.'/data/messages/'.system::getConfig('locale').'.php';
self::$rebuildMessageCache = true;
}
return isset(self::$_messages[$app][$msg])? self::$_messages[$app][$msg] : $msg ;
}
//Check's is ajax callback or not
public function isAjaxCallback()
{
if(is_null(self::$isAjax))
self::$isAjax = ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest')? true:false;
return self::$isAjax;
}
public static $components = array(
'application' => 'base/application.php',
'model' => 'base/model.php',
'widget' => 'base/widget.php',
'controller' => 'base/controller.php',
'view' => 'base/view.php',
'cache' => 'cache/cache.php',
'viewcompiler' => 'base/view_compiler.php',
);
}
new system;
echo "\n<!-- Generated in ".round(microtime(1)-$time, 4)." seconds | Memory used: ".round(memory_get_usage()/1024)." KB -->";