LangJsGenerator::getMessages()   B
last analyzed

Complexity

Conditions 8
Paths 12

Size

Total Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 45
rs 7.9555
cc 8
nc 12
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Foundation\Generators;
6
7
use Mariuzzo\LaravelJsLocalization\Generators\LangJsGenerator as BaseLangJsGenerator;
8
9
/**
10
 * The LangJsGenerator class.
11
 *
12
 * @author  Rubens Mariuzzo <[email protected]>
13
 */
14
class LangJsGenerator extends BaseLangJsGenerator
15
{
16
    /**
17
     * Return all language messages.
18
     *
19
     * @throws \Exception
20
     *
21
     * @return array
22
     */
23
    protected function getMessages(): array
24
    {
25
        $messages = [];
26
27
        foreach (array_merge(array_values(app('translation.loader')->namespaces()), [$this->sourcePath]) as $directory) {
28
            foreach ($this->file->allFiles($directory) as $file) {
29
                $path = mb_substr($file->getPath(), 0, mb_strrpos($file->getPath(), DIRECTORY_SEPARATOR));
30
                $namespace = array_search($path, app('translation.loader')->namespaces());
31
32
                $pathName = $file->getRelativePathName();
33
                $extension = $file->getExtension();
34
                if (! in_array($extension, ['json', 'php'])) {
35
                    continue;
36
                }
37
38
                if ($this->isMessagesExcluded($pathName)) {
39
                    continue;
40
                }
41
42
                $key = mb_substr($pathName, 0, -4);
43
                $key = str_replace('\\', '.', $key);
44
                $key = str_replace('/', '.', $key);
45
46
                if ($namespace) {
47
                    $key = mb_substr($key, 0, mb_strpos($key, '.') + 1).str_replace('/', '.', $namespace).'::'.mb_substr($key, mb_strpos($key, '.') + 1);
48
                }
49
50
                if (starts_with($key, 'vendor')) {
0 ignored issues
show
Deprecated Code introduced by
The function starts_with() has been deprecated with message: Str::startsWith() should be used directly instead. Will be removed in Laravel 6.0.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
51
                    $key = $this->getVendorKey($key);
52
                }
53
54
                if ($extension === 'php') {
55
                    $messages[$key] = include $file->getRealPath();
56
                } else {
57
                    $key = $key.$this->stringsDomain;
58
                    $fileContent = file_get_contents($file->getRealPath());
59
                    $messages[$key] = json_decode($fileContent, true);
60
                }
61
            }
62
        }
63
64
        $this->sortMessages($messages);
65
66
        return $messages;
67
    }
68
69
    private function getVendorKey($key)
70
    {
71
        $keyParts = explode('.', $key, 4);
72
        unset($keyParts[0]);
73
74
        return $keyParts[2].'.'.$keyParts[1].'::'.$keyParts[3];
75
    }
76
}
77