Passed
Push — master ( bf3ff8...091da6 )
by Joël
02:22
created

Gettext   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 96.08%

Importance

Changes 0
Metric Value
dl 0
loc 110
ccs 49
cts 51
cp 0.9608
rs 10
c 0
b 0
f 0
wmc 11
lcom 0
cbo 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 19 1
C init() 0 76 10
1
<?php
1 ignored issue
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 18 and the first side effect is on line 4.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
// @codeCoverageIgnoreStart
3
if (!defined('PHPUNIT_TESTING')) {
4
    defined('BASEPATH') || exit('No direct script access allowed');
5
}
6
// @codeCoverageIgnoreEnd
7
8
/**
9
 * Codeigniter PHP framework library class for dealing with gettext.
10
 *
11
 * @package     CodeIgniter
12
 * @subpackage  Libraries
13
 * @category    Language
14
 * @author      Joël Gaujard <[email protected]>
15
 * @author      Marko Martinović <[email protected]>
16
 * @link        https://github.com/joel-depiltech/codeigniter-gettext
17
 */
18
class Gettext
1 ignored issue
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
19
{
20
    /**
21
     * Initialize Codeigniter PHP framework and get configuration
22
     *
23
     * @codeCoverageIgnore
24
     * @param array $config Override default configuration
25
     */
26
    public function __construct($config = array())
27
    {
28
        log_message('info', 'Gettext Library Class Initialized');
29
30
        $CI = &get_instance();
31
32
        // Merge $config and config/gettext.php $config
33
        $config = array_merge(
34
            array(
35
                'gettext_locale_dir' => $CI->config->item('gettext_locale_dir'),
36
                'gettext_text_domain' => $CI->config->item('gettext_text_domain'),
37
                'gettext_catalog_codeset' => $CI->config->item('gettext_catalog_codeset'),
38
                'gettext_locale' => $CI->config->item('gettext_locale')
39
            ),
40
            $config
41
        );
42
43
        self::init($config);
44
    }
45
46
    /**
47
     * Initialize gettext inside Codeigniter PHP framework.
48
     *
49
     * @param array $config configuration
50
     */
51 6
    static public function init(array $config)
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
52
    {
53
        // Gettext catalog codeset
54 6
        $IsBindTextDomainCodeset = bind_textdomain_codeset(
55 6
            $config['gettext_text_domain'],
56 6
            $config['gettext_catalog_codeset']
57 6
        );
58
59 6
        log_message(
60 6
            (is_string($IsBindTextDomainCodeset) ? 'info' : 'error'),
61
            'Gettext Library -> Try to bind text domain code set: ' .
62 6
            $config['gettext_catalog_codeset']
63 6
        );
64
65
        // Path to gettext locales directory relative to FCPATH.APPPATH
66 6
        $IsBindTextDomain = bindtextdomain(
67 6
            $config['gettext_text_domain'],
68 6
            APPPATH . $config['gettext_locale_dir']
69 6
        );
70
71 6
        log_message(
72 6
            (is_string($IsBindTextDomain) ? 'info' : 'error'),
73
            'Gettext Library -> Try to bind text domain directory: ' .
74 6
            APPPATH . $config['gettext_locale_dir']
75 6
        );
76
77
        // Gettext domain
78 6
        $IsSetTextDomain = textdomain(
79 6
            $config['gettext_text_domain']
80 6
        );
81
82 6
        log_message(
83 6
            (is_string($IsSetTextDomain) ? 'info' : 'error'),
84
            'Gettext Library -> Try to set text domain: ' .
85 6
            $config['gettext_text_domain']
86 6
        );
87
88
        // Gettext locale
89 6
        $IsSetLocale = setlocale(
90 6
            LC_ALL,
91 6
            $config['gettext_locale']
92 6
        );
93
94 6
        log_message(
95 6
            (is_string($IsSetLocale) ? 'info' : 'error'),
96
            'Gettext Library -> Try to set locale: ' .
97 6
            (is_array($config['gettext_locale']) ?
98 6
                print_r($config['gettext_locale'], TRUE) : $config['gettext_locale']
99
            )
100 6
        );
101
102
        // Change environment language for CLI
103 6
        $IsPutEnv = putenv('LANGUAGE=' . (is_array($config['gettext_locale']) ? $config['gettext_locale'][0] : $config['gettext_locale']));
104
105 6
        log_message(
106 6
            ($IsPutEnv === TRUE ? 'info' : 'error'),
107
            'Gettext Library -> Try to set environment language: ' .
108 6
            (is_array($config['gettext_locale']) ?
109 6
                $config['gettext_locale'][0] : $config['gettext_locale']
110
            )
111 6
        );
112
113
        // MO file exists for language
114 6
        $file = APPPATH . $config['gettext_locale_dir'] .
115 6
            '/' . $IsSetLocale .
116 6
            '/LC_MESSAGES/' .
117 6
            $config['gettext_text_domain'] . '.mo'
118 6
        ;
119 6
        $IsFileExists = is_file($file);
120
121 6
        log_message(
122 6
            ($IsFileExists === TRUE ? 'info' : 'error'),
123
            'Gettext Library -> Try to check MO file exists: ' .
124
            $file
125 6
        );
126 6
    }
127
}
128
129
/* End of file Gettext.php */
130
/* Location: ./libraries/config/gettext.php */