|
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
|
|
|
class Toolkit |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Ensure that directory exists. |
|
20
|
|
|
* |
|
21
|
|
|
* @param string $dir |
|
22
|
|
|
*/ |
|
23
|
|
|
public static function ensureDir(string $dir): void |
|
24
|
|
|
{ |
|
25
|
|
|
if (!is_dir($dir)) { |
|
26
|
|
|
mkdir($dir, 0755, true); |
|
27
|
|
|
} |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public static function ensureDotPath(string $relativePathName) |
|
31
|
|
|
{ |
|
32
|
|
|
if (0 !== strpos($relativePathName, '.')) { |
|
33
|
|
|
$relativePathName = '.'.$relativePathName; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
return $relativePathName; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Ensure that directory for file exists. |
|
41
|
|
|
* |
|
42
|
|
|
* @param string $file |
|
43
|
|
|
*/ |
|
44
|
|
|
public static function ensureFileDir($file): void |
|
45
|
|
|
{ |
|
46
|
|
|
static::ensureDir(dirname($file)); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public static function flattenArray(array &$values, $prefix = null): void |
|
50
|
|
|
{ |
|
51
|
|
|
if (null !== $prefix) { |
|
52
|
|
|
$values = array($prefix => $values); |
|
53
|
|
|
} |
|
54
|
|
|
static::doFlattenArray($values); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @return mixed|string |
|
59
|
|
|
* @codeCoverageIgnore Can't tests phar mode |
|
60
|
|
|
*/ |
|
61
|
|
|
public static function getBaseDir() |
|
62
|
|
|
{ |
|
63
|
|
|
$baseDir = getcwd(); |
|
64
|
|
|
if (getenv('DOTFILES_PHAR_MODE')) { |
|
65
|
|
|
$baseDir = str_replace(__FILE__, '', \Phar::running(false)); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return $baseDir; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public static function getRelativePath(string $file): string |
|
72
|
|
|
{ |
|
73
|
|
|
$homeDir = getenv('DOTFILES_HOME_DIR'); |
|
74
|
|
|
$backupDir = getenv('DOTFILES_BACKUP_DIR'); |
|
75
|
|
|
|
|
76
|
|
|
if (false !== strpos($file, $homeDir)) { |
|
77
|
|
|
return str_replace($homeDir.DIRECTORY_SEPARATOR, '', $file); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
if (false !== strpos($file, $backupDir)) { |
|
81
|
|
|
return str_replace(dirname($backupDir).'/', '', $file); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return $file; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Flattens an nested array of translations. |
|
89
|
|
|
* |
|
90
|
|
|
* The scheme used is: |
|
91
|
|
|
* 'key' => array('key2' => array('key3' => 'value')) |
|
92
|
|
|
* Becomes: |
|
93
|
|
|
* 'key.key2.key3' => 'value' |
|
94
|
|
|
* |
|
95
|
|
|
* This function takes an array by reference and will modify it |
|
96
|
|
|
* |
|
97
|
|
|
* @param array &$values The array that will be flattened |
|
98
|
|
|
* @param array $subnode Current subnode being parsed, used internally for recursive calls |
|
99
|
|
|
* @param string $path Current path being parsed, used internally for recursive calls |
|
100
|
|
|
*/ |
|
101
|
|
|
private static function doFlattenArray(array &$values, array $subnode = null, $path = null): void |
|
102
|
|
|
{ |
|
103
|
|
|
if (null === $subnode) { |
|
104
|
|
|
$subnode = &$values; |
|
105
|
|
|
} |
|
106
|
|
|
foreach ($subnode as $key => $value) { |
|
107
|
|
|
if (is_array($value)) { |
|
108
|
|
|
$nodePath = $path ? $path.'.'.$key : $key; |
|
109
|
|
|
static::doFlattenArray($values, $value, $nodePath); |
|
110
|
|
|
if (null === $path) { |
|
111
|
|
|
unset($values[$key]); |
|
112
|
|
|
} |
|
113
|
|
|
} elseif (null !== $path) { |
|
114
|
|
|
$values[$path.'.'.$key] = $value; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|