Passed
Pull Request — development (#3540)
by Emanuele
06:53
created

Loader   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 190
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 104
dl 0
loc 190
rs 10
c 1
b 0
f 0
wmc 19

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A changePath() 0 3 1
B fix_calendar_text() 0 63 1
A loadFile() 0 18 4
A setFallback() 0 3 1
B load() 0 56 10
1
<?php
2
3
/**
4
 * This class takes care of loading language files
5
 *
6
 * @package   ElkArte Forum
7
 * @copyright ElkArte Forum contributors
8
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause (see accompanying LICENSE.txt file)
9
 *
10
 * @version 2.0 dev
11
 *
12
 */
13
14
namespace ElkArte\Languages;
15
16
use ElkArte\Debug;
17
18
/**
19
 * This class takes care of loading language files
20
 */
21
class Loader
22
{
23
	/** @var string */
24
	protected $path = '';
25
26
	/** @var string */
27
	protected $language = 'English';
28
29
	/** @var bool */
30
	protected $load_fallback = true;
31
32
	/** @var mixed[] */
33
	protected $variable = true;
34
35
	/** @var string[] Holds the name of the files already loaded to load them only once */
36
	protected $loaded = [];
37
38
	public function __construct($lang = null, &$variable)
39
	{
40
		if ($lang !== null)
41
		{
42
			$this->language = ucfirst($lang);
43
		}
44
45
		$this->path = SOURCEDIR . '/ElkArte/Languages/';
46
47
		$this->variable = &$variable;
48
	}
49
50
	public function setFallback(bool $new_Status)
51
	{
52
		$this->load_fallback = $new_Status;
53
	}
54
55
	public function changePath($path)
56
	{
57
		$this->path = $path;
58
	}
59
60
	public function load($file_name, $fatal = true, $fix_calendar_arrays = false)
61
	{
62
		global $db_show_debug;
63
64
		$file_names = explode('+', $file_name);
65
66
		// For each file open it up and write it out!
67
		foreach ($file_names as $file)
68
		{
69
			$file = ucfirst($file);
70
			if (isset($this->loaded[$file]))
71
			{
72
				continue;
73
			}
74
75
			$found = false;
0 ignored issues
show
Unused Code introduced by
The assignment to $found is dead and can be removed.
Loading history...
76
			$found_fallback = false;
77
			if ($this->load_fallback)
78
			{
79
				$found_fallback = $this->loadFile($file, 'English');
80
			}
81
			$found = $this->loadFile($file, $this->language);
82
83
			$this->loaded[$file] = true;
84
85
			// Keep track of what we're up to, soldier.
86
			if ($found && $db_show_debug === true)
87
			{
88
				Debug::instance()->add(
89
					'language_files',
90
					$file . '.' . $this->language .
91
					' (' . str_replace(BOARDDIR, '', $this->path) . ')'
92
				);
93
			}
94
95
			// That couldn't be found!  Log the error, but *try* to continue normally.
96
			if (!$found && $fatal)
97
			{
98
				Errors::instance()->log_error(
0 ignored issues
show
Bug introduced by
The type ElkArte\Languages\Errors was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
99
					sprintf(
100
						$txt['theme_language_error'],
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $txt seems to be never defined.
Loading history...
101
						$file . '.' . $this->language,
102
						'template'
103
					)
104
				);
105
				// If we do have a fallback it may not be necessary to break out.
106
				if ($found_fallback === false)
107
				{
108
					break;
109
				}
110
			}
111
		}
112
113
		if ($fix_calendar_arrays)
114
		{
115
			$this->fix_calendar_text();
116
		}
117
	}
118
119
	protected function loadFile($name, $lang)
120
	{
121
		$filepath = $this->path . $name . '/' . $lang . '.php';
122
		if (file_exists($filepath))
123
		{
124
			require($filepath);
125
			if (!empty($txt))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $txt seems to never exist and therefore empty should always be true.
Loading history...
126
			{
127
				$this->variable += $txt;
128
			}
129
			// Temporary solution (I think);
130
			if (!empty($txtBirthdayEmails))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $txtBirthdayEmails seems to never exist and therefore empty should always be true.
Loading history...
131
			{
132
				$this->variable['$txtBirthdayEmails'] = $txtBirthdayEmails;
133
			}
134
			return true;
135
		}
136
		return false;
137
	}
138
139
	/**
140
	 * Loads / Sets arrays for use in date display
141
	 * This is here and not in a language file for two reasons:
142
	 *  1. the structure is required by the code, so better be sure
143
	 *     to have it the way we are supposed to have it
144
	 *  2. Transifex (that we use for translating the strings) doesn't
145
	 *     support array of arrays, so if we move this to a language file
146
	 *     we'd need to move away from Tx.
147
	 */
148
	protected function fix_calendar_text()
149
	{
150
		global $txt;
151
152
		$txt['days'] = array(
153
			$txt['sunday'],
154
			$txt['monday'],
155
			$txt['tuesday'],
156
			$txt['wednesday'],
157
			$txt['thursday'],
158
			$txt['friday'],
159
			$txt['saturday'],
160
		);
161
		$txt['days_short'] = array(
162
			$txt['sunday_short'],
163
			$txt['monday_short'],
164
			$txt['tuesday_short'],
165
			$txt['wednesday_short'],
166
			$txt['thursday_short'],
167
			$txt['friday_short'],
168
			$txt['saturday_short'],
169
		);
170
		$txt['months'] = array(
171
			1 => $txt['january'],
172
			$txt['february'],
173
			$txt['march'],
174
			$txt['april'],
175
			$txt['may'],
176
			$txt['june'],
177
			$txt['july'],
178
			$txt['august'],
179
			$txt['september'],
180
			$txt['october'],
181
			$txt['november'],
182
			$txt['december'],
183
		);
184
		$txt['months_titles'] = array(
185
			1 => $txt['january_titles'],
186
			$txt['february_titles'],
187
			$txt['march_titles'],
188
			$txt['april_titles'],
189
			$txt['may_titles'],
190
			$txt['june_titles'],
191
			$txt['july_titles'],
192
			$txt['august_titles'],
193
			$txt['september_titles'],
194
			$txt['october_titles'],
195
			$txt['november_titles'],
196
			$txt['december_titles'],
197
		);
198
		$txt['months_short'] = array(
199
			1 => $txt['january_short'],
200
			$txt['february_short'],
201
			$txt['march_short'],
202
			$txt['april_short'],
203
			$txt['may_short'],
204
			$txt['june_short'],
205
			$txt['july_short'],
206
			$txt['august_short'],
207
			$txt['september_short'],
208
			$txt['october_short'],
209
			$txt['november_short'],
210
			$txt['december_short'],
211
		);
212
	}
213
}