Completed
Pull Request — master (#9)
by ANTHONIUS
02:53
created

Toolkit::doFlattenArray()   B

Complexity

Conditions 7
Paths 14

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 8.2222
c 0
b 0
f 0
cc 7
eloc 10
nc 14
nop 3
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\Finder\SplFileInfo;
17
18
class Toolkit
19
{
20
    public static function ensureDir(string $dir): void
21
    {
22
        if (!is_dir($dir)) {
23
            mkdir($dir, 0755, true);
24
        }
25
    }
26
27
    public static function ensureDotPath(string $relativePathName)
28
    {
29
        if (0 !== strpos($relativePathName, '.')) {
30
            $relativePathName = '.'.$relativePathName;
31
        }
32
33
        return $relativePathName;
34
    }
35
36
    /**
37
     * Ensure that directory exists.
38
     *
39
     * @param string $file
40
     */
41
    public static function ensureFileDir($file): void
42
    {
43
        static::ensureDir(dirname($file));
44
    }
45
46
    public static function flattenArray(array &$values, $prefix = null): void
47
    {
48
        if (null !== $prefix) {
49
            $values = array($prefix => $values);
50
        }
51
        static::doFlattenArray($values);
52
    }
53
54
    public static function getBaseDir()
55
    {
56
        $baseDir = getcwd();
57
        if (getenv('DOTFILES_PHAR_MODE')) {
58
            $baseDir = str_replace(__FILE__, '', \Phar::running(false));
59
        }
60
61
        return $baseDir;
62
    }
63
64
    public static function getCachePathPrefix()
65
    {
66
        // using argv command to differ each dotfiles executable file
67
        global $argv;
68
        $command = $argv[0];
69
        $cacheDir = getenv('DOTFILES_CACHE_DIR');
70
71
        return $cacheDir.DIRECTORY_SEPARATOR.crc32($command);
72
    }
73
74
    /**
75
     * @param string $path
76
     *
77
     * @return SplFileInfo
78
     */
79
    public static function getFileInfo(string $path)
80
    {
81
        $homeDir = getenv('HOME');
82
        $repoDir = getenv('REPO_DIR');
83
        $cwd = getcwd();
84
        if (false !== strpos($path, $homeDir)) {
85
            $relativePath = $homeDir;
86
        } elseif (false !== strpos($path, $repoDir)) {
87
            $relativePath = $repoDir;
88
        } elseif (false !== strpos($path, $cwd)) {
89
            $relativePath = $cwd;
90
        } else {
91
            $relativePath = dirname($path);
92
        }
93
94
        return new SplFileInfo($path, $relativePath, $relativePath.DIRECTORY_SEPARATOR.basename($path));
95
    }
96
97
    public static function getRelativePath(string $file): string
98
    {
99
        $homeDir = getenv('DOTFILES_HOME_DIR');
100
        $backupDir = getenv('DOTFILES_BACKUP_DIR');
101
102
        if (false !== strpos($file, $homeDir)) {
103
            return str_replace($homeDir.DIRECTORY_SEPARATOR, '', $file);
104
        }
105
106
        if (false !== strpos($file, $backupDir)) {
107
            return str_replace(dirname($backupDir).'/', '', $file);
108
        }
109
110
        return $file;
111
    }
112
113
    public static function normalizeValue($value)
114
    {
115
        // replace environment variables
116
        $pattern = '/%%([A-Z]*)%%/i';
117
        preg_match($pattern, $value, $match);
118
        if (isset($match[1])) {
119
            $value = str_replace($match[0], getenv($match[1]), $value);
120
        }
121
122
        return $value;
123
    }
124
125
    public static function normalizeValues($values)
126
    {
127
        foreach ($values as $section => $contents) {
128
            foreach ($contents as $key => $value) {
129
                $values[$section][$key] = static::normalizeValue($value);
130
            }
131
        }
132
133
        return $values;
134
    }
135
136
    /**
137
     * @param string $path
138
     * @param array  $additionalPath
139
     *
140
     * @return string
141
     *
142
     * @deprecated Will be automatically strip by Output
143
     */
144
    public static function stripPath(string $path, $additionalPath = array())
145
    {
146
        $defaults = array(
147
            getenv('HOME') => '',
148
            getcwd() => '',
149
        );
150
        $path = strtr($path, array_merge($defaults, $additionalPath));
151
152
        return $path;
153
    }
154
155
    /**
156
     * Flattens an nested array of translations.
157
     *
158
     * The scheme used is:
159
     *   'key' => array('key2' => array('key3' => 'value'))
160
     * Becomes:
161
     *   'key.key2.key3' => 'value'
162
     *
163
     * This function takes an array by reference and will modify it
164
     *
165
     * @param array  &$values The array that will be flattened
166
     * @param array  $subnode Current subnode being parsed, used internally for recursive calls
167
     * @param string $path    Current path being parsed, used internally for recursive calls
168
     */
169
    private static function doFlattenArray(array &$values, array $subnode = null, $path = null): void
170
    {
171
        if (null === $subnode) {
172
            $subnode = &$values;
173
        }
174
        foreach ($subnode as $key => $value) {
175
            if (is_array($value)) {
176
                $nodePath = $path ? $path.'.'.$key : $key;
177
                static::doFlattenArray($values, $value, $nodePath);
178
                if (null === $path) {
179
                    unset($values[$key]);
180
                }
181
            } elseif (null !== $path) {
182
                $values[$path.'.'.$key] = $value;
183
            }
184
        }
185
    }
186
}
187