1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ReliqArts\DirectTranslator\Vocabulary; |
6
|
|
|
|
7
|
|
|
use DomainException; |
8
|
|
|
use Exception; |
|
|
|
|
9
|
|
|
use ReliqArts\DirectTranslator\ConfigProvider; |
10
|
|
|
use ReliqArts\DirectTranslator\Vocabulary as VocabularyContract; |
11
|
|
|
use ReliqArts\DirectTranslator\Vocabulary\Exception\LoadingFailed; |
12
|
|
|
use ReliqArts\DirectTranslator\VocabularyLoader; |
13
|
|
|
|
14
|
|
|
final class Loader implements VocabularyLoader |
15
|
|
|
{ |
16
|
|
|
private const VOCAB_FILE_EXTENSION = 'json'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var ConfigProvider |
20
|
|
|
*/ |
21
|
|
|
private $configProvider; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var Reader |
25
|
|
|
*/ |
26
|
|
|
private $reader; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var Builder |
30
|
|
|
*/ |
31
|
|
|
private $builder; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* VocabularyLoader constructor. |
35
|
|
|
* |
36
|
|
|
* @param ConfigProvider $configProvider |
37
|
|
|
* @param Reader $reader |
38
|
|
|
* @param Builder $builder |
39
|
|
|
*/ |
40
|
|
|
public function __construct( |
41
|
|
|
ConfigProvider $configProvider, |
42
|
|
|
Reader $reader, |
43
|
|
|
Builder $builder |
44
|
|
|
) { |
45
|
|
|
$this->configProvider = $configProvider; |
46
|
|
|
$this->reader = $reader; |
47
|
|
|
$this->builder = $builder; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param string $key |
52
|
|
|
* |
53
|
|
|
* @throws LoadingFailed |
54
|
|
|
* |
55
|
|
|
* @return VocabularyContract |
56
|
|
|
*/ |
57
|
|
|
public function load(string $key): VocabularyContract |
58
|
|
|
{ |
59
|
|
|
try { |
60
|
|
|
$filePath = $this->getVocabularyFilePath($key); |
61
|
|
|
|
62
|
|
|
return $this->builder->create($this->reader->read($filePath)); |
63
|
|
|
} catch (Exception $exception) { |
64
|
|
|
throw new LoadingFailed( |
65
|
|
|
sprintf('Could not load vocabulary by key: `%s`', $key), |
66
|
|
|
$exception->getCode(), |
67
|
|
|
$exception |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param string $key |
74
|
|
|
* |
75
|
|
|
* @throws DomainException |
76
|
|
|
* |
77
|
|
|
* @return null|string |
78
|
|
|
*/ |
79
|
|
|
private function getVocabularyFilePath(string $key): ?string |
80
|
|
|
{ |
81
|
|
|
$vocabularyDirectories = $this->configProvider->getVocabularyDirectories(); |
82
|
|
|
|
83
|
|
|
foreach ($vocabularyDirectories as $directory) { |
84
|
|
|
$path = sprintf('%s/%s.%s', $directory, $key, self::VOCAB_FILE_EXTENSION); |
85
|
|
|
|
86
|
|
|
if ($vocabularyPath = realpath($path)) { |
87
|
|
|
return $vocabularyPath; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
throw new DomainException( |
92
|
|
|
sprintf('Vocabulary file not found for key: `%s`.', $key) |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
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: