Passed
Pull Request — master (#1)
by ANTHONIUS
02:43
created

Toolkit::getFileInfo()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.2
c 0
b 0
f 0
cc 4
eloc 12
nc 4
nop 1
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 = realpath(dirname(__DIR__.'/../../../src'));
82
        $baseDir = getcwd();
83
        if (false !== strpos($dir = \Phar::running(), 'phar:///')) {
84
            $baseDir = str_replace('/dotfiles.phar', '', \Phar::running(false));
85
        }
86
87
        return $baseDir;
88
    }
89
90
    /**
91
     * @param string $path
92
     *
93
     * @return SplFileInfo
94
     */
95
    public static function getFileInfo(string $path)
96
    {
97
        $homeDir = getenv('HOME');
98
        $repoDir = getenv('REPO_DIR');
99
        $cwd = getcwd();
100
        if (false !== strpos($path, $homeDir)) {
101
            $relativePath = $homeDir;
102
        } elseif (false !== strpos($path, $repoDir)) {
103
            $relativePath = $repoDir;
104
        } elseif (false !== strpos($path, $cwd)) {
105
            $relativePath = $cwd;
106
        } else {
107
            $relativePath = dirname($path);
108
        }
109
110
        return new SplFileInfo($path, $relativePath, $relativePath.DIRECTORY_SEPARATOR.basename($path));
111
    }
112
113
    public static function loadDotEnv(): void
114
    {
115
        $cwd = static::getBaseDir();
116
        $files = array();
117
        if (is_file($file = $cwd.'/.env.dist')) {
118
            $files[] = $file;
119
        }
120
        if (is_file($file = $cwd.'/.env')) {
121
            $files[] = $file;
122
        }
123
        if (is_file($file = getenv('HOME').'/.dotfiles/.env')) {
124
            $files[] = $file;
125
        }
126
127
        if (count($files) > 0) {
128
            $env = new Dotenv();
129
            call_user_func_array(array($env, 'load'), $files);
130
        }
131
    }
132
133
    public static function normalizeValue($value)
134
    {
135
        // replace environment variables
136
        $pattern = '/%%([A-Z]*)%%/i';
137
        preg_match($pattern, $value, $match);
138
        if (isset($match[1])) {
139
            $value = str_replace($match[0], getenv($match[1]), $value);
140
        }
141
142
        return $value;
143
    }
144
145
    public static function normalizeValues($values)
146
    {
147
        foreach ($values as $section => $contents) {
148
            foreach ($contents as $key => $value) {
149
                $values[$section][$key] = static::normalizeValue($value);
150
            }
151
        }
152
153
        return $values;
154
    }
155
156
    public static function stripPath(string $path, $additionalPath = array())
157
    {
158
        $defaults = array(
159
            getenv('HOME') => '',
160
            getcwd() => '',
161
        );
162
        $path = strtr($path, array_merge($defaults, $additionalPath));
163
164
        return $path;
165
    }
166
}
167