url()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
rs 10
1
<?php
2
/*
3
 * This file is part of the Scrawler package.
4
 *
5
 * (c) Pranjal Pandey <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
declare(strict_types=1);
12
13
if (!function_exists('app')) {
14
    /**
15
     * Get the app instance.
16
     */
17
    function app(): Scrawler\App
18
    {
19
        return Scrawler\App::engine();
20
    }
21
}
22
23
if (!function_exists('config')) {
24
    /**
25
     * Get the config instance.
26
     */
27
    function config(): PHLAK\Config\Config
28
    {
29
        return Scrawler\App::engine()->config();
30
    }
31
}
32
33
if (!function_exists('url')) {
34
    /**
35
     * Generate a url.
36
     */
37
    function url(string $path = ''): string
38
    {
39
        if (Scrawler\App::engine()->config()->has('https') && Scrawler\App::engine()->config()->get('https')) {
40
            return 'https://'.Scrawler\App::engine()->request()->getHttpHost().Scrawler\App::engine()->request()->getBasePath().$path;
41
        }
42
43
        return Scrawler\App::engine()->request()->getSchemeAndHttpHost().Scrawler\App::engine()->request()->getBasePath().$path;
44
    }
45
}
46
47
if (!function_exists('env')) {
48
    /**
49
     * Get the value of an environment variable.
50
     *
51
     * @return mixed|null
52
     */
53
    function env(string $key): mixed
54
    {
55
        if (isset($_ENV[$key])) {
56
            return $_ENV[$key];
57
        }
58
        if (getenv($key)) {
59
            return getenv($key);
60
        }
61
62
        if (request()->server->has($key)) {
63
            return request()->server->get($key);
64
        }
65
66
        return null;
67
    }
68
}
69