Completed
Push — master ( 1f7d3f...49921e )
by ANTHONIUS
12s
created

Toolkit::getCachePathPrefix()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
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
    public static function getCachePathPrefix()
90
    {
91
        // using argv command to differ each dotfiles executable file
92
        global $argv;
93
        $command = $argv[0];
94
        $cachePath = sys_get_temp_dir().'/dotfiles/var/cache';
95
        if (false !== getenv('DOTFILES_REPO_DIR')) {
96
            $cachePath = getenv('DOTFILES_REPO_DIR').'/var/cache';
97
        }
98
99
        return $cachePath.DIRECTORY_SEPARATOR.crc32($command);
100
    }
101
102
    /**
103
     * @param string $path
104
     *
105
     * @return SplFileInfo
106
     */
107
    public static function getFileInfo(string $path)
108
    {
109
        $homeDir = getenv('HOME');
110
        $repoDir = getenv('REPO_DIR');
111
        $cwd = getcwd();
112
        if (false !== strpos($path, $homeDir)) {
113
            $relativePath = $homeDir;
114
        } elseif (false !== strpos($path, $repoDir)) {
115
            $relativePath = $repoDir;
116
        } elseif (false !== strpos($path, $cwd)) {
117
            $relativePath = $cwd;
118
        } else {
119
            $relativePath = dirname($path);
120
        }
121
122
        return new SplFileInfo($path, $relativePath, $relativePath.DIRECTORY_SEPARATOR.basename($path));
123
    }
124
125
    public static function loadDotEnv(): void
126
    {
127
        $cwd = static::getBaseDir();
128
        $files = array();
129
        if (is_file($file = $cwd.'/.env.dist')) {
130
            $files[] = $file;
131
        }
132
        if (is_file($file = $cwd.'/.env')) {
133
            $files[] = $file;
134
        }
135
136
        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...
137
            if (is_file($file = getenv('HOME').'/.dotfiles/.env')) {
138
                $files[] = $file;
139
            }
140
        }
141
142
        if (count($files) > 0) {
143
            $env = new Dotenv();
144
            call_user_func_array(array($env, 'load'), $files);
145
        }
146
    }
147
148
    public static function normalizeValue($value)
149
    {
150
        // replace environment variables
151
        $pattern = '/%%([A-Z]*)%%/i';
152
        preg_match($pattern, $value, $match);
153
        if (isset($match[1])) {
154
            $value = str_replace($match[0], getenv($match[1]), $value);
155
        }
156
157
        return $value;
158
    }
159
160
    public static function normalizeValues($values)
161
    {
162
        foreach ($values as $section => $contents) {
163
            foreach ($contents as $key => $value) {
164
                $values[$section][$key] = static::normalizeValue($value);
165
            }
166
        }
167
168
        return $values;
169
    }
170
171
    /**
172
     * @param string $path
173
     * @param array  $additionalPath
174
     *
175
     * @return string
176
     *
177
     * @deprecated Will be automatically strip by Output
178
     */
179
    public static function stripPath(string $path, $additionalPath = array())
180
    {
181
        $defaults = array(
182
            getenv('HOME') => '',
183
            getcwd() => '',
184
        );
185
        $path = strtr($path, array_merge($defaults, $additionalPath));
186
187
        return $path;
188
    }
189
}
190