-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoader.php
More file actions
205 lines (175 loc) · 6.4 KB
/
Loader.php
File metadata and controls
205 lines (175 loc) · 6.4 KB
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
<?php
namespace org\opencomb\loader ;
use org\opencomb\platform\service\ServiceFactory;
use org\jecat\framework\mvc\controller\Request;
use org\jecat\framework\system\AccessRouter;
class Loader
{
const version = '0.1.0.0' ;
const default_platform_version = '0.3.2.0' ;
const default_framework_version = '0.7.2.0' ;
public function __construct()
{
require_once __DIR__.'/common.php';
$this->loadServiceSettings() ;
// 创建服务
if( !$arrServiceSetting =& $this->serviceSetting($_SERVER['HTTP_HOST']) )
{
throw new \Exception('requesting service is invalid: '.$_SERVER['HTTP_HOST']) ;
}
// framework/platform 的版本
if(empty($arrServiceSetting['framework_version']))
{
$arrServiceSetting['framework_version'] = self::default_framework_version ;
}
if(empty($arrServiceSetting['platform_version']))
{
$arrServiceSetting['platform_version'] = self::default_platform_version ;
}
$arrServiceSetting['framework_folder'] = \org\opencomb\platform\FRAMEWORK_FOLDER.'/'.$arrServiceSetting['framework_version'] ;
$arrServiceSetting['framework_url'] = \org\opencomb\platform\FRAMEWORK_URL.'/'.$arrServiceSetting['framework_version'] ;
$arrServiceSetting['platform_folder'] = \org\opencomb\platform\PLATFORM_FOLDER.'/'.$arrServiceSetting['platform_version'] ;
$arrServiceSetting['platform_url'] = \org\opencomb\platform\PLATFORM_URL.'/'.$arrServiceSetting['platform_version'] ;
// 加载 framework / platform
require_once $arrServiceSetting['framework_folder'].'/jc.init.php' ;
require_once $arrServiceSetting['platform_folder'].'/oc.init.php' ;
if( $this->isUpgradeData($arrServiceSetting) ){
$this->upgradeFrameworkData($arrServiceSetting);
$this->upgradePlatformData($arrServiceSetting);
}
$this->aServiceFactory = new ServiceFactory( $arrServiceSetting ) ;
ServiceFactory::setSingleton( $this->aServiceFactory );
}
public function startup()
{
// 创建请求的服务
$this->aServiceFactory->create() ;
}
public function startBaseSystem(){
$this->aServiceFactory->startBaseSystem() ;
}
public function launch(){
$this->startBaseSystem() ;
$this->startup();
// 根据路由设置创建控制器 并 执行
$aController = AccessRouter::singleton()->createRequestController(Request::singleton()) ;
if($aController)
{
$aController->mainRun() ;
}
else
{
header("HTTP/1.0 404 Not Found");
echo "<h1>Page Not Found</h1>" ;
}
}
private function loadServiceSettings()
{
$sServiceSettingFile = \org\opencomb\platform\SERVICES_FOLDER.'/settings.inc.php' ;
// load domain settings
if( !is_file($sServiceSettingFile) )
{
// domains missing or broken, rebuild it
if( $hServices = opendir(\org\opencomb\platform\SERVICES_FOLDER) )
{
while($sFilename=readdir($hServices))
{
if( $sFilename=='.' or $sFilename=='..')
{
continue ;
}
if( is_dir(\org\opencomb\platform\SERVICES_FOLDER.'/'.$sFilename) )
{
$this->arrServiceSettings[$sFilename] = array(
'domains' => array( $sFilename==='default'? '*': $sFilename ) ,
) ;
}
}
closedir($hServices) ;
if( !file_put_contents($sServiceSettingFile,'<?php return $arrServiceSettings = '.var_export($this->arrServiceSettings,true).';') )
{
throw new \Exception('can not write file: '.$sServiceSettingFile) ;
}
}
}
else
{
$this->arrServiceSettings = include $sServiceSettingFile ;
if(!is_array($this->arrServiceSettings))
{
throw new \Exception($sServiceSettingFile."文件遭到了损坏,删除该文件后,系统会自动重建。") ;
}
}
}
private function & serviceSetting($sHost)
{
if(isset($this->arrServiceSettings[$sHost]))
{
$this->arrServiceSettings[$sHost]['name'] = $sHost ;
// 服务数据目录路径
$this->arrServiceSettings[$sHost]['folder_name'] = $sHost ;
$this->arrServiceSettings[$sHost]['folder_path'] = \org\opencomb\platform\SERVICES_FOLDER . '/' . $sHost ;
return $this->arrServiceSettings[$sHost] ;
}
else
{
foreach($this->arrServiceSettings as $sServiceFolder=>&$arrServiceInfo)
{
foreach($arrServiceInfo['domains'] as &$sDomain)
{
if(fnmatch($sDomain,$sHost))
{
$arrServiceInfo['name'] = $sServiceFolder ;
// 服务数据目录路径
$arrServiceInfo['folder_name'] = $sServiceFolder ;
$arrServiceInfo['folder_path'] = \org\opencomb\platform\SERVICES_FOLDER . '/' . $sServiceFolder ;
return $arrServiceInfo ;
}
}
}
$arrService = null ;
return $arrService ;
}
}
public function __destruct(){
$sServiceSettingFile = \org\opencomb\platform\SERVICES_FOLDER.'/settings.inc.php' ;
if( !file_put_contents($sServiceSettingFile,'<?php return $arrServiceSettings = '.var_export($this->arrServiceSettings,true).';') )
{
throw new \Exception('can not write file: '.$sServiceSettingFile) ;
}
}
private function isUpgradeData(array $arrServiceSetting){
if( isset($arrServiceSetting['upgradeData']) ){
return (bool)$arrServiceSetting['upgradeData'];
}else{
return true;
}
}
private function upgradeFrameworkData(array &$arrServiceSetting){
if( isset($arrServiceSetting['framework'])
and isset($arrServiceSetting['framework']['dataVersion']) ){
$sDataVersionInSetting = $arrServiceSetting['framework']['dataVersion'];
}else{
$sDataVersionInSetting = '0.0.0.0';
}
$sDataVersionInFramework = \org\jecat\framework\DATA_VERSION ;
if( version_compare($sDataVersionInSetting , $sDataVersionInFramework ) < 0 ){
$sUpgradeName = 'upgrade_'.str_replace('.','_',$sDataVersionInSetting).'_To_'.str_replace('.','_',$sDataVersionInFramework);
$sFilename = $arrServiceSetting['framework_folder'].'/class/dataupgrade/'.$sUpgradeName.'.php';
if( file_exists( $sFilename ) ){
require_once($arrServiceSetting['framework_folder'].'/class/dataupgrade/IDataUpgrader.php');
include_once($sFilename);
$sClassName = 'org\jecat\framework\dataupgrade\\'.$sUpgradeName;
$aUpgrader = new $sClassName();
$aUpgrader->upgrade($arrServiceSetting);
$arrServiceSetting['framework']['dataVersion'] = $sDataVersionInFramework ;
}else{
throw new \Exception('framework的数据升级无法运行:未找到从'.$sDataVersionInSetting.'到'.$sDataVersionInFramework.'的数据升级程序');
}
}
}
private function upgradePlatformData(array $arrServiceSetting){
}
private $arrServiceSettings = array() ;
private $aServiceFactory = null ;
}