-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.php
184 lines (168 loc) · 4.92 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
/*
* This file is part of the Discodian bot toolkit.
*
* (c) Daniël Klabbers <daniel@klabbers.email>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @see http://discodian.com
* @see https://github.com/discodian
*/
use Illuminate\Container\Container;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Psr\Log\LoggerInterface;
use Illuminate\Contracts\View\Factory as ViewFactory;
if (! function_exists('app')) {
/**
* Get the available container instance.
*
* @param string $abstract
* @param array $parameters
* @return mixed|\Illuminate\Foundation\Application
*/
function app($abstract = null, array $parameters = [])
{
if (is_null($abstract)) {
return Container::getInstance();
}
return Container::getInstance()->make($abstract, $parameters);
}
}
if (! function_exists('base_path')) {
/**
* Get the path to the base of the install.
*
* @param string $path
* @return string
*/
function base_path($path = '')
{
return app()->basePath().($path ? DIRECTORY_SEPARATOR.$path : $path);
}
}
if (! function_exists('cache')) {
/**
* Get / set the specified cache value.
*
* If an array is passed, we'll assume you want to put to the cache.
*
* @param dynamic key|key,default|data,expiration|null
* @return mixed|\Illuminate\Cache\CacheManager
*
* @throws \Exception
*/
function cache()
{
$arguments = func_get_args();
if (empty($arguments)) {
return app('cache');
}
if (is_string($arguments[0])) {
return app('cache')->get($arguments[0], $arguments[1] ?? null);
}
if (! is_array($arguments[0])) {
throw new Exception(
'When setting a value in the cache, you must pass an array of key / value pairs.'
);
}
if (! isset($arguments[1])) {
throw new Exception(
'You must specify an expiration time when setting a value in the cache.'
);
}
return app('cache')->put(key($arguments[0]), reset($arguments[0]), $arguments[1]);
}
}
if (! function_exists('config')) {
/**
* Get / set the specified configuration value.
*
* If an array is passed as the key, we will assume you want to set an array of values.
*
* @param array|string $key
* @param mixed $default
* @return mixed|\Illuminate\Config\Repository
*/
function config($key = null, $default = null)
{
if (is_null($key)) {
return app('config');
}
if (is_array($key)) {
return app('config')->set($key);
}
return app('config')->get($key, $default);
}
}
if (! function_exists('database_path')) {
/**
* Get the database path.
*
* @param string $path
* @return string
*/
function database_path($path = '')
{
return app()->basePath() . '/cache/' . $path;
}
}
if (! function_exists('logs')) {
function logs(...$args)
{
$logger = app(LoggerInterface::class);
if (! count($args)) {
return $logger;
}
$message = '';
$type = 'debug';
$data = [];
foreach ($args as $argument) {
if (is_int($argument)) {
$data[] = "Integer $argument";
} elseif (is_bool($argument)) {
$data[] = 'Boolean: ' . ($argument ? 'true' : 'false');
} elseif (is_array($argument)) {
$data = $argument;
} elseif (is_object($argument)) {
$data = (array) $argument;
} elseif (in_array($argument, ['debug', 'info', 'error'])) {
$type = $argument;
} elseif (is_string($argument) || count($args) === 1) {
$message = $argument;
}
}
$logger->{$type}($message, $data);
}
}
if (! function_exists('view')) {
/**
* Get the evaluated view contents for the given view.
*
* @param string $view
* @param array $data
* @param array $mergeData
* @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory
*/
function view($view = null, $data = [], $mergeData = [])
{
$factory = app(ViewFactory::class);
if (func_num_args() === 0) {
return $factory;
}
return $factory->file($view, $data, $mergeData);
}
}
if (! function_exists('path_injection')) {
function path_injection(string $path, array $properties = []): string
{
if (empty($properties)) {
return $path;
}
return preg_replace_callback('/:(?<property>[a-z_]+)/', function ($m) use ($properties) {
return Arr::get($properties, $m['property'], $m[0]);
}, $path);
}
}