-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathits-init.php
178 lines (146 loc) · 4.6 KB
/
its-init.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
<?php
use tsframe\App;
use tsframe\Autoload;
use tsframe\Config;
use tsframe\Hook;
use tsframe\Plugins;
use tsframe\exception\BaseException;
use tsframe\module\locale\Lang;
if(!defined('DS')) define('DS', DIRECTORY_SEPARATOR);
define('ITS_ROOT', __DIR__ . DS); // Корневая директория фреймворка
define('ITS_FRAME', ITS_ROOT . 'its-framework' . DS); // Директория с базовыми функциями фрейма
define('ITS_PLUGINS', ITS_ROOT . 'its-plugins' . DS); // Директория с базовыми плагинами
define('ITS_TEMPLATES', ITS_ROOT . 'its-templates' . DS); // Директория с базовыми шаблонами
define('ITS_STORAGE', ITS_ROOT . 'storage' . DS); // Директория c временной папкой, переводами и пр.
define('ITS_TEMP', ITS_STORAGE . 'temp' . DS);
define('ITS_UPLOAD', ITS_STORAGE . 'upload' . DS);
define('ITS_TRANSLATIONS', ITS_STORAGE . 'locale' . DS);
class itsFrame {
/**
* Инициализация путей фреймврока
* @param array $paths Массив с путями, доступные ключи:
* - root,
* - plugins,
* - storage,
* - upload,
* - temp,
* - translations
* - basePath
*/
public static function init(array $paths = []){
$basePath = '/';
foreach($paths as $key => $path){
if(strtolower($key) == 'basepath'){
$basePath = $path;
continue;
}
$pathKey = strtoupper('APP_' . $key);
if(!defined($pathKey)){
if(substr($path, -1, 1) !== DS){
$path .= DS;
}
define($pathKey, $path);
}
}
// Default roots
if(!defined('APP_ROOT'))
define('APP_ROOT', ITS_ROOT);
if(!defined('APP_PLUGINS'))
define('APP_PLUGINS', APP_ROOT . 'its-plugins' . DS);
if(!defined('APP_STORAGE'))
define('APP_STORAGE', APP_ROOT . 'storage' . DS);
if(!defined('APP_UPLOAD'))
define('APP_UPLOAD', APP_STORAGE . 'upload' . DS);
if(!defined('APP_TEMP'))
define('APP_TEMP', APP_STORAGE . 'temp' . DS);
if(!defined('APP_TRANSLATIONS'))
define('APP_TRANSLATIONS', APP_STORAGE . 'translations' . DS);
// Aliases for roots
define('CD', APP_ROOT); // Alias "current dir"
if(is_dir(APP_STORAGE)){
define('STORAGE', APP_STORAGE);
}
else {
define('STORAGE', ITS_STORAGE);
}
if(is_dir(APP_TEMP)){
define('TEMP', APP_TEMP);
}
else {
define('TEMP', ITS_TEMP);
}
if(is_dir(APP_UPLOAD)){
define('UPLOAD', APP_UPLOAD);
}
else {
define('UPLOAD', ITS_UPLOAD);
}
require ITS_FRAME . 'Autoload.php';
// Include composer
if(file_exists(ITS_ROOT . 'vendor/autoload.php')){
require ITS_ROOT . 'vendor/autoload.php';
}
Autoload::init();
Autoload::addRoot(ITS_FRAME);
// Путь к файлу настрек
Config::load(APP_ROOT . 'its-config.json');
// Базовая директория скрипта
App::setBasePath($basePath);
// Путь к директории с переводами
if(APP_TRANSLATIONS != ITS_TRANSLATIONS){
Lang::addTranslationPath(APP_TRANSLATIONS);
}
Lang::addTranslationPath(ITS_TRANSLATIONS);
self::registerMigrateHooks();
return new self;
}
private static function registerMigrateHooks(){
Hook::registerOnce('app.install', function(){
// Migrate from ts-frame
$oldCfg = APP_ROOT . 'ts-config.json';
if(file_exists($oldCfg)){
$data = json_decode(file_get_contents($oldCfg), true);
Config::set('*', $data);
@unlink($oldCfg);
Config::set('install_mode', true);
}
});
Hook::registerOnce('app.installed', function(){
// Migrate from ts-framework v1.0
$canReg = Config::get('user.canRegister');
if(!is_null($canReg)){
Config::set('user.auth.register', $canReg);
}
Config::unset('user.canRegister');
$canSocial = Config::get('user.canSocial');
if(!is_null($canSocial)){
Config::set('user.auth.social', $canSocial);
}
Config::unset('user.canSocial');
$loginUsed = Config::get('user.loginUsed');
if(!is_null($loginUsed)){
Config::set('user.auth.login', $loginUsed);
}
Config::unset('user.loginUsed');
});
}
public function setLanguages(array $langs = [], ?string $default = null){
Lang::setList($langs, $default);
return $this;
}
public function setLanguageAutoRedirect(bool $redirect){
Lang::setAutoRedirectToSubdomain($redirect);
return $this;
}
public function addPlugin(string $path){
Plugins::addCustom($path);
return $this;
}
public function onAppInit(callable $func){
Hook::registerOnce('app.init', $func, Hook::MIN_PRIORITY);
return $this;
}
public function start(){
App::start();
}
}