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; |
||
0 ignored issues
–
show
|
|||
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 | if ($this->isJson($path)) { |
|
50 | PrettyFile::make( |
||
51 | 30 | json_encode($data, JSON_PRETTY_PRINT ^ JSON_UNESCAPED_UNICODE) |
|
52 | 18 | )->storeRaw($path); |
|
53 | 18 | ||
54 | 18 | return; |
|
55 | } |
||
56 | 18 | ||
57 | $service = Formatter::make(); |
||
58 | $service->setKeyAsString(); |
||
59 | 12 | $service->setCase(Config::getCase()); |
|
60 | 12 | ||
61 | 12 | if (Config::isAlignment()) { |
|
62 | $service->setEqualsAlign(); |
||
63 | 12 | } |
|
64 | 12 | ||
65 | PrettyFile::make( |
||
66 | $service->raw($data) |
||
67 | 12 | )->store($path); |
|
68 | 12 | } |
|
69 | 12 | ||
70 | 12 | public function directoryExist(string $path, string $locale): void |
|
71 | { |
||
72 | 24 | if (! $path || ! $this->exists($path)) { |
|
73 | throw new SourceLocaleDirectoryDoesntExist($locale); |
||
74 | 24 | } |
|
75 | 12 | } |
|
76 | |||
77 | 12 | public function fileExist(string $path, string $locale): void |
|
78 | { |
||
79 | 30 | if (! $path || ! $this->exists($path)) { |
|
80 | throw new SourceLocaleFileDoesntExist($locale); |
||
81 | 30 | } |
|
82 | 12 | } |
|
83 | |||
84 | 18 | public function exists(string $path): bool |
|
85 | { |
||
86 | 30 | return file_exists($path); |
|
87 | } |
||
88 | 30 | ||
89 | public function name(string $path): string |
||
90 | { |
||
91 | 30 | return pathinfo($path, PATHINFO_FILENAME); |
|
92 | } |
||
93 | 30 | ||
94 | public function isJson(string $path): bool |
||
95 | { |
||
96 | 30 | $extension = pathinfo($path, PATHINFO_EXTENSION); |
|
97 | |||
98 | 30 | return Str::lower($extension) === 'json'; |
|
99 | } |
||
100 | } |
||
101 |
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: