Completed
Push — master ( bcac64...9b6ee4 )
by Nassif
11:39
created

Reader::loadTranslations()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 4.0047

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 14
cts 15
cp 0.9333
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 14
nc 3
nop 4
crap 4.0047
1
<?php
2
3
namespace Nikaia\TranslationSheet\Translation;
4
5
use Illuminate\Filesystem\Filesystem;
6
use Illuminate\Foundation\Application;
7
use Illuminate\Support\Collection;
8
9
class Reader
10
{
11
    /**
12
     * Translation items.
13
     *
14
     * @var \Illuminate\Support\Collection
15
     */
16
    protected $translations;
17
18
    /**
19
     * @var Application
20
     */
21
    protected $app;
22
23
    /**
24
     * @var \Illuminate\Filesystem\Filesystem
25
     */
26
    protected $files;
27
28
    /**
29
     * @var string specify locale that we need to scan.
30
     * Returns all if no locale specified.
31
     */
32
    private $locale;
33
34
    /**
35
     * Reader.
36
     *
37
     * @param Application $app
38
     * @param Filesystem $files
39
     */
40 2
    public function __construct(Application $app, Filesystem $files)
41
    {
42 2
        $this->app = $app;
43 2
        $this->files = $files;
44 2
    }
45
46
    /**
47
     * Set reader locale.
48
     *
49
     * @param $locale
50
     * @return $this
51
     */
52
    public function setLocale($locale)
53
    {
54
        $this->locale = $locale;
55
56
        return $this;
57
    }
58
59
    /**
60
     * Scan modules, app and overridden packages lang
61
     * and return all defined translations.
62
     *
63
     * @return Collection
64
     * @return array
65
     */
66 1
    public function scan()
67
    {
68
        // Reset
69 1
        $this->translations = new Collection;
70
71
        // App directory
72 1
        $this->scanDirectory($this->app->make('path.lang'));
73
74 1
        return $this->translations;
75
    }
76
77
    /**
78
     * Scan a directory.
79
     *
80
     * @param string $path to directory to scan
81
     */
82 1
    protected function scanDirectory($path)
83
    {
84 1
        foreach ($this->files->directories($path) as $directory) {
85 1
            if ($this->isVendorDirectory($directory)) {
86 1
                return $this->scanVendorDirectory($directory);
87
            }
88
89 1
            $this->loadTranslationsInDirectory($directory, $this->getLocaleFromDirectory($directory), null);
90
        }
91
    }
92
93
    /**
94
     * Scan overridden packages lang.
95
     *
96
     * @param $vendorsDirectory
97
     */
98 1
    private function scanVendorDirectory($vendorsDirectory)
99
    {
100 1
        foreach ($this->files->directories($vendorsDirectory) as $vendorPath) {
101 1
            $namespace = basename($vendorPath);
102 1
            foreach ($this->files->directories($vendorPath) as $localePath) {
103 1
                $this->loadTranslationsInDirectory($localePath, basename($localePath), $namespace);
104
            }
105
        }
106 1
    }
107
108
    /**
109
     * Load all directory file translation (multiple group) into translations collection.
110
     *
111
     * @param $directory
112
     * @param $locale
113
     * @param $namespace
114
     */
115 1
    private function loadTranslationsInDirectory($directory, $locale, $namespace)
116
    {
117 1
        if (! $this->requestedLocale($locale)) {
118
            return;
119
        }
120
121 1
        foreach ($this->files->files($directory) as $file) {
122 1
            $info = pathinfo($file);
123 1
            $group = $info['filename'];
124 1
            $this->loadTranslations($locale, $group, $namespace, $file);
125
        }
126 1
    }
127
128
    /**
129
     * Load file translation (group) into translations collection.
130
     *
131
     * @param $locale
132
     * @param $group
133
     * @param $namespace
134
     * @param $file
135
     */
136 1
    private function loadTranslations($locale, $group, $namespace, $file)
137
    {
138 1
        $translations = array_dot($this->app['translator']->getLoader()->load($locale, $group, $namespace));
139
140 1
        foreach ($translations as $key => $value) {
141
142
            // Avoid break in this case :
143
            // Case of 'car.messages = []', the key 'messages' is specified in the translation
144
            // file but no items defined inside.
145 1
            if (is_array($value) && count($value) === 0) {
146
                continue;
147
            }
148
149 1
            $entity = new Item;
150 1
            $entity->namespace = $namespace;
151 1
            $entity->locale = $locale;
152 1
            $entity->group = $group;
153 1
            $entity->key = $key;
154 1
            $entity->full_key = $this->fullKey($namespace, $group, $key);
155 1
            $entity->value = (string) $value;
156 1
            $entity->source_file = $this->sourceFile($file);
157
158 1
            $this->translations->push($entity);
159
        }
160 1
    }
161
162
    /**
163
     * Return a full lang key.
164
     *
165
     * @param $namespace
166
     * @param $group
167
     * @param $key
168
     * @return string
169
     */
170 1
    private function fullKey($namespace, $group, $key)
171
    {
172
        return
173 1
            ($namespace ? "$namespace::" : '')
174 1
            .$group.'.'
175 1
            .$key;
176
    }
177
178
    /**
179
     * Return relative path of language file.
180
     *
181
     * @param $file
182
     * @return mixed
183
     */
184 1
    private function sourceFile($file)
185
    {
186 1
        return $this->toRelative(realpath($file));
187
    }
188
189
    /**
190
     * Return relative path related to base_path().
191
     *
192
     * @param $path
193
     * @return string
194
     */
195 1
    private function toRelative($path)
196
    {
197 1
        $relative = str_replace($this->app->make('path.lang').DIRECTORY_SEPARATOR, '', $path);
198 1
        $relative = str_replace('\\', '/', $relative);
199
200 1
        return $relative;
201
    }
202
203
    /**
204
     *  Determine if a found locale is requested for scanning.
205
     *  If $this->locale is not set, we assume that all the locales were requested.
206
     *
207
     * @param string $locale the locale to check
208
     * @return bool
209
     */
210 1
    private function requestedLocale($locale)
211
    {
212 1
        if (empty($this->locale)) {
213 1
            return true;
214
        }
215
216
        return $locale === $this->locale;
217
    }
218
219
    /**
220
     * Return locale from directory
221
     *  ie. resources/lang/en -> en.
222
     *
223
     * @param $directory
224
     * @return string
225
     */
226 1
    private function getLocaleFromDirectory($directory)
227
    {
228 1
        return basename($directory);
229
    }
230
231
    /**
232
     * Return true if it is the vendor directory.
233
     *
234
     * @param $directory
235
     * @return bool
236
     */
237 2
    private function isVendorDirectory($directory)
238
    {
239 2
        return basename($directory) === 'vendor';
240
    }
241
}
242