function.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: NODELOG
  5. * Date: 16/6/17
  6. * Time: 下午9:41
  7. */
  8. if (! function_exists('value')) {
  9. /**
  10. * Return the default value of the given value.
  11. *
  12. * @param mixed $value
  13. * @return mixed
  14. */
  15. function value($value)
  16. {
  17. return $value instanceof Closure ? $value() : $value;
  18. }
  19. }
  20. if (! function_exists('env')) {
  21. /**
  22. * Gets the value of an environment variable. Supports boolean, empty and null.
  23. *
  24. * @param string $key
  25. * @param mixed $default
  26. * @return mixed
  27. */
  28. function env($key, $default = null)
  29. {
  30. $value = getenv($key);
  31. if ($value === false) {
  32. return value($default);
  33. }
  34. switch (strtolower($value)) {
  35. case 'true':
  36. case '(true)':
  37. return true;
  38. case 'false':
  39. case '(false)':
  40. return false;
  41. case 'empty':
  42. case '(empty)':
  43. return '';
  44. case 'null':
  45. case '(null)':
  46. return;
  47. }
  48. return $value;
  49. }
  50. }
  51. if (! function_exists('url')) {
  52. function url($url, $scheme = false)
  53. {
  54. return \yii\helpers\Url::to($url, $scheme);
  55. }
  56. }
  57. if (! function_exists('array_get')) {
  58. /**
  59. * 通过foo.bar方式获取多维数组的值
  60. * ```
  61. * $arr = ['a' => 1. 'b' => ['c' => 2, 'd' => 3]];
  62. * echo array_get($arr, b.d);
  63. * ```
  64. * 输出3
  65. * @param $array
  66. * @param $key
  67. * @param null $default
  68. * @return mixed
  69. */
  70. function array_get($array, $key, $default = null)
  71. {
  72. return \yii\helpers\ArrayHelper::getValue($array, $key, $default);
  73. }
  74. }
  75. if (! function_exists('p')) {
  76. function p($var, $die = true)
  77. {
  78. echo '<pre>' . print_r($var, true), '</pre>';
  79. if ($die) {
  80. die;
  81. }
  82. }
  83. }
  84. if (! function_exists('config')) {
  85. /**
  86. * `config()`获取config组件
  87. * `config('key')` 获取配置key的值
  88. * `config([key,value])` 设置配置key的值为value
  89. * @param null $key
  90. * @param null $default
  91. * @return array|bool|\common\modules\config\components\Config|mixed
  92. */
  93. function config($key = null, $default = null)
  94. {
  95. if (is_null($key)) {
  96. return Yii::$app->config;
  97. }
  98. if (is_array($key)) {
  99. return Yii::$app->config->set($key[0], $key[1]);
  100. }
  101. return Yii::$app->config->get($key, $default);
  102. }
  103. }
  104. if (! function_exists('request')) {
  105. function request($name = null, $defaultValue = null)
  106. {
  107. if (is_null($name)) {
  108. return Yii::$app->request;
  109. }
  110. $params = Yii::$app->request->getQueryParams() + Yii::$app->request->getBodyParams();
  111. return isset($params[$name]) ? $params[$name] : $defaultValue;
  112. }
  113. }
  114. if (! function_exists('app')) {
  115. function app($name = null)
  116. {
  117. if (is_null($name)) {
  118. return Yii::$app;
  119. }
  120. return Yii::$app->get($name);
  121. }
  122. }
  123. if (! function_exists('t')) {
  124. /**
  125. * @param $category
  126. * @param $message
  127. * @param array $params
  128. * @param null $language
  129. * @return string
  130. */
  131. function t($category, $message, $params = [], $language = null)
  132. {
  133. return Yii::t($category, $message, $params, $language);
  134. }
  135. }