|
13 | 13 | use App\Services\Http\RedirectResponse; |
14 | 14 | use App\Services\Http\Response; |
15 | 15 |
|
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); |
30 | 34 | } |
31 | | - |
32 | | - return Config::get($property, $default); |
33 | 35 | } |
34 | 36 |
|
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'); |
44 | 47 |
|
45 | | - if (!$engine) { |
46 | | - abort('No template engine found.'); |
47 | | - } |
| 48 | + if (!$engine) { |
| 49 | + abort('No template engine found.'); |
| 50 | + } |
48 | 51 |
|
49 | | - $body = $engine->render($template, (array) $data); |
| 52 | + $body = $engine->render($template, (array) $data); |
50 | 53 |
|
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 | + } |
52 | 56 | } |
53 | 57 |
|
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 | + } |
62 | 68 | } |
63 | 69 |
|
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 | + } |
72 | 80 | } |
73 | 81 |
|
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); |
87 | 99 | } |
88 | | - |
89 | | - throw new ErrorException($message, $code); |
90 | 100 | } |
91 | 101 |
|
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 | + } |
100 | 112 | } |
101 | 113 |
|
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 | + } |
112 | 126 | } |
113 | 127 |
|
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 | + } |
127 | 143 | } |
128 | 144 |
|
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 | + } |
138 | 156 | } |
139 | 157 |
|
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 | + } |
148 | 168 | } |
149 | 169 |
|
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 | + } |
158 | 180 | } |
0 commit comments