1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Helldar\LaravelLangPublisher\Support; |
4
|
|
|
|
5
|
|
|
use DirectoryIterator; |
6
|
|
|
use Helldar\LaravelLangPublisher\Exceptions\SourceLocaleDirectoryDoesntExist; |
7
|
|
|
use Helldar\LaravelLangPublisher\Exceptions\SourceLocaleFileDoesntExist; |
8
|
|
|
use Helldar\LaravelLangPublisher\Facades\Config; |
|
|
|
|
9
|
|
|
use Helldar\PrettyArray\Services\File as PrettyFile; |
10
|
|
|
use Helldar\PrettyArray\Services\Formatter; |
11
|
|
|
use Helldar\Support\Facades\Str; |
12
|
|
|
|
13
|
|
|
final class File |
14
|
|
|
{ |
15
|
12 |
|
public function files(string $path): DirectoryIterator |
16
|
|
|
{ |
17
|
12 |
|
return new DirectoryIterator($path); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param string $path |
22
|
|
|
* @param bool $return_empty |
23
|
|
|
* |
24
|
|
|
* @throws \Helldar\PrettyArray\Exceptions\FileDoesntExistsException |
25
|
|
|
* |
26
|
|
|
* @return array |
27
|
|
|
*/ |
28
|
30 |
|
public function load(string $path, bool $return_empty = false): array |
29
|
|
|
{ |
30
|
30 |
|
if ($return_empty && ! $this->exists($path)) { |
31
|
12 |
|
return []; |
32
|
|
|
} |
33
|
|
|
|
34
|
30 |
|
$pretty = PrettyFile::make(); |
35
|
|
|
|
36
|
30 |
|
return $this->isJson($path) |
37
|
18 |
|
? json_decode($pretty->loadRaw($path), true) |
38
|
30 |
|
: $pretty->load($path); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param string $path |
43
|
|
|
* @param array $data |
44
|
|
|
* |
45
|
|
|
* @throws \Helldar\PrettyArray\Exceptions\UnknownCaseTypeException |
46
|
|
|
*/ |
47
|
30 |
|
public function save(string $path, array $data): void |
48
|
|
|
{ |
49
|
30 |
|
ksort($data); |
50
|
|
|
|
51
|
30 |
|
if ($this->isJson($path)) { |
52
|
18 |
|
PrettyFile::make( |
53
|
18 |
|
json_encode($data, JSON_PRETTY_PRINT) |
54
|
18 |
|
)->storeRaw($path); |
55
|
|
|
|
56
|
18 |
|
return; |
57
|
|
|
} |
58
|
|
|
|
59
|
12 |
|
$service = Formatter::make(); |
60
|
12 |
|
$service->setKeyAsString(); |
61
|
12 |
|
$service->setCase(Config::getCase()); |
62
|
|
|
|
63
|
12 |
|
if (Config::isAlignment()) { |
64
|
12 |
|
$service->setEqualsAlign(); |
65
|
|
|
} |
66
|
|
|
|
67
|
12 |
|
PrettyFile::make( |
68
|
12 |
|
$service->raw($data) |
69
|
12 |
|
)->store($path); |
70
|
12 |
|
} |
71
|
|
|
|
72
|
36 |
|
public function directoryExist(string $path, string $locale): void |
73
|
|
|
{ |
74
|
36 |
|
if (! $path || ! $this->exists($path)) { |
75
|
12 |
|
throw new SourceLocaleDirectoryDoesntExist($locale); |
76
|
|
|
} |
77
|
24 |
|
} |
78
|
|
|
|
79
|
42 |
|
public function fileExist(string $path, string $locale): void |
80
|
|
|
{ |
81
|
42 |
|
if (! $path || ! $this->exists($path)) { |
82
|
12 |
|
throw new SourceLocaleFileDoesntExist($locale); |
83
|
|
|
} |
84
|
30 |
|
} |
85
|
|
|
|
86
|
54 |
|
public function exists(string $path): bool |
87
|
|
|
{ |
88
|
54 |
|
return file_exists($path); |
89
|
|
|
} |
90
|
|
|
|
91
|
30 |
|
public function name(string $path): string |
92
|
|
|
{ |
93
|
30 |
|
return pathinfo($path, PATHINFO_FILENAME); |
94
|
|
|
} |
95
|
|
|
|
96
|
30 |
|
public function isJson(string $path): bool |
97
|
|
|
{ |
98
|
30 |
|
$extension = pathinfo($path, PATHINFO_EXTENSION); |
99
|
|
|
|
100
|
30 |
|
return Str::lower($extension) === 'json'; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: