auto_lang::add()   A
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 42
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 25
nc 8
nop 1
dl 0
loc 42
ccs 27
cts 27
cp 1
crap 5
rs 9.2088
c 0
b 0
f 0
1
<?php
2
/**
3
 *
4
 * @package sitemaker
5
 * @copyright (c) 2013 Daniel A. (blitze)
6
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
7
 *
8
 */
9
10
namespace blitze\sitemaker\services;
11
12
class auto_lang
13
{
14
	/** @var \phpbb\config\config */
15
	protected $config;
16
17
	/** @var \phpbb\extension\manager */
18
	protected $ext_manager;
19
20
	/** @var \phpbb\language\language */
21
	protected $translator;
22
23
	/** @var \phpbb\user */
24
	protected $user;
25
26
	/** @var string phpEx */
27
	protected $php_ext;
28
29
	/**
30
	 * Constructor
31
	 *
32
	 * @param \phpbb\config\config				$config				Config object
33
	 * @param \phpbb\extension\manager			$ext_manager		Extension manager object
34
	 * @param \phpbb\language\language			$translator			Language object
35
	 * @param \phpbb\user						$user				User object
36
	 * @param string							$php_ext			phpEx
37
	 */
38 4
	public function __construct(\phpbb\config\config $config, \phpbb\extension\manager $ext_manager, \phpbb\language\language $translator, \phpbb\user $user, $php_ext)
39
	{
40 4
		$this->config = $config;
41 4
		$this->ext_manager = $ext_manager;
42 4
		$this->translator = $translator;
43 4
		$this->user = $user;
44 4
		$this->php_ext = $php_ext;
45 4
	}
46
47
	/**
48
	 * Auto add the specified language file from across all extensions
49
	 *
50
	 * This is a modified copy of the add_mod_info in functions_module.php
51
	 * @param string $lang_file
52
	 */
53 4
	public function add($lang_file)
54
	{
55 4
		$finder = $this->ext_manager->get_finder();
56
57
		// We grab the language files from the default, English and user's language.
58
		// So we can fall back to the other files like we do when using add_lang()
59 4
		$default_lang_files = $english_lang_files = array();
60
61
		// Search for board default language if it's not the user language
62 4
		if ($this->config['default_lang'] != $this->user->lang_name)
63 4
		{
64
			$default_lang_files = $finder
65 2
				->prefix($lang_file)
66 2
				->suffix(".$this->php_ext")
67 2
				->extension_directory('/language/' . basename($this->config['default_lang']))
68 2
				->core_path('language/' . basename($this->config['default_lang']) . '/')
69 2
				->find();
70 2
		}
71
72
		// Search for english, if its not the default or user language
73 4
		if ($this->config['default_lang'] != 'en' && $this->user->lang_name != 'en')
74 4
		{
75
			$english_lang_files = $finder
76 2
				->prefix($lang_file)
77 2
				->suffix(".$this->php_ext")
78 2
				->extension_directory('/language/en')
79 2
				->core_path('language/en/')
80 2
				->find();
81 2
		}
82
83
		// Find files in the user's language
84
		$user_lang_files = $finder
85 4
			->prefix($lang_file)
86 4
			->suffix(".$this->php_ext")
87 4
			->extension_directory('/language/' . $this->user->lang_name)
88 4
			->core_path('language/' . $this->user->lang_name . '/')
89 4
			->find();
90
91 4
		$lang_files = array_unique(array_merge($user_lang_files, $english_lang_files, $default_lang_files));
92 4
		foreach ($lang_files as $file => $ext_name)
93
		{
94 4
			$this->translator->add_lang($file, $ext_name);
95 4
		}
96 4
	}
97
}
98