blessing-skin-server/app/helpers.php

85 lines
2.0 KiB
PHP
Raw Normal View History

<?php
2019-07-02 22:22:05 +08:00
declare(strict_types=1);
use Illuminate\Support\Arr;
2016-09-14 22:44:30 +08:00
2019-12-14 11:10:37 +08:00
if (!function_exists('plugin')) {
2019-08-13 18:42:17 +08:00
function plugin(string $name)
2017-01-08 16:05:54 +08:00
{
2019-08-13 18:42:17 +08:00
return app('plugins')->get($name);
2017-01-08 16:05:54 +08:00
}
}
2019-12-14 11:10:37 +08:00
if (!function_exists('plugin_assets')) {
2019-08-15 17:08:25 +08:00
function plugin_assets(string $name, string $relativeUri): string
2016-12-31 16:07:12 +08:00
{
2019-08-15 17:08:25 +08:00
$plugin = plugin($name);
if ($plugin) {
2017-01-08 16:05:54 +08:00
return $plugin->assets($relativeUri);
2016-12-31 16:07:12 +08:00
} else {
throw new InvalidArgumentException('No such plugin.');
2016-12-31 16:07:12 +08:00
}
}
}
2019-12-14 11:10:37 +08:00
if (!function_exists('json')) {
2016-09-10 21:39:45 +08:00
function json()
{
$args = func_get_args();
2019-09-10 19:52:17 +08:00
if (count($args) === 1 && is_array($args[0])) {
return response()->json($args[0]);
} elseif (count($args) === 3 && is_array($args[2])) {
2018-02-16 17:31:04 +08:00
// The third argument is array of extra fields
2019-09-10 19:52:17 +08:00
return response()->json([
2019-04-23 11:47:45 +08:00
'code' => $args[1],
'message' => $args[0],
2019-04-23 19:14:41 +08:00
'data' => $args[2],
]);
} else {
2019-09-10 19:52:17 +08:00
return response()->json([
2019-04-23 11:47:45 +08:00
'code' => Arr::get($args, 1, 1),
'message' => $args[0],
2016-09-10 21:39:45 +08:00
]);
}
}
}
2019-12-14 11:10:37 +08:00
if (!function_exists('option')) {
/**
* Get / set the specified option value.
*
* If an array is passed as the key, we will assume you want to set an array of values.
*
2019-12-14 11:10:37 +08:00
* @param array|string $key
* @param mixed $default
2019-12-21 15:50:29 +08:00
* @param bool $raw return raw value without convertion
2019-12-14 11:10:37 +08:00
*
* @return mixed
*/
function option($key = null, $default = null, $raw = false)
{
$options = app('options');
if (is_null($key)) {
return $options;
}
if (is_array($key)) {
2019-03-23 15:44:16 +08:00
$options->set($key);
2019-04-19 19:36:36 +08:00
return;
}
return $options->get($key, $default, $raw);
}
}
2016-09-27 22:35:04 +08:00
2019-12-14 11:10:37 +08:00
if (!function_exists('option_localized')) {
2018-07-06 14:46:25 +08:00
function option_localized($key = null, $default = null, $raw = false)
{
return option($key.'_'.config('app.locale'), option($key));
}
}