-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDictionary.php
More file actions
177 lines (150 loc) · 4.91 KB
/
Copy pathDictionary.php
File metadata and controls
177 lines (150 loc) · 4.91 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
<?php
declare(strict_types=1);
/*
* UserFrosting Framework (http://www.userfrosting.com)
*
* @link https://github.com/userfrosting/framework
* @copyright Copyright (c) 2013-2024 Alexander Weissman, Louis Charette, Jordan Mele
* @license https://github.com/userfrosting/framework/blob/master/LICENSE.md (MIT License)
*/
namespace UserFrosting\I18n;
use Illuminate\Support\Arr;
use LogicException;
use UserFrosting\Support\Repository\Loader\ArrayFileLoader;
use UserFrosting\Support\Repository\Loader\FileRepositoryLoader;
use UserFrosting\Support\Repository\Repository;
use UserFrosting\UniformResourceLocator\ResourceLocatorInterface;
/**
* Load all locale all "Key => translation" data matrix.
*/
class Dictionary extends Repository implements DictionaryInterface
{
/**
* @var string Base URI for locator
*/
protected string $uri = 'locale://';
/**
* @var FileRepositoryLoader
*/
protected FileRepositoryLoader $fileLoader;
/**
* @var mixed[] Locale "Key => translation" data matrix cache
*/
protected $items = [];
/**
* @param LocaleInterface $locale
* @param ResourceLocatorInterface $locator
* @param FileRepositoryLoader|null $fileLoader File loader used to load each dictionary files (default to Array Loader)
*/
public function __construct(
protected LocaleInterface $locale,
protected ResourceLocatorInterface $locator,
?FileRepositoryLoader $fileLoader = null
) {
$this->fileLoader = $fileLoader ?? new ArrayFileLoader();
parent::__construct();
}
/**
* {@inheritdoc}
*/
public function getDictionary(): array
{
if (count($this->items) === 0) {
$this->items = $this->loadDictionary();
}
return $this->items;
}
/**
* {@inheritdoc}
*/
public function getFlattenDictionary(): array
{
return Arr::dot($this->getDictionary());
}
/**
* Set the locator base URI (default 'locale://').
*
* @param string $uri
*/
public function setUri(string $uri): void
{
$this->uri = $uri;
}
/**
* {@inheritdoc}
*/
public function getLocale(): LocaleInterface
{
return $this->locale;
}
/**
* Return the file repository loader used to load.
*
* @return FileRepositoryLoader
*/
public function getFileLoader(): FileRepositoryLoader
{
return $this->fileLoader;
}
/**
* Load the dictionary from file.
*
* @return mixed[] The locale dictionary
*/
protected function loadDictionary(): array
{
$dictionary = [];
// List of loaded locales
$loadedLocale = [$this->locale->getIdentifier()];
// Get list of files to load
$files = $this->getFiles();
$files = $this->filterDictionaryFiles($files);
$files = array_map('strval', $files);
// Load all files content if files are present
if (count($files) !== 0) {
$loader = $this->getFileLoader();
$loader->setPaths($files);
$dictionary = $loader->load();
}
// Now load dependent dictionaries
foreach ($this->locale->getDependentLocales() as $locale) {
// Stop if locale already loaded to prevent recursion
$localesToLoad = array_merge([$locale->getIdentifier()], $locale->getDependentLocalesIdentifier());
$intersection = array_intersect($localesToLoad, $loadedLocale);
if (count($intersection) !== 0) {
throw new LogicException("Can't load dictionary. Dependencies recursion detected : " . implode(', ', $intersection));
}
$dependentDictionary = new self($locale, $this->locator, $this->fileLoader);
$dictionary = array_replace_recursive($dependentDictionary->getDictionary(), $dictionary);
$loadedLocale[] = $locale->getIdentifier();
}
return $dictionary;
}
/**
* Remove config files from locator results and convert ResourceInterface to path/string.
*
* @param \UserFrosting\UniformResourceLocator\ResourceInterface[] $files
*
* @return string[]
*/
protected function filterDictionaryFiles(array $files): array
{
// @phpstan-ignore-next-line False positive. ResourceInterface is Stringable.
return array_filter($files, function ($file) {
if ($file->getExtension() == 'php') {
return (string) $file;
}
});
}
/**
* List all files for a given locale using the locator.
*
* @return \UserFrosting\UniformResourceLocator\ResourceInterface[]
*/
protected function getFiles(): array
{
$resources = $this->locator->listResources($this->uri . $this->locale->getIdentifier(), true, false);
$resources = array_reverse($resources);
return $resources;
}
}