-
-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathConfig.php
More file actions
224 lines (192 loc) · 6.15 KB
/
Copy pathConfig.php
File metadata and controls
224 lines (192 loc) · 6.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
declare(strict_types=1);
namespace Peck;
use Closure;
use Peck\Support\PresetProvider;
use Peck\Support\ProjectPath;
final class Config
{
/**
* The name of the configuration file.
*/
private const JSON_CONFIGURATION_NAME = 'peck.json';
/**
* The default language passed to Aspell.
*/
private const DEFAULT_LANGUAGE = 'en_US';
/**
* The instance of the configuration.
*/
private static ?self $instance = null;
/**
* The closure to resolve the config file path.
*/
private static ?Closure $resolveConfigFilePathUsing = null;
/**
* Creates a new instance of Config.
*
* @param array<int, string> $whitelistedWords
* @param array<int, string> $whitelistedPaths
* @param array<string, array<int, string>> $fileSpecificIgnores
*/
public function __construct(
public array $whitelistedWords = [],
public array $whitelistedPaths = [],
public array $fileSpecificIgnores = [],
public ?string $preset = null,
public ?string $language = null,
) {
$this->whitelistedWords = array_map(strtolower(...), $whitelistedWords);
$this->fileSpecificIgnores = array_map(
fn (array $words): array => array_map(strtolower(...), $words),
$fileSpecificIgnores
);
}
/**
* Resolves the configuration file path.
*/
public static function resolveConfigFilePathUsing(Closure $closure): void
{
self::flush();
self::$resolveConfigFilePathUsing = $closure;
}
/**
* Flushes the configuration.
*/
public static function flush(): void
{
self::$instance = null;
self::$resolveConfigFilePathUsing = null;
}
/**
* Checks if the configuration file exists.
*/
public static function exists(): bool
{
return file_exists(ProjectPath::get().'/'.self::JSON_CONFIGURATION_NAME);
}
/**
* Fetches the instance of the configuration.
*/
public static function instance(): self
{
if (self::$instance instanceof self) {
return self::$instance;
}
$basePath = ProjectPath::get();
$filePath = $basePath.'/'.(self::$resolveConfigFilePathUsing instanceof Closure
? (self::$resolveConfigFilePathUsing)()
: self::JSON_CONFIGURATION_NAME);
$contents = file_exists($filePath)
? (string) file_get_contents($filePath)
: '{}';
/**
* @var array{
* preset?: string,
* language?: string,
* ignore?: array{
* words?: array<int, string>,
* paths?: array<int, string>,
* files?: array<string, array<int, string>>
* }
* } $jsonAsArray
*/
$jsonAsArray = json_decode($contents, true) ?: [];
return self::$instance = new self(
$jsonAsArray['ignore']['words'] ?? [],
$jsonAsArray['ignore']['paths'] ?? [],
$jsonAsArray['ignore']['files'] ?? [],
$jsonAsArray['preset'] ?? null,
$jsonAsArray['language'] ?? null,
);
}
/**
* Creates the configuration file for the user running the command.
*/
public static function init(): bool
{
$filePath = ProjectPath::get().'/'.self::JSON_CONFIGURATION_NAME;
if (file_exists($filePath)) {
return false;
}
return (bool) file_put_contents($filePath, json_encode([
...match (true) {
class_exists('\Illuminate\Support\Str') => [
'preset' => 'laravel',
],
default => [
'preset' => 'base',
],
},
'ignore' => [
'words' => [
'php',
],
'paths' => [],
],
], JSON_PRETTY_PRINT));
}
/**
* Adds a word to the ignore list.
*
* @param array<int, string> $words
*/
public function ignoreWords(array $words): void
{
$this->whitelistedWords = array_merge($this->whitelistedWords, array_map(strtolower(...), $words));
$this->persist();
}
/**
* Checks if the word is ignored globally or for a specific file.
*/
public function isWordIgnored(string $word, ?string $filePath = null): bool
{
$word = strtolower($word);
// Check global ignores
$globalIgnores = [
...$this->whitelistedWords,
...array_map(strtolower(...), PresetProvider::whitelistedWords($this->preset)),
];
if (in_array($word, $globalIgnores)) {
return true;
}
// Check file-specific ignores
if ($filePath !== null) {
$projectPath = ProjectPath::get();
// Normalize the file path to be relative to project root
$normalizedFilePath = $filePath;
if (str_starts_with($filePath, $projectPath.'/')) {
$normalizedFilePath = substr($filePath, strlen($projectPath) + 1);
}
foreach ($this->fileSpecificIgnores as $path => $words) {
if ($normalizedFilePath === $path && in_array($word, $words)) {
return true;
}
}
}
return false;
}
/**
* Retrieves the configured language or the default
*/
public function getLanguage(): string
{
return $this->language ?? self::DEFAULT_LANGUAGE;
}
/**
* Save the configuration to the file.
*/
private function persist(): void
{
$filePath = ProjectPath::get().'/'.self::JSON_CONFIGURATION_NAME;
file_put_contents($filePath, json_encode([
...$this->preset !== null ? ['preset' => $this->preset] : [],
...$this->language !== null ? ['language' => $this->language] : [],
'ignore' => [
'words' => $this->whitelistedWords,
'paths' => $this->whitelistedPaths,
'files' => $this->fileSpecificIgnores,
],
], JSON_PRETTY_PRINT));
}
}