Completed
Push — master ( 01982b...3c7394 )
by ANTHONIUS
11s
created

Toolkit::loadDotEnv()   B

Complexity

Conditions 6
Paths 24

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 12
nc 24
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the dotfiles project.
7
 *
8
 *     (c) Anthonius Munthi <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Dotfiles\Core\Util;
15
16
use Symfony\Component\Dotenv\Dotenv;
17
use Symfony\Component\Finder\SplFileInfo;
18
19
class Toolkit
20
{
21
    public static function ensureDir(string $dir): void
22
    {
23
        if (!is_dir($dir)) {
24
            mkdir($dir, 0755, true);
25
        }
26
    }
27
28
    public static function ensureDotPath(string $relativePathName)
29
    {
30
        if (0 !== strpos($relativePathName, '.')) {
31
            $relativePathName = '.'.$relativePathName;
32
        }
33
34
        return $relativePathName;
35
    }
36
37
    /**
38
     * Ensure that directory exists.
39
     *
40
     * @param string $file
41
     */
42
    public static function ensureFileDir($file): void
43
    {
44
        static::ensureDir(dirname($file));
45
    }
46
47
    /**
48
     * Flattens an nested array of translations.
49
     *
50
     * The scheme used is:
51
     *   'key' => array('key2' => array('key3' => 'value'))
52
     * Becomes:
53
     *   'key.key2.key3' => 'value'
54
     *
55
     * This function takes an array by reference and will modify it
56
     *
57
     * @param array  &$values The array that will be flattened
58
     * @param array  $subnode Current subnode being parsed, used internally for recursive calls
59
     * @param string $path    Current path being parsed, used internally for recursive calls
60
     */
61
    public static function flattenArray(array &$values, array $subnode = null, $path = null): void
62
    {
63
        if (null === $subnode) {
64
            $subnode = &$values;
65
        }
66
        foreach ($subnode as $key => $value) {
67
            if (is_array($value)) {
68
                $nodePath = $path ? $path.'.'.$key : $key;
69
                static::flattenArray($values, $value, $nodePath);
70
                if (null === $path) {
71
                    unset($values[$key]);
72
                }
73
            } elseif (null !== $path) {
74
                $values[$path.'.'.$key] = $value;
75
            }
76
        }
77
    }
78
79
    public static function getBaseDir()
80
    {
81
        $baseDir = getcwd();
82
        if (DOTFILES_PHAR_MODE) {
0 ignored issues
show
Bug introduced by
The constant Dotfiles\Core\Util\DOTFILES_PHAR_MODE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
83
            $baseDir = str_replace(__FILE__, '', \Phar::running(false));
84
        }
85
86
        return $baseDir;
87
    }
88
89
    /**
90
     * @param string $path
91
     *
92
     * @return SplFileInfo
93
     */
94
    public static function getFileInfo(string $path)
95
    {
96
        $homeDir = getenv('HOME');
97
        $repoDir = getenv('REPO_DIR');
98
        $cwd = getcwd();
99
        if (false !== strpos($path, $homeDir)) {
100
            $relativePath = $homeDir;
101
        } elseif (false !== strpos($path, $repoDir)) {
102
            $relativePath = $repoDir;
103
        } elseif (false !== strpos($path, $cwd)) {
104
            $relativePath = $cwd;
105
        } else {
106
            $relativePath = dirname($path);
107
        }
108
109
        return new SplFileInfo($path, $relativePath, $relativePath.DIRECTORY_SEPARATOR.basename($path));
110
    }
111
112
    public static function loadDotEnv(): void
113
    {
114
        $cwd = static::getBaseDir();
115
        $files = array();
116
        if (is_file($file = $cwd.'/.env.dist')) {
117
            $files[] = $file;
118
        }
119
        if (is_file($file = $cwd.'/.env')) {
120
            $files[] = $file;
121
        }
122
123
        if (DOTFILES_PHAR_MODE) {
0 ignored issues
show
Bug introduced by
The constant Dotfiles\Core\Util\DOTFILES_PHAR_MODE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
124
            if (is_file($file = getenv('HOME').'/.dotfiles/.env')) {
125
                $files[] = $file;
126
            }
127
        }
128
129
        if (count($files) > 0) {
130
            $env = new Dotenv();
131
            call_user_func_array(array($env, 'load'), $files);
132
        }
133
    }
134
135
    public static function normalizeValue($value)
136
    {
137
        // replace environment variables
138
        $pattern = '/%%([A-Z]*)%%/i';
139
        preg_match($pattern, $value, $match);
140
        if (isset($match[1])) {
141
            $value = str_replace($match[0], getenv($match[1]), $value);
142
        }
143
144
        return $value;
145
    }
146
147
    public static function normalizeValues($values)
148
    {
149
        foreach ($values as $section => $contents) {
150
            foreach ($contents as $key => $value) {
151
                $values[$section][$key] = static::normalizeValue($value);
152
            }
153
        }
154
155
        return $values;
156
    }
157
158
    public static function stripPath(string $path, $additionalPath = array())
159
    {
160
        $defaults = array(
161
            getenv('HOME') => '',
162
            getcwd() => '',
163
        );
164
        $path = strtr($path, array_merge($defaults, $additionalPath));
165
166
        return $path;
167
    }
168
}
169