Passed
Push — 0.7.0 ( 01cd5f...881fd9 )
by Alexander
03:57
created

isCli()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 1
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 3
rs 10
1
<?php 
2
3
/**
4
 * Lenevor Framework
5
 *
6
 * LICENSE
7
 *
8
 * This source file is subject to the new BSD license that is bundled
9
 * with this package in the file license.md.
10
 * It is also available through the world-wide-web at this URL:
11
 * https://lenevor.com/license
12
 * If you did not receive a copy of the license and are unable to
13
 * obtain it through the world-wide-web, please send an email
14
 * to [email protected] so we can send you a copy immediately.
15
 *
16
 * @package     Lenevor
17
 * @subpackage  Base
18
 * @link        https://lenevor.com
19
 * @copyright   Copyright (c) 2019 - 2021 Alexander Campo <[email protected]>
20
 * @license     https://opensource.org/licenses/BSD-3-Clause New BSD license or see https://lenevor.com/license or see /license.md
21
 */
22
23
use Syscodes\Core\Application;
24
use Syscodes\Support\WebString;
25
use Syscodes\Routing\UrlGenerator;
26
use Syscodes\Support\Facades\Date;
27
use Syscodes\Contracts\View\Factory;
28
use Syscodes\Contracts\Routing\RouteResponse;
29
30
if ( ! function_exists('abort')) 
31
{
32
    /**
33
     * Throw an HttpException with the given data.
34
     *
35
     * @param  int  $code
36
     * @param  string  $message
37
     * @param  array  $headers
38
     * 
39
     * @return void
40
     *
41
     * @throws \Syscodes\Core\Http\Exceptions\HttpException
42
     * @throws \Syscodes\Core\Http\Exceptions\LenevorException
43
     */
44
    function abort($code, $message = '', array $headers = [])
45
    {
46
        return app()->abort($code, $message, $headers);
47
    }
48
}
49
50
if ( ! function_exists('app')) 
51
{
52
    /**
53
     * Get the available Application instance.
54
     *
55
     * @param  string  $id  (null by default)
56
     * @param  array  $parameters
57
     * 
58
     * @return mixed|\Syscodes\Contracts\Core\Application
59
     */
60
    function app($id = null, array $parameters = [])
61
    {
62
        if (is_null($id)) {
63
            return Application::getInstance();
64
        }
65
66
        return Application::getInstance()->make($id, $parameters);
67
    }
68
}
69
70
if ( ! function_exists('asset')) 
71
{
72
    /**
73
     * Generate an asset path for the application.
74
     * 
75
     * @param  string  $path
76
     * @param  bool  $secure  (null by default)
77
     * 
78
     * @return string
79
     */
80
    function asset($path, $secure = null)
81
    {
82
        return app('url')->asset($path, $secure);
83
    }
84
}
85
86
if ( ! function_exists('back')) 
87
{
88
    /**
89
     * Create a new redirect response to the previous location.
90
     * 
91
     * @param  int  $status    (302 by default)
92
     * @param  array  $headers
93
     * @param  mixed  $fallback  (false by default)
94
     * 
95
     * @return \Syscodes\Http\RedirectResponse
96
     */
97
    function back($status = 302, $headers = [], $fallback = false)
98
    {
99
        return app('redirect')->back($status, $headers, $fallback);
100
    }
101
}
102
103
if ( ! function_exists('basePath')) 
104
{
105
    /**
106
     * Get the path to the base of the install.
107
     *
108
     * @param  string  $path
109
     * 
110
     * @return string
111
     */
112
    function basePath($path = '')
113
    {
114
        return app()->basePath($path);
115
    }
116
}
117
118
if ( ! function_exists('cache'))
119
{
120
    /**
121
     * Get / set the specified cache value.
122
     *
123
     * If an array is passed, we'll assume you want to put to the cache.
124
     *
125
     * @param  dynamic  key|key,default|data,expiration|null
0 ignored issues
show
Bug introduced by
The type key was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
126
     * 
127
     * @return mixed|\Syscodes\Cache\CacheManager
128
     *
129
     * @throws \Exception
130
     */
131
    function cache()
132
    {
133
        $arguments = func_get_args();
134
        
135
        if (empty($arguments)) {
136
            return app('cache');
137
        }
138
        
139
        if (is_string($arguments[0])) {
140
            return app('cache')->get(...$arguments);
141
        }
142
        
143
        if ( ! is_array($arguments[0])) {
144
            throw new Exception('When setting a value in the cache, you must pass an array of key / value pairs.');
145
        }
146
        
147
        if ( ! isset($arguments[1])) {
148
            throw new Exception('You must specify an expiration time when setting a value in the cache.');
149
        }
150
        
151
        return app('cache')->put(key($arguments[0]), reset($arguments[0]), $arguments[1]);
152
    }
153
}
154
155
if ( ! function_exists('config'))
156
{
157
    /**
158
     * Get / set the specified configuration value.
159
     * If an array is passed as the key, we will assume you want to set 
160
     * an array of values.
161
     *
162
     * @param   array|string  $key  (null by default)
163
     * @param   mixed  $default  (null by default)
164
     *
165
     * @return  mixed|\Syscodes\Config\Configure
166
     */
167
    function config($key = null, $value = null)
168
    {
169
        if ($key === null) {
170
            return app('config');
171
        }
172
173
        if (is_array($key)) {
174
            return app('config')->set($key, $value);
175
        }
176
        
177
        return app('config')->get($key, $value);
178
    }
179
}
180
181
if ( ! function_exists('csrfField'))
182
{
183
    /**
184
     * Generate a CSRF token form field.
185
     * 
186
     * @return string
187
     */
188
    function csrfField()
189
    {
190
        return new WebString('<input type="hidden" name="_token" value="'.csrfToken().'">');
191
    }
192
}
193
194
if ( ! function_exists('csrfToken'))
195
{
196
    /**
197
     * Get the CSRF token value.
198
     * 
199
     * @return string
200
     * 
201
     * @throws \RuntimeException
202
     */
203
    function csrfToken()
204
    {
205
        $session = app('session');
206
        
207
        if (isset($session)) {
208
            return $session->token();
209
        }
210
211
        throw new RuntimeException('Application session store not set.');
212
    }
213
}
214
215
if ( ! function_exists('databasePath'))
216
{
217
    /**
218
     * Get the path to the database directory.
219
     * 
220
     * @param  string  $path
221
     * 
222
     * @return string
223
     */
224
    function databasePath($path = '')
225
    {
226
        return app()->databasePath($path);
227
    }
228
}
229
230
if ( ! function_exists('decrypt'))
231
{
232
    /**
233
     * Decrypt the given value.
234
     * 
235
     * @param  mixed  $value
236
     * @param  bool  $unserialize  (true by default)
237
     * 
238
     * @return string
239
     */
240
    function decrypt($value, $unserialize = true)
241
    {
242
        return app('encrypter')->decrypt($value, $unserialize);
243
    }
244
}
245
246
if ( ! function_exists('e'))
247
{
248
    /**
249
     * Escape HTML entities in a string.
250
     *
251
     * @param  string  $value
252
     *
253
     * @return string
254
     */
255
    function e($value)
256
    {
257
        return htmlentities($value, ENT_QUOTES, 'UTF-8', false);
258
    }
259
}
260
261
if ( ! function_exists('encrypt'))
262
{
263
    /**
264
     * Encrypt the given value.
265
     * 
266
     * @param  mixed  $value
267
     * @param  bool  $serialize  (true by default)
268
     * 
269
     * @return string
270
     */
271
    function encrypt($value, $serialize = true)
272
    {
273
        return app('encrypter')->encrypt($value, $serialize);
274
    }
275
}
276
277
if ( ! function_exists('getClass'))
278
{
279
    /**
280
     * Function to crop the full name of the namespace and leave 
281
     * only the name of the class.
282
     * 
283
     * @param  string  $classname
284
     * @param  bool  $bool  (false by default)
285
     * 
286
     * @return array
287
     */
288
    function getClass($classname, $bool = false)
289
    {
290
        $position = explode('\\', get_class($classname));
0 ignored issues
show
Bug introduced by
$classname of type string is incompatible with the type object expected by parameter $object of get_class(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

290
        $position = explode('\\', get_class(/** @scrutinizer ignore-type */ $classname));
Loading history...
291
        
292
        return ! $bool ? array_pop($position) : get_class($classname);
0 ignored issues
show
Bug Best Practice introduced by
The expression return ! $bool ? array_p...: get_class($classname) also could return the type string which is incompatible with the documented return type array.
Loading history...
293
    }
294
}
295
296
if ( ! function_exists('isCli'))
297
{
298
    /**
299
     * Determines if this request was made from the command line (CLI).
300
     * 
301
     * @return bool
302
     */
303
    function isCli()
304
    {
305
        return (PHP_SAPI === 'cli' || defined('STDIN'));
306
    }
307
}
308
309
310
if ( ! function_exists('isGetCommonPath'))
311
{
312
    /**
313
     * Find the common "root" path of two given paths or FQFN's.
314
     * 
315
     * @param  array  $paths  Array with the paths to compare
316
     * 
317
     * @return string  The determined common path section
318
     */
319
    function isGetCommonPath($paths)
320
    {
321
        $lastOffset = 1;
322
        $common     = '/';
323
        
324
        while (($index = strpos($paths[0], '/', $lastOffset)) !== false) {
325
            $dirLen = $index - $lastOffset + 1; // include
326
            $dir = substr($paths[0], $lastOffset, $dirLen);
327
            
328
            foreach ($paths as $path) {
329
                if (substr($path, $lastOffset, $dirLen) != $dir) {
330
                    return $common;
331
                }
332
            }
333
            
334
            $common    .= $dir;
335
            $lastOffset = $index + 1;
336
        }
337
        
338
        return $common;
339
    }
340
}
341
342
if ( ! function_exists('isImport'))
343
{
344
    /**
345
     * Loads in a core class and optionally an app class override if it exists.
346
     * 
347
     * @param  string  $path
348
     * @param  string  $folder
349
     * 
350
     * @return void
351
     */
352
    function isImport($path, $folder = 'classes')
353
    {
354
        $path = str_replace('/', DIRECTORY_SEPARATOR, $path);
355
        
356
        // load it ffrom the core if it exists
357
        if (is_file(SYS_PATH.$folder.DIRECTORY_SEPARATOR.$path.'.php')) {
358
            require_once SYS_PATH.$folder.DIRECTORY_SEPARATOR.$path.'.php';
359
        }
360
        
361
        // if the app has an override (or a non-core file), load that too
362
        if (is_file(APP_PATH.$folder.DIRECTORY_SEPARATOR.$path.'.php')) {
363
            require_once APP_PATH.$folder.DIRECTORY_SEPARATOR.$path.'.php';
364
        }
365
    }
366
}
367
368
if ( ! function_exists('now'))
369
{
370
    /**
371
     * Create a new Chronos class instance for the current time.
372
     * 
373
     * @param  \DateTimeZone|string|null  $timezone
374
     * 
375
     * @return \Syscodes\Support\Chronos
376
     */
377
    function now($timezone = null)
378
    {
379
        return Date::now($timezone);
0 ignored issues
show
Bug introduced by
It seems like $timezone can also be of type DateTimeZone; however, parameter $timezone of Syscodes\Support\Facades\Date::now() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

379
        return Date::now(/** @scrutinizer ignore-type */ $timezone);
Loading history...
380
    }
381
}
382
383
if ( ! function_exists('redirect'))
384
{
385
    /**
386
     * Get an instance of the redirect.
387
     *
388
     * @param  string|null  $url  The url  (null by default)
389
     * @param  int   $code  The redirect status code  (302 by default)
390
     * @param  array  $headers  An array of headers
391
     * @param  bool|null  $secure  Type of protocol (http|https)  (null by default)
392
     *
393
     * @return \Syscodes\Routing\Redirector
394
     */
395
    function redirect($url = null, $code = 302, $headers = [], $secure = null)
396
    {
397
        if (null === $url) {
398
            return app('redirect');
399
        }
400
        
401
        return app('redirect')->to($url, $code, $headers, $secure);
402
    }
403
}
404
405
if ( ! function_exists('request'))
406
{
407
    /**
408
     * Get an instance of the current request or an input item from the request.
409
     * 
410
     * @param  array|string|null  $key  (null by default)
411
     * @param  mixed  $default  (null by default)
412
     * 
413
     * @return \Syscodes\Http\Request|string|array 
414
     */
415
    function request($key = null, $default = null)
416
    {
417
        if (null === $key) {
418
            return app('request');
419
        }
420
421
        $value = app('request')->__get($key);
422
423
        return null === $value ? value($default) : $value;
424
    }
425
}
426
427
if ( ! function_exists('response')) 
428
{
429
    /**
430
     * Return a new Response from the application.
431
     *
432
     * @param  string  $body
433
     * @param  int  $status  (200 by default)
434
     * @param  array  $headers
435
     * 
436
     * @return \Syscodes\Http\Response|\Syscodes\Routing\RouteResponse
437
     */
438
    function response($body = '', $status = 200, array $headers = [])
439
    {
440
        $response = app(RouteResponse::class);
441
442
        if (func_num_args() === 0) {
443
            return $response;
444
        }
445
446
        return $response->make($body, $status, $headers);
447
    }
448
}
449
450
if ( ! function_exists('resourcePath')) 
451
{
452
    /**
453
     * Get the path to the resources folder.
454
     *
455
     * @param  string  $path
456
     * 
457
     * @return string
458
     */
459
    function resourcePath($path = '')
460
    {
461
        return app()->resourcePath($path);
462
    }
463
}
464
465
if ( ! function_exists('route'))
466
{
467
    /**
468
     * Get the URL to a named route.
469
     * 
470
     * @param  string  $name
471
     * @param  array  $parameters
472
     * @param  bool  $forced  (true by default)
473
     * @param  \Syscodes\Routing\Route|null  $route  (null by default)
474
     * 
475
     * @return string
476
     */
477
    function route($name, $parameters = [], $forced = true, $route = null)
478
    {
479
        return app('url')->route($name, $parameters, $forced, $route);
480
    }
481
}
482
483
if ( ! function_exists('secureAsset'))
484
{
485
    /**
486
     * Generate an asset path for the application.
487
     * 
488
     * @param  string  $path
489
     * 
490
     * @return string
491
     */
492
    function secureAsset($path)
493
    {
494
        return asset($path, true);
495
    }
496
}
497
498
if ( ! function_exists('secureUrl'))
499
{
500
    /**
501
     * Generate a HTTPS URL for the application.
502
     * 
503
     * @param  string  $path
504
     * @param  array  $parameters
505
     * 
506
     * @return string
507
     */
508
    function secureUrl($path, $parameters = [])
509
    {
510
        return url($path, $parameters, true);
511
    }
512
}
513
514
if ( ! function_exists('session'))
515
{
516
    /**
517
     * Get / set the specified session value.
518
     * 
519
     * @param  string  $key  (null by default)
520
     * @param  mixed  $default  (null by default)
521
     * 
522
     * @return mixed|\Syscodes\Session\Store|\Syscodes\Session\SessionManager
523
     */
524
    function session($key = null, $default = null)
525
    {
526
        if (is_null($key)) {
527
            return app('session');
528
        }
529
530
        if (is_array($key)) {
0 ignored issues
show
introduced by
The condition is_array($key) is always false.
Loading history...
531
            return app('session')->put($key, $default);
532
        }
533
534
        return app('session')->get($key, $default);
535
    }
536
}
537
538
if ( ! function_exists('segment'))
539
{
540
  /**
541
     * Returns the desired segment, or $default if it does not exist.
542
     *
543
     * @param  int  $segment  
544
     * @param  mixed  $default  (null by default)
545
     *
546
     * @return string
547
     */
548
    function segment($index, $default = null)
549
    {
550
        return request()->segment($index, $default);
551
    }
552
}
553
554
if ( ! function_exists('segments'))
555
{
556
  /**
557
     * Returns all segments in an array.
558
     *
559
     * @return array
560
     */
561
    function segments()
562
    {
563
        return request()->segments();
564
    }
565
}
566
567
if ( ! function_exists('storagePath')) 
568
{
569
    /**
570
     * Get the path to the storage folder.
571
     *
572
     * @param  string  $path
573
     * 
574
     * @return string
575
     */
576
    function storagePath($path = '')
577
    {
578
        return app('path.storage').($path ? DIRECTORY_SEPARATOR.$path : $path);
0 ignored issues
show
Bug introduced by
Are you sure app('path.storage') of type object can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

578
        return /** @scrutinizer ignore-type */ app('path.storage').($path ? DIRECTORY_SEPARATOR.$path : $path);
Loading history...
579
    }
580
}
581
582
if ( ! function_exists('totalSegments'))
583
{
584
  /**
585
     * Returns the total number of segment.
586
     *
587
     * @return int
588
     */
589
    function totalSegments()
590
    {
591
        return request()->totalSegments();
592
    }
593
}
594
595
if ( ! function_exists('__'))
596
{
597
    /**
598
     * A convenience method to translate a string and format it
599
     * with the intl extension's MessageFormatter object.
600
     * 
601
     * @param  string  $line
602
     * @param  array  $args
603
     * 
604
     * @return string
605
     */
606
    function __($line, array $args = [])
607
    {
608
        return app('translator')->getLine($line, $args);
609
    }
610
}
611
612
if ( ! function_exists('url'))
613
{
614
    /**
615
     * Generate a URL for the application.
616
     *
617
     * @param  string|null  $path  (null by default)
618
     * @param  array  $parameters
619
     * @param  bool|null  $secure  (null by default)
620
     *
621
     * @return string
622
     */
623
    function url($path = null, $parameters = [], $secure = null)
624
    {
625
        if (is_null($path)) {
626
            return app(UrlGenerator::class);
0 ignored issues
show
Bug Best Practice introduced by
The expression return app(Syscodes\Routing\UrlGenerator::class) returns the type object which is incompatible with the documented return type string.
Loading history...
627
        }
628
629
        return app(UrlGenerator::class)->to($path, $parameters, $secure);
630
    }
631
}
632
633
if ( ! function_exists('view'))
634
{
635
    /**
636
     * Returns a new View object. If you do not define the "file" parameter, 
637
     * you must call [$this->view].
638
     *
639
     * @example $view->make($file, $data);
640
     *  
641
     * @param  string|null  $file  View filename
642
     * @param  array  $data  Array of values
643
     * 
644
     * @return \Syscodes\View\View|\Syscodes\Contracts\View\Factory
645
     */
646
    function view($file = null, $data = [])
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
647
    {
648
        $view = app(Factory::class);
649
650
        if (func_num_args() === 0) {
651
            return $view;
652
        }
653
654
        return $view->make($file, $data);
655
    }
656
}