You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
109 lines
2.5 KiB
109 lines
2.5 KiB
5 years ago
|
<?php
|
||
|
|
||
|
namespace app\hejiang;
|
||
|
|
||
|
if (!function_exists('env')) {
|
||
|
/**
|
||
|
* Gets the value of an environment variable.
|
||
|
*
|
||
|
* @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;
|
||
|
}
|
||
|
|
||
|
if (strlen($value) > 1 && str_starts_with($value, '"') && str_ends_with($value, '"')) {
|
||
|
return substr($value, 1, -1);
|
||
|
}
|
||
|
|
||
|
return $value;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
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('str_starts_with')) {
|
||
|
/**
|
||
|
* Determine if a given string starts with a given substring.
|
||
|
*
|
||
|
* @param string $haystack
|
||
|
* @param string|array $needles
|
||
|
* @return bool
|
||
|
*/
|
||
|
function str_starts_with($haystack, $needles)
|
||
|
{
|
||
|
foreach ((array) $needles as $needle) {
|
||
|
if ($needle !== '' && substr($haystack, 0, strlen($needle)) === (string) $needle) {
|
||
|
return true;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (!function_exists('str_ends_with')) {
|
||
|
/**
|
||
|
* Determine if a given string ends with a given substring.
|
||
|
*
|
||
|
* @param string $haystack
|
||
|
* @param string|array $needles
|
||
|
* @return bool
|
||
|
*/
|
||
|
function str_ends_with($haystack, $needles)
|
||
|
{
|
||
|
foreach ((array) $needles as $needle) {
|
||
|
if (substr($haystack, -strlen($needle)) === (string) $needle) {
|
||
|
return true;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (!function_exists('define_once')) {
|
||
|
/**
|
||
|
* Define a const if not exists.
|
||
|
*
|
||
|
* @param string $name
|
||
|
* @param mixed $value
|
||
|
* @return bool
|
||
|
*/
|
||
|
function define_once($name, $value = true)
|
||
|
{
|
||
|
return defined($name) or define($name, $value);
|
||
|
}
|
||
|
}
|