DatabaseLoader::addNamespace()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
namespace MikeZange\LaravelDatabaseTranslation\Loaders;
4
5
use Illuminate\Cache\Repository as CacheRepository;
6
use Illuminate\Filesystem\Filesystem;
7
use Illuminate\Support\Collection;
8
use Illuminate\Translation\FileLoader;
9
use Illuminate\Translation\LoaderInterface;
10
use MikeZange\LaravelDatabaseTranslation\Models\Translation;
11
use MikeZange\LaravelDatabaseTranslation\Repositories\TranslationRepository;
12
13
/**
14
 * Class DatabaseLoader.
15
 */
16
class DatabaseLoader implements LoaderInterface
17
{
18
    /**
19
     * The filesystem instance.
20
     *
21
     * @var \Illuminate\Filesystem\Filesystem
22
     */
23
    protected $files;
24
25
    /**
26
     * The default path for the loader.
27
     *
28
     * @var string
29
     */
30
    protected $path;
31
32
    /**
33
     * The cache repository.
34
     *
35
     * @var CacheRepository
36
     */
37
    protected $cache;
38
39
    /**
40
     * Is cache enabled.
41
     *
42
     * @var bool
43
     */
44
    protected $cacheEnabled;
45
46
    /**
47
     * All of the namespace hints.
48
     *
49
     * @var array
50
     */
51
    protected $hints = [];
52
53
    /**
54
     * The cache repository.
55
     *
56
     * @var TranslationRepository
57
     */
58
    protected $translationRepository;
59
60
    /**
61
     * The cache repository.
62
     *
63
     * @var FileLoader
64
     */
65
    protected $laravelFileLoader;
66
67
    /**
68
     * DatabaseLoader constructor.
69
     *
70
     * @param Filesystem $filesystem
71
     * @param $path
72
     * @param TranslationRepository $translationRepository
73
     * @param CacheRepository       $cache
74
     */
75
    public function __construct(
76
        Filesystem $filesystem,
77
        $path,
78
        TranslationRepository $translationRepository,
79
        CacheRepository $cache
80
    ) {
81
        $this->files = $filesystem;
82
        $this->path = $path;
83
84
        $this->translationRepository = $translationRepository;
85
86
        $this->laravelFileLoader = new FileLoader($this->files, $this->path);
87
88
        $this->cache = $cache;
89
        $this->cacheEnabled = config('database.translations.cache_enabled');
90
    }
91
92
    /**
93
     * Load the messages for the given locale.
94
     *
95
     * @param string $locale
96
     * @param string $group
97
     * @param string $namespace
98
     *
99
     * @return array
100
     */
101
    public function load($locale, $group, $namespace = null) : array
102
    {
103
        $cacheKey = "translations.{$locale}.{$namespace}.{$group}";
104
105
        if (!$this->cacheEnabled) {
106
            $translations = $this->loadCombinedTranslations($locale, $group, $namespace);
107
        } else {
108
            $translations = $this->cache->remember($cacheKey, 1440, function () use ($locale, $namespace, $group) {
109
                return $this->loadCombinedTranslations($locale, $group, $namespace);
110
            });
111
        }
112
113
        return $translations;
114
    }
115
116
    /**
117
     * Get the messages for the given locale from the database and merge
118
     * them with the file based ones as a fallback for a given locale.
119
     *
120
     * @param string $locale
121
     * @param string $group
122
     * @param string $namespace
123
     *
124
     * @return array
125
     */
126
    protected function loadCombinedTranslations($locale, $group, $namespace) : array
127
    {
128
        return array_replace_recursive(
129
            $this->laravelFileLoader->load($locale, $group, $namespace),
130
            $this->loadFromDatabase($locale, $group, $namespace)
131
        );
132
    }
133
134
    /**
135
     * Add a new namespace to the loader.
136
     *
137
     * @param string $namespace
138
     * @param string $hint
139
     *
140
     * @return void
141
     */
142
    public function addNamespace($namespace, $hint)
143
    {
144
        $this->hints[$namespace] = $hint;
145
        $this->laravelFileLoader->addNamespace($namespace, $hint);
146
    }
147
148
    /**
149
     * Get an array of all the registered namespaces.
150
     *
151
     * @return array
152
     */
153
    public function namespaces() : array
154
    {
155
        return $this->hints;
156
    }
157
158
    /**
159
     * Load the required translations from the database.
160
     *
161
     * @param string $locale
162
     * @param string $group
163
     * @param string $namespace
164
     *
165
     * @return array
166
     */
167
    protected function loadFromDatabase($locale, $group, $namespace) : array
168
    {
169
        if (is_null($namespace) || $namespace == '*') {
170
            return $this->loadGroup($locale, $group);
171
        }
172
173
        return $this->loadNamespaced($locale, $namespace, $group);
174
    }
175
176
    /**
177
     * Load a non-namespaced translation group.
178
     *
179
     * @param string $locale
180
     * @param string $group
181
     *
182
     * @return array
183
     */
184
    protected function loadGroup($locale, $group) : array
185
    {
186
        $translations = $this->translationRepository->getItems(null, $group);
187
188
        return $this->createFormattedArray($translations, $locale);
189
    }
190
191
    /**
192
     * Load a namespaced translation group.
193
     *
194
     * @param string $locale
195
     * @param string $group
196
     * @param string $namespace
197
     *
198
     * @return array
199
     */
200
    protected function loadNamespaced($locale, $namespace, $group) : array
201
    {
202
        $translations = $this->translationRepository->getItems($namespace, $group);
203
204
        return $this->createFormattedArray($translations, $locale);
205
    }
206
207
    /**
208
     * Create a formatted array as if it was coming from the default loader.
209
     *
210
     * @param Collection $translations
211
     * @param string     $locale
212
     *
213
     * @return array
214
     */
215
    protected function createFormattedArray($translations, $locale) : array
216
    {
217
        $array = [];
218
219
        if ($translations) {
220
            foreach ($translations as $translation) {
221
                $value = $translation->getTranslation('values', $locale, true);
222
223
                if (!empty($value)) {
224
                    array_set($array, $translation->key, $value);
225
                }
226
            }
227
        }
228
229
        return $array;
230
    }
231
232
    /**
233
     * Get the Laravel File loader.
234
     *
235
     * @return FileLoader
236
     */
237
    public function getFileLoader() : FileLoader
238
    {
239
        return $this->laravelFileLoader;
240
    }
241
}
242