Completed
Push — develop ( 2e9746...a14a57 )
by Abdelrahman
01:46
created

LangJsGenerator::getMessages()   C

Complexity

Conditions 8
Paths 12

Size

Total Lines 46
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 46
rs 5.5555
c 0
b 0
f 0
cc 8
eloc 27
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
     * @return array
20
     *
21
     * @throws \Exception
22
     */
23
    protected function getMessages()
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 = substr($file->getPath(), 0, 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 = substr($pathName, 0, -4);
43
                $key = str_replace('\\', '.', $key);
44
                $key = str_replace('/', '.', $key);
45
46
                if ($namespace) {
47
                    $key = substr($key, 0, strpos($key, '.')+1).str_replace('/', '.', $namespace).'::'.substr($key, strpos($key, '.')+1);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 137 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
65
        $this->sortMessages($messages);
66
67
        return $messages;
68
    }
69
70
    private function getVendorKey($key)
71
    {
72
        $keyParts = explode('.', $key, 4);
73
        unset($keyParts[0]);
74
75
        return $keyParts[2] .'.'. $keyParts[1] . '::' . $keyParts[3];
76
    }
77
}
78