Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Translation;
4
 
5
use Illuminate\Contracts\Translation\Loader;
6
use Illuminate\Filesystem\Filesystem;
7
use RuntimeException;
8
 
9
class FileLoader implements Loader
10
{
11
    /**
12
     * The filesystem instance.
13
     *
14
     * @var \Illuminate\Filesystem\Filesystem
15
     */
16
    protected $files;
17
 
18
    /**
19
     * The default path for the loader.
20
     *
21
     * @var string
22
     */
23
    protected $path;
24
 
25
    /**
26
     * All of the registered paths to JSON translation files.
27
     *
28
     * @var array
29
     */
30
    protected $jsonPaths = [];
31
 
32
    /**
33
     * All of the namespace hints.
34
     *
35
     * @var array
36
     */
37
    protected $hints = [];
38
 
39
    /**
40
     * Create a new file loader instance.
41
     *
42
     * @param  \Illuminate\Filesystem\Filesystem  $files
43
     * @param  string  $path
44
     * @return void
45
     */
46
    public function __construct(Filesystem $files, $path)
47
    {
48
        $this->path = $path;
49
        $this->files = $files;
50
    }
51
 
52
    /**
53
     * Load the messages for the given locale.
54
     *
55
     * @param  string  $locale
56
     * @param  string  $group
57
     * @param  string|null  $namespace
58
     * @return array
59
     */
60
    public function load($locale, $group, $namespace = null)
61
    {
62
        if ($group === '*' && $namespace === '*') {
63
            return $this->loadJsonPaths($locale);
64
        }
65
 
66
        if (is_null($namespace) || $namespace === '*') {
67
            return $this->loadPath($this->path, $locale, $group);
68
        }
69
 
70
        return $this->loadNamespaced($locale, $group, $namespace);
71
    }
72
 
73
    /**
74
     * Load a namespaced translation group.
75
     *
76
     * @param  string  $locale
77
     * @param  string  $group
78
     * @param  string  $namespace
79
     * @return array
80
     */
81
    protected function loadNamespaced($locale, $group, $namespace)
82
    {
83
        if (isset($this->hints[$namespace])) {
84
            $lines = $this->loadPath($this->hints[$namespace], $locale, $group);
85
 
86
            return $this->loadNamespaceOverrides($lines, $locale, $group, $namespace);
87
        }
88
 
89
        return [];
90
    }
91
 
92
    /**
93
     * Load a local namespaced translation group for overrides.
94
     *
95
     * @param  array  $lines
96
     * @param  string  $locale
97
     * @param  string  $group
98
     * @param  string  $namespace
99
     * @return array
100
     */
101
    protected function loadNamespaceOverrides(array $lines, $locale, $group, $namespace)
102
    {
103
        $file = "{$this->path}/vendor/{$namespace}/{$locale}/{$group}.php";
104
 
105
        if ($this->files->exists($file)) {
106
            return array_replace_recursive($lines, $this->files->getRequire($file));
107
        }
108
 
109
        return $lines;
110
    }
111
 
112
    /**
113
     * Load a locale from a given path.
114
     *
115
     * @param  string  $path
116
     * @param  string  $locale
117
     * @param  string  $group
118
     * @return array
119
     */
120
    protected function loadPath($path, $locale, $group)
121
    {
122
        if ($this->files->exists($full = "{$path}/{$locale}/{$group}.php")) {
123
            return $this->files->getRequire($full);
124
        }
125
 
126
        return [];
127
    }
128
 
129
    /**
130
     * Load a locale from the given JSON file path.
131
     *
132
     * @param  string  $locale
133
     * @return array
134
     *
135
     * @throws \RuntimeException
136
     */
137
    protected function loadJsonPaths($locale)
138
    {
139
        return collect(array_merge($this->jsonPaths, [$this->path]))
140
            ->reduce(function ($output, $path) use ($locale) {
141
                if ($this->files->exists($full = "{$path}/{$locale}.json")) {
142
                    $decoded = json_decode($this->files->get($full), true);
143
 
144
                    if (is_null($decoded) || json_last_error() !== JSON_ERROR_NONE) {
145
                        throw new RuntimeException("Translation file [{$full}] contains an invalid JSON structure.");
146
                    }
147
 
148
                    $output = array_merge($output, $decoded);
149
                }
150
 
151
                return $output;
152
            }, []);
153
    }
154
 
155
    /**
156
     * Add a new namespace to the loader.
157
     *
158
     * @param  string  $namespace
159
     * @param  string  $hint
160
     * @return void
161
     */
162
    public function addNamespace($namespace, $hint)
163
    {
164
        $this->hints[$namespace] = $hint;
165
    }
166
 
167
    /**
168
     * Get an array of all the registered namespaces.
169
     *
170
     * @return array
171
     */
172
    public function namespaces()
173
    {
174
        return $this->hints;
175
    }
176
 
177
    /**
178
     * Add a new JSON path to the loader.
179
     *
180
     * @param  string  $path
181
     * @return void
182
     */
183
    public function addJsonPath($path)
184
    {
185
        $this->jsonPaths[] = $path;
186
    }
187
 
188
    /**
189
     * Get an array of all the registered paths to JSON translation files.
190
     *
191
     * @return array
192
     */
193
    public function jsonPaths()
194
    {
195
        return $this->jsonPaths;
196
    }
197
}