-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.php
executable file
·148 lines (126 loc) · 3.39 KB
/
application.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
<?php
class Application {
/**
* @var Request
*/
protected $_request;
/**
* @var Application
*/
protected static $_instance = null;
/**
* @var array пути
*/
protected static $_paths = array();
/**
* @var array
*/
protected static $_folders = array('classes');
/**
* Создание запроса
*
* @param Request $request
* @return Application
*/
public function create($request) {
$this->_request = $request;
return $this;
}
/**
* Включение библиотеки с помощью
* файла её конфигурации
*
* @param string $lib
* @param string $libFile
*/
public function includeLib($lib, $libFile)
{
require_once self::getLibPath($lib) . $libFile;
}
/**
* Получить путь до библиотеки
* Не обязательно чтобы она существовала
*
* @static
* @param $lib
* @return string
*/
public static function getLibPath($lib)
{
return ROOT_PATH . 'library' . DIRECTORY_SEPARATOR .
$lib . DIRECTORY_SEPARATOR;
}
/**
* Регистрация библиотеки
*
* @param string $lib
* @param bool $init
*/
public function registyLib($lib, $init = FALSE) {
$libPath = self::getLibPath($lib);
self::registryAutoload($libPath);
if ($init != FALSE)
$this->includeLib($lib, $init);
}
/**
* Регистрация директории
*
* @param string $directory
*/
public function registryAutoload($directory) {
self::$_paths[] = $directory;
}
/**
* @static
* @param $fileName
* @param $folder classes|config or else
* @return string|FALSE
*/
public static function findFile($fileName, $folder = FALSE)
{
if ($folder == FALSE)
$searchDir = self::$_folders;
else $searchDir = array($folder);
foreach (self::$_paths as $path)
{
foreach ($searchDir as $folder)
{
$full = $path . $folder . DIRECTORY_SEPARATOR . $fileName;
if (file_exists($full) != FALSE)
return $full;
}
}
return FALSE;
}
public static function autoLoad($className) {
$realName = str_replace('_', DIRECTORY_SEPARATOR, $className);
$fileName = strtolower($realName) . '.php';
if ($path = self::findFile($fileName))
{
require_once $path;
}
return FALSE;
}
/**
* Init Application
*/
protected function __construct() {
spl_autoload_register('Application::autoLoad');
}
/**
* @static
* @return Application
*/
public static function getInstance() {
if(self::$_instance == null){
self::$_instance = new Application();
}
return self::$_instance;
}
public function run() {
$request = $this->_request;
$request->execute();
$request->acceptHeaders();
return $request->getBody();
}
}