Completed
Push — master ( e3b8a9...e92bf7 )
by huang
05:36
created

helpers.php ➔ exception()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
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\Database;
14
use FastD\Model\Model;
15
use FastD\Model\ModelFactory;
16
use FastD\Routing\RouteCollection;
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
 * @return RouteCollection
31
 */
32
function route()
33
{
34
    return app()->get('router');
35
}
36
37
/**
38
 * @return Config
39
 */
40
function config()
41
{
42
    return app()->get('config');
43
}
44
45
/**
46
 * @return ServerRequestInterface
47
 */
48
function request()
49
{
50
    return app()->get('request');
51
}
52
53
/**
54
 * @return Response
55
 */
56
function response()
57
{
58
    return app()->get('response');
59
}
60
61
/**
62
 * @return Exception
63
 */
64
function exception()
65
{
66
    return app()->get('exception');
67
}
68
69
/**
70
 * @param array $content
71
 * @param int   $statusCode
72
 *
73
 * @return Response
74
 */
75
function json(array $content = [], $statusCode = Response::HTTP_OK)
76
{
77
    return new JsonResponse($content, $statusCode);
78
}
79
80
/**
81
 * @param $message
82
 * @param $statusCode
83
 *
84
 * @throws Exception
85
 */
86
function abort($message, $statusCode)
87
{
88
    throw new Exception((is_null($message) ? Response::$statusTexts[$statusCode] : $message), $statusCode);
89
}
90
91
/**
92
 * @return Logger
93
 */
94
function logger()
95
{
96
    return app()->get('logger');
97
}
98
99
/**
100
 * @param $key
101
 *
102
 * @return AbstractAdapter
103
 */
104
function cache($key = 'default')
105
{
106
    return app()->get('cache')->getCache($key);
107
}
108
109
/**
110
 * @param $key
111
 *
112
 * @return Database
113
 */
114
function database($key = 'default')
115
{
116
    return app()->get('database')->getConnection($key);
117
}
118
119
/**
120
 * @param $name
121
 * @param $key
122
 *
123
 * @return Model
124
 */
125
function model($name, $key = 'default')
126
{
127
    return ModelFactory::createModel($name, $key);
128
}
129
130
/**
131
 * @return \swoole_server
132
 */
133
function server()
134
{
135
    return app()->get('server');
136
}
137