Completed
Push — feature/database-migrations ( b550b6 )
by Avtandil
02:27
created

helpers.php ➔ value()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 4
ccs 0
cts 1
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
use Dotenv\Environment\Adapter\EnvConstAdapter;
5
use Dotenv\Environment\Adapter\PutenvAdapter;
6
use Dotenv\Environment\Adapter\ServerConstAdapter;
7
use Dotenv\Environment\DotenvFactory;
8
use Illuminate\Container\Container;
9
use PhpOption\Option;
10
11
if (! function_exists('value')) {
12
    function value($value)
13
    {
14
        return $value instanceof Closure ? $value() : $value;
15
    }
16
}
17
18
if (! function_exists('env')) {
19
    function env($key, $default = null)
20
    {
21
        static $variables;
22
23
        if ($variables === null) {
24
            $variables = (new DotenvFactory([new EnvConstAdapter, new PutenvAdapter, new ServerConstAdapter]))->createImmutable();
25
        }
26
27
        return Option::fromValue($variables->get($key))
28
            ->map(function ($value) {
29
                switch (strtolower($value)) {
30
                    case 'true':
31
                    case '(true)':
32
                        return true;
33
                    case 'false':
34
                    case '(false)':
35
                        return false;
36
                    case 'empty':
37
                    case '(empty)':
38
                        return '';
39
                    case 'null':
40
                    case '(null)':
41
                        return;
42
                }
43
44
                if (preg_match('/\A([\'"])(.*)\1\z/', $value, $matches)) {
45
                    return $matches[2];
46
                }
47
48
                return $value;
49
            })
50
            ->getOrCall(function () use ($default) {
51
                return value($default);
52
            });
53
    }
54
}
55
56
if (! function_exists('app')) {
57
    function app($abstract = null, array $parameters = [])
58
    {
59
        if (is_null($abstract)) {
60
            return Container::getInstance();
61
        }
62
63
        return Container::getInstance()->make($abstract, $parameters);
64
    }
65
}
66