Completed
Push — master ( 0e1003...1276c7 )
by Abdelrahman
61:46 queued 59:06
created

LangJsGenerator::getMessages()   B

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) {
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 121 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
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);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 153 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
48
                }
49
50
                if (starts_with($key, 'vendor')) {
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