Passed
Push — master ( f25758...21e806 )
by Fran
11:16
created

CustomTranslateExtension::generate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 0
loc 6
ccs 0
cts 5
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
namespace PSFS\base\extension;
3
4
use PSFS\base\config\Config;
5
use PSFS\base\Logger;
6
use PSFS\base\Security;
7
use PSFS\base\types\helpers\I18nHelper;
8
use PSFS\base\types\traits\SingletonTrait;
9
10
/**
11
 * Class CustomTranslateExtension
12
 * @package PSFS\base\extension
13
 */
14
class CustomTranslateExtension extends \Twig_Extension {
15
    use SingletonTrait;
16
17
    const CUSTOM_LOCALE_SESSION_KEY = '__PSFS_CUSTOM_LOCALE_KEY__';
18
19
    protected static $translations = [];
20
    protected static $locale = 'es_ES';
21
    protected static $generate = false;
22
    protected static $filename = '';
23
24
    /**
25
     * @param string $custom_key
0 ignored issues
show
Documentation introduced by
Should the type for parameter $custom_key not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
26
     * @param bool $force_reload
27
     * @param bool $use_base
28
     */
29 1
    protected static function checkLoad($custom_key = null, $force_reload = false, $use_base = false) {
30 1
        if($force_reload) self::dropInstance();
31 1
        self::$locale = I18nHelper::extractLocale(Security::getInstance()->getSessionKey(I18nHelper::PSFS_SESSION_LANGUAGE_KEY));
32 1
        self::$generate = (boolean)Config::getParam('i18n.autogenerate', false);
33 1
        if(!$use_base) {
34 1
            $custom_key = $custom_key ?: Security::getInstance()->getSessionKey(self::CUSTOM_LOCALE_SESSION_KEY);
35
        }
36 1
        if(null !== $custom_key) {
37
            Logger::log('[' . self::class . '] Custom key detected: ' . $custom_key, LOG_INFO);
38
            self::$filename = implode(DIRECTORY_SEPARATOR, [LOCALE_DIR, 'custom', $custom_key, self::$locale . '.json']);
39
        } else {
40 1
            self::$filename = implode(DIRECTORY_SEPARATOR, [LOCALE_DIR, 'custom', self::$locale . '.json']);
41
        }
42 1
        if(file_exists(self::$filename)) {
43
            Logger::log('[' . self::class . '] Custom locale detected: ' . $custom_key . ' [' . self::$locale . ']', LOG_INFO);
44
            self::$translations = json_decode(file_get_contents(self::$filename), true);
45 1
        } elseif(null !== $custom_key) {
46
            self::checkLoad(null, $force_reload, true);
47
        }
48 1
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 1
    public function getTokenParsers()
54
    {
55 1
        return array(new \Twig_Extensions_TokenParser_Trans());
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function getFilters()
62
    {
63
        return array(
64 1
            new \Twig_SimpleFilter('trans', function($message) {
65 1
                return self::_($message);
66 1
            }),
67
        );
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function getName()
74
    {
75
        return 'PSFSi18n';
76
    }
77
78
    /**
79
     * @param $message
80
     * @param string $custom_key
0 ignored issues
show
Documentation introduced by
Should the type for parameter $custom_key not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
81
     * @param bool $force_reload
82
     * @return mixed|string
83
     */
84 1
    public static function _($message, $custom_key = null, $force_reload = false) {
85 1
        self::checkLoad($custom_key, $force_reload);
86 1
        if(array_key_exists($message, self::$translations)) {
87
            $translation =  self::$translations[$message];
88
        } else {
89 1
            $translation = gettext($message);
90
        }
91 1
        if(self::$generate) {
92
            self::generate($message, $translation);
93
        }
94 1
        return $translation;
95
    }
96
97
    /**
98
     * @param string $message
99
     * @param string $translation
100
     */
101
    protected static function generate($message, $translation) {
102
        if(!array_key_exists($message, self::$translations)) {
103
            self::$translations[$message] = $translation;
104
        }
105
        file_put_contents(self::$filename, json_encode(array_unique(self::$translations), JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
106
    }
107
}