-
Notifications
You must be signed in to change notification settings - Fork 0
/
YPi.php
211 lines (166 loc) · 4.98 KB
/
YPi.php
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php
define( 'SYS', dirname(__FILE__) );
define( 'DS', DIRECTORY_SEPARATOR );
defined( 'ROOT' )||define( 'ROOT', dirname(__FILE__).'/..' );
defined( 'CONTROL_DIR' )||define( 'CONTROL_DIR', ROOT.'/Controller' );
defined( 'MODULE_DIR' )||define( 'MODULE_DIR', ROOT.'/Module' );
defined( 'VIEW_DIR' )||define( 'VIEW_DIR', ROOT.'/View' );
defined( 'LANG_DIR' )||define( 'LANG_DIR', ROOT.'/Lang' );
defined( 'CACHE_DIR' )||define( 'CACHE_DIR', ROOT.'/Cache' );
defined( 'SYS_MODULE' )||define( 'SYS_MODULE', SYS.'/module' );
defined( 'DEBUG' )||define( 'DEBUG', TRUE );
defined( 'SMYCACHE' )||define( 'SMYCACHE', FALSE ); //smarty cache
defined( 'SMCOMPILE' )||define( 'SMCOMPILE', CACHE_DIR.'/compile' ); //smarty compile directory
defined( 'DB_DRIVER' )||define( 'DB_DRIVER', 'pdo' );
defined( 'DB_CHARSET' )||define( 'DB_CHARSET', 'utf8' );
defined( 'DB_USER' )||define( 'DB_USER', 'root' );
defined( 'DB_PASS' )||define( 'DB_PASS', '' );
defined( 'DB_PRE' )||define( 'DB_PRE', 'db_' );
defined( 'SESSION_ENGINE' )||define( 'SESSION_ENGINE', 'file' ); //file, apc, memcache
defined( 'SESSION_DIR' )||define( 'SESSION_DIR', CACHE_DIR . '/session' );
defined( 'MEMCACHE_HOST' )||define( 'MEMCACHE_HOST', '127.0.0.1' );
defined( 'MEMCACHE_PORT' )||define( 'MEMCACHE_PORT', '11211' );
if (!defined('COOKIE_DOMAIN')) {
$domain = $_SERVER['HTTP_HOST'];
$strpos = strpos( $domain, '.' );
if ( $strpos !== false ) {
$domain = substr( $domain, $strpos );
}
define('COOKIE_DOMAIN', $domain);
}
require 'smarty/Smarty.class.php';
require 'function.php';
require 'module/route.php';
//load Exception module
if ( file_exists(MODULE_DIR.'/exception.php') ) {
require MODULE_DIR.'/exception.php';
}
else {
require 'module/exception.php';
}
require 'module/cache.php';
require 'module/lang.php';
require 'module/connect.php';
require 'module/debug.php';
require 'module/controller.php';
require 'module/dbmodule.php';
require 'module/CHttpSession.php';
require 'module/CCache.php';
class YPi {
//static
static protected $YPi;
static protected $modules = array(); //loaded modules
static protected $modules_index = array(); //modules index
//private
private $page_screen_start;
//public
public static function & instance () {
if ( is_null( self::$YPi ) ) {
self::$YPi = new YPi();
}
return self::$YPi;
}
public function __construct() {
if ( !file_exists( SESSION_DIR ) ) {
makedir( SESSION_DIR );
}
CHttpSession::engine( SESSION_ENGINE );
ob_start();
$this->page_screen_start = microtime(true);
}
public function __destruct() {
$page_screen_end = microtime(true);
if ( DEBUG ) {
$code = ob_get_contents();
YPiDebug::showDebugWindow( $page_screen_end - $this->page_screen_start );
$message = ob_get_contents();
ob_clean();
$message = substr( $message, strlen($code) );
echo str_replace( '<!-- debug message -->', $message, $code );
}
ob_end_flush();
}
public function start () {
try {
$route = route::instance();
$route->render();
}
catch ( Exception $Error ) {
exit( $Error->__toString() );
}
}
//old start action
public static function run () {
$YPi = self::instance();
$YPi->start();
}
//old import method
public static function imports() {
$modules = func_get_args();
if ($modules) {
foreach ($modules as $item) {
$item = str_replace('.', '/', $item);
$file = SYS.'/module/'.$item.'.php';
if (file_exists($file)) {
if (DEBUG)
YPiDebug::import_module($file);
require_once $file;
}
}
}
}
public static function interfaces() {
$interface = func_get_args();
if ($interface) {
foreach ($interface as $item) {
$item = str_replace('.', '/', $item);
$file = SYS.'/interface/'.$item.'.php';
if (file_exists($file)) {
require $file;
}
}
}
}
static private function require_file ( $moduleFile ) {
$fileName = SYS_MODULE . '/' . $moduleFile;
if ( file_exists( $fileName ) ) {
require_once $fileName;
return true;
}
else {
$fileName = MODULE_DIR . '/' . $moduleFile;
if ( file_exists( $fileName ) ) {
require_once $fileName;
return true;
}
}
return false;
}
static public function loadModule($moduleName) {
if ( strpos( $moduleName, '.' ) !== -1 ) {
$moduleName = str_replace('.', '/', $moduleName);
}
$module_path = $moduleName . '.php';
$module_name = str_replace( '/', '_', $moduleName ) . '_module';
$index = array_search( $moduleName, self::$modules_index );
if ( $index !== false ) {
return self::$modules[ $index ];
}
$result = self::require_file( $module_path );
if ( $result ) {
if ( !class_exists( $module_name ) ) {
$module_name = basename( $moduleName );
$module_name .= '_module';
}
$module = @call_user_func( $module_name . '::instance' );
//Compatibility mode
if ( !$module ) {
$module = new $module_name;
}
array_push( self::$modules_index, $moduleName );
array_push( self::$modules, $module );
return $module;
}
return false;
}
}