123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- <?php
- /**
- * Created by PhpStorm.
- * User: NODELOG
- * 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|\common\modules\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;
- }
- }
- if (! function_exists('app')) {
- function app($name = null)
- {
- if (is_null($name)) {
- return Yii::$app;
- }
- return Yii::$app->get($name);
- }
- }
- if (! function_exists('t')) {
- /**
- * @param $category
- * @param $message
- * @param array $params
- * @param null $language
- * @return string
- */
- function t($category, $message, $params = [], $language = null)
- {
- return Yii::t($category, $message, $params, $language);
- }
- }
|