|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author jan huang <[email protected]> |
|
4
|
|
|
* @copyright 2016 |
|
5
|
|
|
* |
|
6
|
|
|
* @see https://www.github.com/janhuang |
|
7
|
|
|
* @see http://www.fast-d.cn/ |
|
8
|
|
|
*/ |
|
9
|
|
|
use FastD\Application; |
|
10
|
|
|
use FastD\Config\Config; |
|
11
|
|
|
use FastD\Http\JsonResponse; |
|
12
|
|
|
use FastD\Http\Response; |
|
13
|
|
|
use FastD\Model\Model; |
|
14
|
|
|
use FastD\Model\ModelFactory; |
|
15
|
|
|
use FastD\Routing\RouteCollection; |
|
16
|
|
|
use Medoo\Medoo; |
|
17
|
|
|
use Monolog\Logger; |
|
18
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
19
|
|
|
use Symfony\Component\Cache\Adapter\AbstractAdapter; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @return Application |
|
23
|
|
|
*/ |
|
24
|
|
|
function app() |
|
25
|
|
|
{ |
|
26
|
|
|
return Application::$app; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param $prefix |
|
31
|
|
|
* @param $callback |
|
32
|
|
|
* |
|
33
|
|
|
* @return RouteCollection |
|
34
|
|
|
*/ |
|
35
|
|
|
function route($prefix = null, callable $callback = null) |
|
36
|
|
|
{ |
|
37
|
|
|
if (null === $prefix) { |
|
38
|
|
|
return app()->get('router'); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
return app()->get('router')->group($prefix, $callback); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @return Config |
|
46
|
|
|
*/ |
|
47
|
|
|
function config() |
|
48
|
|
|
{ |
|
49
|
|
|
return app()->get('config'); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @return ServerRequestInterface |
|
54
|
|
|
*/ |
|
55
|
|
|
function request() |
|
56
|
|
|
{ |
|
57
|
|
|
return app()->get('request'); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param array $content |
|
62
|
|
|
* @param int $statusCode |
|
63
|
|
|
* @param array $headers |
|
64
|
|
|
* |
|
65
|
|
|
* @return JsonResponse |
|
66
|
|
|
*/ |
|
67
|
|
|
function json(array $content = [], $statusCode = Response::HTTP_OK, array $headers = []) |
|
68
|
|
|
{ |
|
69
|
|
|
$headers['X-Version'] = Application::VERSION; |
|
70
|
|
|
|
|
71
|
|
|
return new JsonResponse($content, $statusCode, $headers); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @return Logger |
|
76
|
|
|
*/ |
|
77
|
|
|
function logger() |
|
78
|
|
|
{ |
|
79
|
|
|
return app()->get('logger'); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param $key |
|
84
|
|
|
* |
|
85
|
|
|
* @return AbstractAdapter |
|
86
|
|
|
*/ |
|
87
|
|
|
function cache($key = 'default') |
|
88
|
|
|
{ |
|
89
|
|
|
return app()->get('cache')->getCache($key); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param $key |
|
94
|
|
|
* |
|
95
|
|
|
* @return Medoo |
|
96
|
|
|
*/ |
|
97
|
|
|
function database($key = 'default') |
|
98
|
|
|
{ |
|
99
|
|
|
return app()->get('database')->getConnection($key); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param $name |
|
104
|
|
|
* @param $key |
|
105
|
|
|
* |
|
106
|
|
|
* @return Model |
|
107
|
|
|
*/ |
|
108
|
|
|
function model($name, $key = 'default') |
|
109
|
|
|
{ |
|
110
|
|
|
return ModelFactory::createModel($name, $key); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @return \swoole_server |
|
115
|
|
|
*/ |
|
116
|
|
|
function server() |
|
117
|
|
|
{ |
|
118
|
|
|
return app()->get('server'); |
|
119
|
|
|
} |
|
120
|
|
|
|