Lotus.php
3.7 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
<?php
class Lotus
{
/**
* Lotus Option array
*
* @var array array(
* "proj_dir" =>
* "app_name" =>
* "autoload_dir" =>
* );
*/
public $option;
public $devMode = true;
public $defaultStoreDir;
protected $proj_dir;
protected $app_dir;
protected $data_dir;
protected $lotusRuntimeDir;
protected $coreCacheHandle;
public function __construct()
{
$this->lotusRuntimeDir = dirname(__FILE__) . DIRECTORY_SEPARATOR;
}
public function init()
{
$underMVC = false;
if (isset($this->option["proj_dir"]) && !empty($this->option["proj_dir"]))
{
$this->proj_dir = rtrim($this->option["proj_dir"], '\\/') . '/';
if (isset($this->option["app_name"]) && !empty($this->option["app_name"]))
{
$this->app_dir = $this->proj_dir . "app/" . $this->option["app_name"] . "/";
$this->data_dir = $this->proj_dir . "data/" . $this->option["app_name"] . "/";
$underMVC = true;
}
else
{
trigger_error("Lotus option [app_name] is missing.");
}
}
/**
* Load core component
*/
require_once $this->lotusRuntimeDir . "Store.php";
require_once $this->lotusRuntimeDir . "StoreMemory.php";
require_once $this->lotusRuntimeDir . "StoreFile.php";
if ($this->defaultStoreDir)
{
if ($defaultStoreDir = realpath($this->defaultStoreDir))
{
LtStoreFile::$defaultStoreDir = $defaultStoreDir;
}
else
{
trigger_error("invalid [default store dir]: " . $this->defaultStoreDir);
}
}
if (!$this->devMode)
{
/**
* accelerate LtAutoloader, LtConfig
*/
$this->coreCacheHandle = new LtStoreFile;
$prefix = sprintf("%u", crc32(serialize($this->app_dir)));
$this->coreCacheHandle->prefix = 'Lotus-' . $prefix;
$this->coreCacheHandle->useSerialize = true;
$this->coreCacheHandle->init();
}
/**
* Init Autoloader, do this before init all other lotusphp component.
*/
$this->prepareAutoloader();
/**
* init Config
*/
$this->prepareConfig();
/**
* Run dispatcher when under MVC mode
*/
if ($underMVC)
{
$this->runMVC();
}
}
/**
* Autoload all lotus components and user-defined libraries;
*/
protected function prepareAutoloader()
{
require_once $this->lotusRuntimeDir . "Autoloader/Autoloader.php";
$autoloader = new LtAutoloader;
$autoloader->autoloadPath[] = $this->lotusRuntimeDir;
if (isset($this->option["autoload_dir"]))
{
$autoloader->autoloadPath[] = $this->option["autoload_dir"];
}
if ($this->proj_dir)
{
is_dir($this->proj_dir . 'lib') && $autoloader->autoloadPath[] = $this->proj_dir . 'lib';
is_dir($this->app_dir . 'action') && $autoloader->autoloadPath[] = $this->app_dir . 'action';
is_dir($this->app_dir . 'lib') && $autoloader->autoloadPath[] = $this->app_dir . 'lib';
}
if (!$this->devMode)
{
$autoloader->storeHandle = $this->coreCacheHandle;
}
$autoloader->init();
}
protected function prepareConfig()
{
$this->configHandle = LtObjectUtil::singleton('LtConfig');
if (!$this->devMode)
{
$configFile = 'conf/conf.php';
$this->configHandle->storeHandle = $this->coreCacheHandle;
}
else
{
$configFile = 'conf/conf_dev.php';
}
$this->configHandle->init();
if ($this->app_dir && is_file($this->app_dir . $configFile))
{
$this->configHandle->loadConfigFile($this->app_dir . $configFile);
}
}
protected function runMVC()
{
$router = LtObjectUtil::singleton('LtRouter');
$router->init();
$dispatcher = LtObjectUtil::singleton('LtDispatcher');
$dispatcher->viewDir = $this->app_dir . 'view/';
$dispatcher->viewTplDir = $this->data_dir . 'templateView/';
if (!$this->devMode)
{
$dispatcher->viewTplAutoCompile = false;
}
else
{
$dispatcher->viewTplAutoCompile = true;
}
$dispatcher->dispatchAction($router->module, $router->action);
}
}