-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.php
executable file
·130 lines (113 loc) · 2.81 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
<?php
/**
* Created by PhpStorm.
* User: yidashi
* Date: 16/6/17
* Time: 下午9:41
*/
if (! function_exists('value')) {
/**
* Return the default value of the given value.
*
* @param mixed $value
* @return mixed
*/
function value($value)
{
return $value instanceof Closure ? $value() : $value;
}
}
if (! function_exists('env')) {
/**
* Gets the value of an environment variable. Supports boolean, empty and null.
*
* @param string $key
* @param mixed $default
* @return mixed
*/
function env($key, $default = null)
{
$value = getenv($key);
if ($value === false) {
return value($default);
}
switch (strtolower($value)) {
case 'true':
case '(true)':
return true;
case 'false':
case '(false)':
return false;
case 'empty':
case '(empty)':
return '';
case 'null':
case '(null)':
return;
}
return $value;
}
}
if (! function_exists('url')) {
function url($url, $scheme = false)
{
return \yii\helpers\Url::to($url, $scheme);
}
}
if (! function_exists('array_get')) {
/**
* 通过foo.bar方式获取多维数组的值
* ```
* $arr = ['a' => 1. 'b' => ['c' => 2, 'd' => 3]];
* echo array_get($arr, b.d);
* ```
* 输出3
* @param $array
* @param $key
* @param null $default
* @return mixed
*/
function array_get($array, $key, $default = null)
{
return \yii\helpers\ArrayHelper::getValue($array, $key, $default);
}
}
if (! function_exists('p')) {
function p($var, $die = true)
{
echo '<pre>' . print_r($var, true), '</pre>';
if ($die) {
die;
}
}
}
if (! function_exists('config')) {
/**
* `config()`获取config组件
* `config('key')` 获取配置key的值
* `config([key,value])` 设置配置key的值为value
* @param null $key
* @param null $default
* @return array|bool|\config\components\Config|mixed
*/
function config($key = null, $default = null)
{
if (is_null($key)) {
return Yii::$app->config;
}
if (is_array($key)) {
return Yii::$app->config->set($key[0], $key[1]);
}
return Yii::$app->config->get($key, $default);
}
}
if (! function_exists('request')) {
function request($name = null, $defaultValue = null)
{
if (is_null($name)) {
return Yii::$app->request;
}
$params = Yii::$app->request->getQueryParams() + Yii::$app->request->getBodyParams();
return isset($params[$name]) ? $params[$name] : $defaultValue;
}
}