1
|
|
|
<?php namespace Modules\Translation\Repositories\File; |
2
|
|
|
|
3
|
|
|
use Illuminate\Filesystem\Filesystem; |
4
|
|
|
use Illuminate\Translation\LoaderInterface; |
5
|
|
|
use Modules\Translation\Repositories\FileTranslationRepository as FileTranslationRepositoryInterface; |
6
|
|
|
|
7
|
|
|
class FileTranslationRepository implements FileTranslationRepositoryInterface |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var Filesystem |
11
|
|
|
*/ |
12
|
|
|
private $finder; |
13
|
|
|
/** |
14
|
|
|
* @var LoaderInterface |
15
|
|
|
*/ |
16
|
|
|
private $loader; |
17
|
|
|
|
18
|
|
|
public function __construct(Filesystem $finder, LoaderInterface $loader) |
19
|
|
|
{ |
20
|
|
|
$this->finder = $finder; |
21
|
|
|
$this->loader = $loader; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Get all the translations for all modules on disk |
26
|
|
|
* @return array |
27
|
|
|
*/ |
28
|
|
|
public function all() |
29
|
|
|
{ |
30
|
|
|
$files = $this->getTranslationFilenamesFromPaths($this->loader->paths()); |
|
|
|
|
31
|
|
|
|
32
|
|
|
$translations = []; |
33
|
|
|
|
34
|
|
|
foreach ($files as $locale => $files) { |
35
|
|
|
foreach ($files as $namespace => $file) { |
36
|
|
|
$trans = $this->finder->getRequire($file); |
37
|
|
|
$trans = array_dot($trans); |
38
|
|
|
|
39
|
|
|
foreach ($trans as $key => $value) { |
40
|
|
|
$translations[$locale]["{$namespace}.{$key}"] = $value; |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return $translations; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Get all of the names of the Translations files from an array of Paths. |
50
|
|
|
* Returns [ 'translationkeyprefix' => 'filepath' ] |
51
|
|
|
* @param array $paths |
52
|
|
|
* @return array |
53
|
|
|
*/ |
54
|
|
|
protected function getTranslationFilenamesFromPaths(array $paths) |
55
|
|
|
{ |
56
|
|
|
$files = []; |
57
|
|
|
$locales = config('laravellocalization.supportedLocales'); |
58
|
|
|
|
59
|
|
|
foreach ($paths as $hint => $path) { |
60
|
|
|
foreach ($locales as $locale => $language) { |
61
|
|
|
foreach ($this->finder->glob("{$path}/{$locale}/*.php") as $file) { |
62
|
|
|
$category = str_replace(["$path/", ".php", "{$locale}/"], "", $file); |
63
|
|
|
$category = str_replace("/", ".", $category); |
64
|
|
|
$category = !is_int($hint) ? "{$hint}::{$category}" : $category; |
65
|
|
|
|
66
|
|
|
$files[$locale][$category] = $file; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $files; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: