Passed
Push — master ( 76b450...5fc191 )
by Peter
02:19
created

I18nBootstrapper::scanDir()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 10
nc 4
nop 1
dl 0
loc 19
rs 9.6111
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Framework\Bootstrappers\I18n;
6
7
use AbterPhp\Framework\Constant\Env;
8
use AbterPhp\Framework\Constant\Session;
9
use AbterPhp\Framework\I18n\ITranslator;
10
use AbterPhp\Framework\I18n\Translator;
11
use Opulence\Framework\Configuration\Config;
12
use Opulence\Ioc\Bootstrappers\Bootstrapper;
13
use Opulence\Ioc\IContainer;
14
use Opulence\Sessions\ISession;
15
use Opulence\Views\Compilers\Fortune\ITranspiler;
16
17
class I18nBootstrapper extends Bootstrapper
18
{
19
    const LANG_PATH = 'lang/';
20
21
    /**
22
     * @inheritdoc
23
     */
24
    public function registerBindings(IContainer $container)
25
    {
26
        $this->registerTranslator($container);
27
        $this->registerViewFunction($container);
28
    }
29
30
    /**
31
     * @param IContainer $container
32
     */
33
    private function registerTranslator(IContainer $container)
34
    {
35
        $lang = $this->getLang($container);
36
37
        $translations = $this->getTranslations($container, $lang);
38
39
        $translator = new Translator($translations);
40
41
        $container->bindInstance(Translator::class, $translator);
42
        $container->bindInstance(ITranslator::class, $translator);
43
    }
44
45
    /**
46
     * @param IContainer $container
47
     *
48
     * @return string
49
     * @throws \Opulence\Ioc\IocException
50
     */
51
    protected function getLang(IContainer $container): string
52
    {
53
        /** @var ISession $session */
54
        $session = $container->resolve(ISession::class);
55
56
        if ($session->has(Session::LANGUAGE_IDENTIFIER)) {
57
            return (string)$session->get(Session::LANGUAGE_IDENTIFIER);
58
        }
59
60
        return (string)getenv(Env::DEFAULT_LANGUAGE);
61
    }
62
63
    /**
64
     * @param IContainer $container
65
     * @param string     $lang
66
     *
67
     * @return array
68
     * @throws \Opulence\Ioc\IocException
69
     */
70
    protected function getTranslations(IContainer $container, string $lang): array
0 ignored issues
show
Unused Code introduced by
The parameter $container is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

70
    protected function getTranslations(/** @scrutinizer ignore-unused */ IContainer $container, string $lang): array

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
71
    {
72
        $translations = [];
73
        foreach ($this->getLangDirs($lang) as $dir) {
74
            $translations = array_merge($translations, $this->scanDir($dir));
75
        }
76
77
        return $translations;
78
    }
79
80
    /**
81
     * @param string $dir
82
     *
83
     * @return string[]
84
     */
85
    protected function scanDir(string $dir): array
86
    {
87
        if (!is_dir($dir)) {
88
            return [];
89
        }
90
91
        $translations = [];
92
        foreach (scandir($dir) as $file) {
93
            if (strlen($file) < 4 || substr($file, -4) !== '.php') {
94
                continue;
95
            }
96
97
            $key   = substr($file, 0, -4);
98
            $value = require $dir . $file;
99
100
            $translations[$key] = $value;
101
        }
102
103
        return $translations;
104
    }
105
106
    /**
107
     * @param string $lang
108
     *
109
     * @return string[]
110
     * @throws \Opulence\Ioc\IocException
111
     */
112
    protected function getLangDirs(string $lang): array
113
    {
114
        global $abterModuleManager;
115
116
        $paths = [];
117
118
        $globalPath = Config::get('paths', 'resources.lang');
119
        if ($globalPath) {
120
            $paths[] = sprintf('%s/%s/', $globalPath, $lang);
121
        }
122
123
        foreach ($abterModuleManager->getResourcePaths() as $path) {
124
            $paths[] = sprintf('%s/%s/%s/', $path, static::LANG_PATH, $lang);
125
        }
126
127
        return $paths;
128
    }
129
130
    /**
131
     * @param IContainer $container
132
     */
133
    private function registerViewFunction(IContainer $container)
134
    {
135
        /** @var Translator $translator */
136
        $translator = $container->resolve(Translator::class);
137
138
        /** @var ITranspiler $transpiler */
139
        $transpiler = $container->resolve(ITranspiler::class);
140
        $transpiler->registerViewFunction(
141
            'tr',
142
            function (string $key, ...$args) use ($translator) {
143
                return $translator->translate($key, ...$args);
144
            }
145
        );
146
    }
147
}
148