This repository has been archived by the owner on Mar 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCore.php
267 lines (234 loc) · 8.29 KB
/
Core.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
<?php
/**********************************************************************************************
___ __ __ ______ __
/ | ____ ____ / /__ _____/ /_ / ____/________ _____ ___ ___ _ ______ _____/ /__
/ /| | / __ \/ __ \/ / _ \/ ___/ __// /_ / ___/ __ `/ __ `__ \/ _ \ | /| / / __ \/ ___/ //_/
/ ___ |/ /_/ / /_/ / / __(__ ) /_ / __/ / / / /_/ / / / / / / __/ |/ |/ / /_/ / / / ,<
/_/ |_/ .___/ .___/_/\___/____/\__//_/ /_/ \__,_/_/ /_/ /_/\___/|__/|__/\____/_/ /_/|_|
/_/ /_/
***********************************************************************************************/
/**
* ApplestFramework - Go to the next generation of PHP.
*
* @version 3.0
* @package ApplestFramework
* @author Yusei Yamanaka<[email protected]>
* @copyright 2013- Applest
* @license MIT License<http://opensource.org/licenses/mit-license.php>
* @link http://fw.applest.net/
*/
define('BASE_DIR', dirname(__FILE__));
define('ROUTE_FILE', BASE_DIR.'/Route.php');
define('ENVIRONMENT_DIR', BASE_DIR.'/environment');
$core_classes = array('View', 'Event', 'Vendor', 'Redis', 'Session', 'Hash', 'Validation', 'Validator', 'Model', 'Type', 'MySQL', 'Util', 'Input', 'Response', 'Cookie', 'Config', 'Route', 'Router', 'Environment', 'Logic', 'Controller', 'Log', 'Test', 'Console', 'Exception');
foreach ($core_classes as $class) {
require(BASE_DIR.'/core/'.$class.'.class.php');
}
/**
* Run the action in the controller.
* コントローラーの中のアクションを実行します
*
* @param string $controller_target
*/
function dispatchAction($controller_target = null) {
$af = new ApplestFramework();
$af->dispatchAction($controller_target);
}
/**
* The core of the framework.
* フレームワークのコア
*
* @package ApplestFramework
*/
class ApplestFramework {
private $controller_target;
/**
* Called by spl_autoload_register.
* __autoloadにスタックするspl_autoload_registerにより呼ばれます
*
* @param string $class classname
*/
public static function autoloader($class) {
if(!preg_match("/^_?[A-z0-9]+$/",$class)) return;
if(Util::ends_with($class, 'Model')) {
eval("class $class extends Model { protected static \$instance = null; }");
}
if(Util::ends_with($class, 'Type')) {
$type_file = Config::get('path.type').'/'.$class.'.class.php';
if(file_exists($type_file)) {
include($type_file);
}else{
eval("class $class extends Type {}");
}
}
if(Util::ends_with($class, 'Logic')) {
include(Config::get('path.logic').'/'.$class.'.class.php');
}
}
public function __construct() {
mb_internal_encoding('utf-8');
spl_autoload_register('ApplestFramework::autoloader', true, true);
// タイムゾーン未設定に対応
if(!ini_get('date.timezone')) {
Log::warn('タイムゾーンの設定が行われていません。今すぐにタイムゾーンの設定をphp.iniに書き込むべきです!');
Log::warn('There is not system\'s timezone settings. You shohuld write timezone on php.ini so quickly.');
date_default_timezone_set(@date_default_timezone_get());
}
switch (Environment::get()) {
case Environment::PRODUCTION:
define('CONFIG_FILE', BASE_DIR.'/Config.production.php');
break;
case Environment::TEST:
define('CONFIG_FILE', BASE_DIR.'/Config.test.php');
break;
case Environment::STAGING:
define('CONFIG_FILE', BASE_DIR.'/Config.staging.php');
break;
case Environment::DEVELOPMENT:
define('CONFIG_FILE', BASE_DIR.'/Config.development.php');
break;
case Environment::NONE:
define('CONFIG_FILE', BASE_DIR.'/Config.php');
break;
}
// デバッグ設定
if(Config::get('debug', false)) {
error_reporting(-1);
ini_set('display_errors', 1);
}else{
error_reporting(0);
ini_set('display_errors', 0);
}
// ログ設定
if(!is_null(Config::get('log_level'))) {
Log::level(Config::get('log_level'));
}
// URL正規化
$base_url = Config::get('base_url');
if(!is_null($base_url)) {
// 現在のURL(ベース部分)を取得
$real_base_url = (empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] == 'off' ? 'http://' : 'https://') . $_SERVER['HTTP_HOST'];
// もしbase_urlと異なる場合はリダイレクト
if($base_url !== $real_base_url) {
Response::redirect($_SERVER['REQUEST_URI']);
exit;
}
}
// MySQL使用
if(in_array('mysql', Config::get('use'))) {
$db = MySQL::getInstance();
$db->connect(sprintf('mysql:dbname=%s;host=%s', Config::get('mysql.name'), Config::get('mysql.host')), Config::get('mysql.user'), Config::get('mysql.password'));
}
if(in_array('session', Config::get('use'))) {
// $session_path = Config::get('session.path');
// if(!$session_path) {
// throw new Exception('No Define Session Path!');
// }
// Session::setPath($session_path);
}
}
public function dispatchAction($controller_name = null) {
try {
$this->dispatchActionThrowableException($controller_name);
} catch (Exception $e) {
Log::error("Exception occurred: ".$e->getMessage());
if(Config::get('debug', false)) {
throw $e;
}
}
}
private function dispatchActionThrowableException($controller_name) {
if(!is_null($controller_name)) {
$route = Route::run($controller_name.'#exec');
$this->run($route);
return;
}
$route = Router::get();
if($route->type === Route::T_RUN) {
$this->run($route);
return;
}
// ターゲットが静的なファイルだった場合
if($route->type === Route::T_FILE) {
if(file_exists(Config::get('path.public').'/'.$route->file_path)) {
echo file_get_contents(Config::get('path.public').'/'.$route->file_path);
}else{
throw new Exception("Does not exists static file.");
}
return;
}
if($route->type === Route::T_TEMPLATE) {
if(file_exists(Config::get('path.template').'/'.$route->file_path)) {
View::template($route->file_path, null);
}else{
throw new Exception("Does not exists template file.");
}
return;
}
// ターゲットが出力だった場合
if($route->type === Route::T_OUTPUT) {
echo $route->output_str;
return;
}
throw new Exception("Route Error!");
}
private function run($route) {
if(!class_exists($route->controller_name)) {
if(!file_exists(Config::get('path.controller').'/'.$route->controller_name.'.class.php')) {
throw new Exception("Does not exists controller script or static file.");
}
require_once(Config::get('path.controller').'/'.$route->controller_name.'.class.php');
}
$controller = new $route->controller_name();
// _BeforeAction
$before_action = Config::get('path.action').'/_BeforeAction.class.php';
if(file_exists($before_action) && $action->before_action) {
require_once($before_action);
$before_action = new _BeforeAction;
if(method_exists($before_action, 'smarty_exec')) {
list($smarty, $action->data) = $before_action->smarty_exec($this->getSmartyObject());
}else{
$action->data = $before_action->exec();
}
}
// メイン
$controller_ref = new ReflectionClass($controller);
if(method_exists($controller, $route->controller_method) && $controller_ref->getParentClass()->name === 'APIController') {
try {
$controller->before();
$method = $route->controller_method;
$result = $controller->$method();
$controller->after();
View::api($result);
} catch (Error $e) {
Response::statusCode($e->getStatusCode());
View::api(array('message' => $e->getMessage()), false);
} catch (Exception $e) {
throw $e;
}
}else if(method_exists($controller, $route->controller_method)) {
try {
$controller->before();
$method = $route->controller_method;
$controller->$method();
$controller->after();
} catch (Error $e) {
Response::statusCode($e->getStatusCode());
echo $e->getMessage();
} catch (Exception $e) {
throw $e;
}
}else if(method_exists($controller, 'exec')) {
$controller->exec();
}else{
throw new Exception("There is no method that can be run.", 1);
}
// _AfterAction
$after_action = Config::get('path.action').'/_AfterAction.class.php';
if(file_exists($after_action) && $action->after_action) {
require_once($after_action);
$after_action = new _BeforeAction;
$after_action->exec();
}
}
}