Passed
Push — master ( cea7d1...f386a3 )
by huang
03:09
created

helpers.php ➔ abort()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 1
nop 2
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
 * @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 $message
62
 * @param $statusCode
63
 *
64
 * @return Response
65
 */
66
function response($message, $statusCode)
67
{
68
    return new Response($message, $statusCode);
69
}
70
71
/**
72
 * @param $message
73
 * @param $statusCode
74
 *
75
 * @throws Exception
76
 */
77
function abort($message, $statusCode)
78
{
79
    throw new Exception((is_null($message) ? Response::$statusTexts[$statusCode] : $message), $statusCode);
80
}
81
82
/**
83
 * @param array $content
84
 * @param int   $statusCode
85
 * @param array $headers
86
 *
87
 * @return JsonResponse
88
 */
89
function json(array $content = [], $statusCode = Response::HTTP_OK, array $headers = [])
90
{
91
    return new JsonResponse($content, $statusCode, $headers);
92
}
93
94
/**
95
 * @return Logger
96
 */
97
function logger()
98
{
99
    return app()->get('logger');
100
}
101
102
/**
103
 * @param $key
104
 *
105
 * @return AbstractAdapter
106
 */
107
function cache($key = 'default')
108
{
109
    return app()->get('cache')->getCache($key);
110
}
111
112
/**
113
 * @param $key
114
 *
115
 * @return Database
116
 */
117
function database($key = 'default')
118
{
119
    return app()->get('database')->getConnection($key);
120
}
121
122
/**
123
 * @param $name
124
 * @param $key
125
 *
126
 * @return Model
127
 */
128
function model($name, $key = 'default')
129
{
130
    return ModelFactory::createModel($name, $key);
131
}
132
133
/**
134
 * @return \swoole_server
135
 */
136
function server()
137
{
138
    return app()->get('server');
139
}
140