forked from laravel/lumen-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.php
424 lines (382 loc) · 10.1 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
<?php
use Illuminate\Container\Container;
use Illuminate\Contracts\Broadcasting\Factory as BroadcastFactory;
use Illuminate\Contracts\Bus\Dispatcher;
use Illuminate\Contracts\Debug\ExceptionHandler;
use Laravel\Lumen\Bus\PendingDispatch;
use Symfony\Component\Debug\Exception\FatalThrowableError;
if (! function_exists('abort')) {
/**
* Throw an HttpException with the given data.
*
* @param int $code
* @param string $message
* @param array $headers
* @return void
*
* @throws \Symfony\Component\HttpKernel\Exception\HttpException
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
*/
function abort($code, $message = '', array $headers = [])
{
return app()->abort($code, $message, $headers);
}
}
if (! function_exists('app')) {
/**
* Get the available container instance.
*
* @param string|null $make
* @param array $parameters
* @return mixed|\Laravel\Lumen\Application
*/
function app($make = null, array $parameters = [])
{
if (is_null($make)) {
return Container::getInstance();
}
return Container::getInstance()->make($make, $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 ? '/'.$path : $path);
}
}
if (! function_exists('broadcast')) {
/**
* Begin broadcasting an event.
*
* @param mixed|null $event
* @return \Illuminate\Broadcasting\PendingBroadcast
*/
function broadcast($event = null)
{
return app(BroadcastFactory::class)->event($event);
}
}
if (! function_exists('decrypt')) {
/**
* Decrypt the given value.
*
* @param string $value
* @return string
*/
function decrypt($value)
{
return app('encrypter')->decrypt($value);
}
}
if (! function_exists('dispatch')) {
/**
* Dispatch a job to its appropriate handler.
*
* @param mixed $job
* @return mixed
*/
function dispatch($job)
{
return new PendingDispatch($job);
}
}
if (! function_exists('dispatch_now')) {
/**
* Dispatch a command to its appropriate handler in the current process.
*
* @param mixed $job
* @param mixed $handler
* @return mixed
*/
function dispatch_now($job, $handler = null)
{
return app(Dispatcher::class)->dispatchNow($job, $handler);
}
}
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|null $key
* @param mixed $default
* @return mixed
*/
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 path to the database directory of the install.
*
* @param string $path
* @return string
*/
function database_path($path = '')
{
return app()->databasePath($path);
}
}
if (! function_exists('encrypt')) {
/**
* Encrypt the given value.
*
* @param string $value
* @return string
*/
function encrypt($value)
{
return app('encrypter')->encrypt($value);
}
}
if (! function_exists('event')) {
/**
* Dispatch an event and call the listeners.
*
* @param object|string $event
* @param mixed $payload
* @param bool $halt
* @return array|null
*/
function event($event, $payload = [], $halt = false)
{
return app('events')->dispatch($event, $payload, $halt);
}
}
if (! function_exists('factory')) {
/**
* Create a model factory builder for a given class, name, and amount.
*
* @param dynamic class|class,name|class,amount|class,name,amount
* @return \Illuminate\Database\Eloquent\FactoryBuilder
*/
function factory()
{
app('db');
$factory = app('Illuminate\Database\Eloquent\Factory');
$arguments = func_get_args();
if (isset($arguments[1]) && is_string($arguments[1])) {
return $factory->of($arguments[0], $arguments[1])->times($arguments[2] ?? null);
} elseif (isset($arguments[1])) {
return $factory->of($arguments[0])->times($arguments[1]);
} else {
return $factory->of($arguments[0]);
}
}
}
if (! function_exists('info')) {
/**
* Write some information to the log.
*
* @param string $message
* @param array $context
* @return void
*/
function info($message, $context = [])
{
return app('Psr\Log\LoggerInterface')->info($message, $context);
}
}
if (! function_exists('redirect')) {
/**
* Get an instance of the redirector.
*
* @param string|null $to
* @param int $status
* @param array $headers
* @param bool|null $secure
* @return \Laravel\Lumen\Http\Redirector|\Illuminate\Http\RedirectResponse
*/
function redirect($to = null, $status = 302, $headers = [], $secure = null)
{
$redirector = new Laravel\Lumen\Http\Redirector(app());
if (is_null($to)) {
return $redirector;
}
return $redirector->to($to, $status, $headers, $secure);
}
}
if (! function_exists('report')) {
/**
* Report an exception.
*
* @param \Throwable $exception
* @return void
*/
function report($exception)
{
if ($exception instanceof Throwable &&
! $exception instanceof Exception) {
$exception = new FatalThrowableError($exception);
}
app(ExceptionHandler::class)->report($exception);
}
}
if (! function_exists('resource_path')) {
/**
* Get the path to the resources folder.
*
* @param string $path
* @return string
*/
function resource_path($path = '')
{
return app()->resourcePath($path);
}
}
if (! function_exists('response')) {
/**
* Return a new response from the application.
*
* @param string $content
* @param int $status
* @param array $headers
* @return \Illuminate\Http\Response|\Laravel\Lumen\Http\ResponseFactory
*/
function response($content = '', $status = 200, array $headers = [])
{
$factory = new Laravel\Lumen\Http\ResponseFactory;
if (func_num_args() === 0) {
return $factory;
}
return $factory->make($content, $status, $headers);
}
}
if (! function_exists('route')) {
/**
* Generate a URL to a named route.
*
* @param string $name
* @param array $parameters
* @param bool|null $secure
* @return string
*/
function route($name, $parameters = [], $secure = null)
{
return app('url')->route($name, $parameters, $secure);
}
}
if (! function_exists('storage_path')) {
/**
* Get the path to the storage folder.
*
* @param string $path
* @return string
*/
function storage_path($path = '')
{
return app()->storagePath($path);
}
}
if (! function_exists('trans')) {
/**
* Translate the given message.
*
* @param string|null $id
* @param array $replace
* @param string|null $locale
* @return \Illuminate\Contracts\Translation\Translator|string|array|null
*/
function trans($id = null, $replace = [], $locale = null)
{
if (is_null($id)) {
return app('translator');
}
return app('translator')->get($id, $replace, $locale);
}
}
if (! function_exists('__')) {
/**
* Translate the given message.
*
* @param string $key
* @param array $replace
* @param string|null $locale
* @return string|array|null
*/
function __($key, $replace = [], $locale = null)
{
return app('translator')->get($key, $replace, $locale);
}
}
if (! function_exists('trans_choice')) {
/**
* Translates the given message based on a count.
*
* @param string $id
* @param int|array|\Countable $number
* @param array $replace
* @param string|null $locale
* @return string
*/
function trans_choice($id, $number, array $replace = [], $locale = null)
{
return app('translator')->choice($id, $number, $replace, $locale);
}
}
if (! function_exists('url')) {
/**
* Generate a url for the application.
*
* @param string $path
* @param mixed $parameters
* @param bool|null $secure
* @return string
*/
function url($path = null, $parameters = [], $secure = null)
{
return app('url')->to($path, $parameters, $secure);
}
}
if (! function_exists('validator')) {
/**
* Create a new Validator instance.
*
* @param array $data
* @param array $rules
* @param array $messages
* @param array $customAttributes
* @return \Illuminate\Contracts\Validation\Validator
*/
function validator(array $data = [], array $rules = [], array $messages = [], array $customAttributes = [])
{
$factory = app('validator');
if (func_num_args() === 0) {
return $factory;
}
return $factory->make($data, $rules, $messages, $customAttributes);
}
}
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
*/
function view($view = null, $data = [], $mergeData = [])
{
$factory = app('view');
if (func_num_args() === 0) {
return $factory;
}
return $factory->make($view, $data, $mergeData);
}
}