-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.php
208 lines (194 loc) · 5.01 KB
/
helpers.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
use ManaPHP\Di;
if (!function_exists('abort')) {
/**
* Throw an ManaPHP\Application\AbortException with the given data.
*
* @param int $code
* @param string $message
*
* @throws \ManaPHP\Application\AbortException
*
* @return void
*/
function abort($code, $message = '')
{
Di::getDefault()->application->abort($code, $message);
}
}
if (!function_exists('abort_if')) {
/**
* Throw an ManaPHP\Application\AbortException with the given data if the given condition is true.
*
* @param bool $boolean
* @param int $code
* @param string $message
*
* @throws \ManaPHP\Application\AbortException
*
* @return void
*/
function abort_if($boolean, $code, $message = '')
{
if ($boolean) {
abort($code, $message);
}
}
}
if (!function_exists('app')) {
/**
* Get the Application instance.
*
* @return \ManaPHP\ApplicationInterface
*/
function app()
{
return Di::getDefault()->application;
}
}
if (!function_exists('cache')) {
/**
* Get / set the specified cache value.
*
* If an array is passed, we'll assume you want to put to the cache.
*
* @param dynamic key|key,default|key,data,expiration|null
*
* @throws \Exception
*
* @return \ManaPHP\CacheInterface|string|false
*/
function cache()
{
$arguments = func_get_args();
switch (count($arguments)) {
case 0:
return Di::getDefault()->cache;
break;
case 1:
return Di::getDefault()->cache->get($arguments[0]);
case 2:
return Di::getDefault()->cache->get($arguments) ?: $arguments[1];
case 3:
Di::getDefault()->cache->set($arguments[0], $arguments[1], $arguments[2]);
return;
default:
throw new \ManaPHP\Cache\Exception('too many arguments');
}
}
}
if (!function_exists('request')) {
/**
* Get an instance of the current request or an input item from the request.
*
* @param string $key
* @param string $rule
* @param mixed $default
*
* @return \ManaPHP\Http\Request|string|array
*/
function request($key = null, $rule = null, $default = null)
{
if ($key === null) {
return Di::getDefault()->request;
} else {
return Di::getDefault()->request->get($key, $rule, $default);
}
}
}
if (!function_exists('response')) {
/**
* Return a new response from the application.
*
* @param null|string|array $content
*
* @return \ManaPHP\Http\ResponseInterface
*/
function response($content = null)
{
if ($content === null) {
return Di::getDefault()->response;
} elseif (is_array($content)) {
return Di::getDefault()->response->setJsonContent($content);
} else {
return Di::getDefault()->response->setContent($content);
}
}
}
if (!function_exists('translate')) {
/**
* Translate the given message.
*
* @param string $id
* @param array $parameters
*
* @return \ManaPHP\I18n\TranslationInterface |string
*/
function translate($id = null, $parameters = [])
{
if (!$id) {
return Di::getDefault()->translation;
} else {
return Di::getDefault()->translation->translate($id, $parameters);
}
}
}
if (!function_exists('url')) {
/**
* Generate a url for the application.
*
* @param string $path
* @param array $parameters
* @param string $module
*
* @return \ManaPHP\Mvc\UrlInterface|string
*/
function url($path = null, $parameters = [], $module = null)
{
if ($path === null) {
return Di::getDefault()->url;
} else {
return Di::getDefault()->url->get($path, $parameters, $module);
}
}
}
if (!function_exists('info')) {
/**
* Write some information to the log.
*
* @param string $message
* @param array $context
*
* @return void
*/
function info($message, $context = [])
{
Di::getDefault()->logger->info($message, $context);
}
}
if (!function_exists('session')) {
/**
* Get / set the specified session value.
*
* If an array is passed as the key, we will assume you want to set an array of values.
*
* @param array|string $key
* @param mixed $default
*
* @return mixed
*/
function session($key = null, $default = null)
{
if ($key === null) {
return Di::getDefault()->session;
}
if (is_array($key)) {
$session = Di::getDefault()->session;
foreach ($key as $k => $v) {
$session->set($k, $v);
}
} else {
return Di::getDefault()->session->get($key, $default);
}
}
}