forked from Razzwan/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.php
120 lines (106 loc) · 4.05 KB
/
App.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
<?php
namespace liw\core;
use liw\core\access\AccessMulti;
use liw\core\web\Request;
use liw\core\web\Session;
class App
{
protected static $router;
/**
* Загружаем язык
* @param null|string $lang
* @throws \Exception
*/
static private function loadLanguage($lang = null)
{
if($lang !== null){
$file = LIW_WEB . 'config/languages/' . $lang . '.php';
if(file_exists($file)){
$_SESSION['language'] = $lang;
Liw::$lang = require $file;
return;
} else {
throw new \Exception("File " . $file . " not exist.");
}
}
if(!empty($_SESSION['language'])){
$file = LIW_WEB . 'config/languages/' . $_SESSION['language'] . '.php';
if(file_exists($file)){
Liw::$lang = require $file;
return;
}
}
if(isset($_SESSION['language'])) unset($_SESSION['language']);
$file = LIW_WEB . 'config/languages/' . Liw::$config['def_lang'] . '.php';
Liw::$lang = require $file;
}
/**
* Загружаем все необходимые данные и запускаем выполнение метода контроллера
*/
static public function start(){
set_error_handler("self::show_errors"); // изменение отображения ошибок по умолчанию
Liw::$config = require_once LIW_WEB . 'config/config.php';
try {
Session::start();
Request::getRequest();
self::loadLanguage(Request::$lang);
self::run(new Router(include LIW_WEB . "config/ways/all.php"));
}
catch (\Exception $e) {
self::show_errors($e->getCode(), $e->getMessage(), $e->getFile(), $e->getLine());
}
}
/**
* @param int $errno
* @param string $errstr
* @param string $file
* @param int $line
* @throws \Exception
*/
static public function show_errors($errno, $errstr, $file, $line)
{
$message = 'Error level: ' . $errno . '<hr>' . $errstr . '<hr>' . $file . '<hr>string: ' . $line . '<hr>';
$view = View::getView();
if (!defined('DEVELOP') || !DEVELOP){
//добавить логирование
$view->render('main', 'error', [
'error' => Liw::$lang['message']['error']
]);
} else {
$view->render('main', 'error', [
'error' => $message
]);
}
exit;
}
static public function run($router)
{
$router = $router->run();
$controller_route = '\web\controllers\\' . $router['action'][0] . 'Controller';
if (!class_exists($controller_route)) {
throw new \Exception(Liw::$lang['message']['no_controller'] . $router['action'][0] . 'Controller');
}
$controller_obj = new $controller_route();
if (!method_exists($controller_obj, $router['action'][1] . 'Action')) {
throw new \Exception(Liw::$lang['message']['no_action'] .
'<strong>' . $router['action'][1] . 'Action' . '</strong> in controller <strong>' .
$router['action'][0] . 'Controller' . '</strong>');
}
/**
* Если существует метод before, то запускаем его перед действием
*/
if (method_exists($controller_obj, "before")) {
call_user_func_array([$controller_obj, "before"], Request::$attr);
}
/**
* запускает метод контроллера с параметрами
*/
call_user_func_array([$controller_obj, $router['action'][1] . 'Action'], Request::$attr);
/**
* Если существует метод after, то запускаем его после действия
*/
if (method_exists($controller_obj, "after")) {
call_user_func_array([$controller_obj, "after"], Request::$attr);
}
}
}