Skip to content
This repository was archived by the owner on Jul 18, 2022. It is now read-only.

Commit bfb5624

Browse files
authored
Fixed #11
1 parent 9308f4d commit bfb5624

File tree

1 file changed

+139
-117
lines changed

1 file changed

+139
-117
lines changed

app/helpers.php

Lines changed: 139 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -13,146 +13,168 @@
1313
use App\Services\Http\RedirectResponse;
1414
use App\Services\Http\Response;
1515

16-
/**
17-
* Config 类的别名方法.
18-
*
19-
* @param string $property
20-
* @param mixed $default
21-
*
22-
* @return mixed
23-
*/
24-
function config($property, $default = null)
25-
{
26-
if (is_array($property)) {
27-
list($key, $value) = $property;
28-
29-
return Config::set($key, $value);
16+
if (!function_exists('config')) {
17+
/**
18+
* Config 类的别名方法.
19+
*
20+
* @param string $property
21+
* @param mixed $default
22+
*
23+
* @return mixed
24+
*/
25+
function config($property, $default = null)
26+
{
27+
if (is_array($property)) {
28+
list($key, $value) = $property;
29+
30+
return Config::set($key, $value);
31+
}
32+
33+
return Config::get($property, $default);
3034
}
31-
32-
return Config::get($property, $default);
3335
}
3436

35-
/**
36-
* @param string $template
37-
* @param array $data
38-
*
39-
* @return string|Response
40-
*/
41-
function view(string $template, array $data)
42-
{
43-
$engine = Registry::get('services.view');
37+
if (!function_exists('view')) {
38+
/**
39+
* @param string $template
40+
* @param array $data
41+
*
42+
* @return string|Response
43+
*/
44+
function view(string $template, array $data)
45+
{
46+
$engine = Registry::get('services.view');
4447

45-
if (!$engine) {
46-
abort('No template engine found.');
47-
}
48+
if (!$engine) {
49+
abort('No template engine found.');
50+
}
4851

49-
$body = $engine->render($template, (array) $data);
52+
$body = $engine->render($template, (array) $data);
5053

51-
return new Response(200, ['content-type' => 'text/html;charset=utf-8'], $body);
54+
return new Response(200, ['content-type' => 'text/html;charset=utf-8'], $body);
55+
}
5256
}
5357

54-
/**
55-
* @param array|object|string $data
56-
*
57-
* @return \App\Services\Http\Response
58-
*/
59-
function json($data)
60-
{
61-
return new Response(200, ['content-type' => 'application/json;charset=utf-8'], json_encode($data));
58+
if (!function_exists('json')) {
59+
/**
60+
* @param array|object|string $data
61+
*
62+
* @return \App\Services\Http\Response
63+
*/
64+
function json($data)
65+
{
66+
return new Response(200, ['content-type' => 'application/json;charset=utf-8'], json_encode($data));
67+
}
6268
}
6369

64-
/**
65-
* @param string $targetUrl
66-
*
67-
* @return \App\Services\Http\RedirectResponse
68-
*/
69-
function redirect(string $targetUrl)
70-
{
71-
return new RedirectResponse($targetUrl);
70+
if (!function_exists('redirect')) {
71+
/**
72+
* @param string $targetUrl
73+
*
74+
* @return \App\Services\Http\RedirectResponse
75+
*/
76+
function redirect(string $targetUrl)
77+
{
78+
return new RedirectResponse($targetUrl);
79+
}
7280
}
7381

74-
/**
75-
* 停止并抛出异常.
76-
*
77-
* @param string|array $message
78-
* @param int $code
79-
*
80-
* @throws ErrorException
81-
*/
82-
function abort($message = '系统错误', $code = 500)
83-
{
84-
if (is_array($message)) {
85-
$error = $message['error'] ?? false;
86-
$message = $error && is_string($error) ? $error : '未知错误';
82+
if (!function_exists('abort')) {
83+
/**
84+
* 停止并抛出异常.
85+
*
86+
* @param string|array $message
87+
* @param int $code
88+
*
89+
* @throws ErrorException
90+
*/
91+
function abort($message = '系统错误', $code = 500)
92+
{
93+
if (is_array($message)) {
94+
$error = $message['error'] ?? false;
95+
$message = $error && is_string($error) ? $error : '未知错误';
96+
}
97+
98+
throw new ErrorException($message, $code);
8799
}
88-
89-
throw new ErrorException($message, $code);
90100
}
91101

92-
/**
93-
* 是否的测试环境中.
94-
*
95-
* @return bool
96-
*/
97-
function running_unit_tests()
98-
{
99-
return defined('TESTING') && TESTING;
102+
if (!function_exists('running_unit_tests')) {
103+
/**
104+
* 是否的测试环境中.
105+
*
106+
* @return bool
107+
*/
108+
function running_unit_tests()
109+
{
110+
return defined('TESTING') && TESTING;
111+
}
100112
}
101113

102-
/**
103-
* 读取 ENV 配置.
104-
*
105-
* @param string $name
106-
*
107-
* @return mixed
108-
*/
109-
function env($name)
110-
{
111-
return getenv($name);
114+
if (!function_exists('env')) {
115+
/**
116+
* 读取 ENV 配置.
117+
*
118+
* @param string $name
119+
*
120+
* @return mixed
121+
*/
122+
function env($name)
123+
{
124+
return getenv($name);
125+
}
112126
}
113127

114-
/**
115-
* mobile dump and die.
116-
*
117-
* @param array ...$args
118-
*/
119-
function mdd(...$args)
120-
{
121-
ob_start();
122-
array_map('var_dump', $args);
123-
$content = html_entity_decode(strip_tags(ob_get_contents()));
124-
ob_end_clean();
125-
echo $content;
126-
exit;
128+
if (!function_exists('mdd')) {
129+
/**
130+
* mobile dump and die.
131+
*
132+
* @param array ...$args
133+
*/
134+
function mdd(...$args)
135+
{
136+
ob_start();
137+
array_map('var_dump', $args);
138+
$content = html_entity_decode(strip_tags(ob_get_contents()));
139+
ob_end_clean();
140+
echo $content;
141+
exit;
142+
}
127143
}
128144

129-
/**
130-
* Debug 日志.
131-
*
132-
* @param string $message
133-
* @param array $context
134-
*/
135-
function debug($message, $context = [])
136-
{
137-
\Log::debug($message, $context);
145+
if (!function_exists('debug')) {
146+
/**
147+
* Debug 日志.
148+
*
149+
* @param string $message
150+
* @param array $context
151+
*/
152+
function debug($message, $context = [])
153+
{
154+
\Log::debug($message, $context);
155+
}
138156
}
139157

140-
/**
141-
* 获取当前运行环境名称:dev/production.
142-
*
143-
* @return string
144-
*/
145-
function environment()
146-
{
147-
return getenv('APP_ENV') ?: 'dev';
158+
if (!function_exists('environment')) {
159+
/**
160+
* 获取当前运行环境名称:dev/production.
161+
*
162+
* @return string
163+
*/
164+
function environment()
165+
{
166+
return getenv('APP_ENV') ?: 'dev';
167+
}
148168
}
149169

150-
/**
151-
* 是不是开发环境.
152-
*
153-
* @return bool
154-
*/
155-
function is_dev()
156-
{
157-
return environment() == 'dev';
170+
if (!function_exists('is_dev')) {
171+
/**
172+
* 是不是开发环境.
173+
*
174+
* @return bool
175+
*/
176+
function is_dev()
177+
{
178+
return environment() == 'dev';
179+
}
158180
}

0 commit comments

Comments
 (0)