|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* The MIT License |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright 2016 Alejandro Peña Florentín ([email protected]). |
|
7
|
|
|
* |
|
8
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
9
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
10
|
|
|
* in the Software without restriction, including without limitation the rights |
|
11
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
12
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
13
|
|
|
* furnished to do so, subject to the following conditions: |
|
14
|
|
|
* |
|
15
|
|
|
* The above copyright notice and this permission notice shall be included in |
|
16
|
|
|
* all copies or substantial portions of the Software. |
|
17
|
|
|
* |
|
18
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
19
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
20
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
21
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
22
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
23
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
24
|
|
|
* THE SOFTWARE. |
|
25
|
|
|
*/ |
|
26
|
|
|
|
|
27
|
|
|
namespace Tight\Modules\Localize; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Description of Localize |
|
31
|
|
|
* |
|
32
|
|
|
* @author Alejandro Peña Florentín ([email protected]) |
|
33
|
|
|
*/ |
|
34
|
|
|
class Localize extends \Tight\Modules\AbstractModule |
|
35
|
|
|
{ |
|
36
|
|
|
|
|
37
|
|
|
private $config; |
|
38
|
|
|
private $locale; |
|
39
|
|
|
private $values; |
|
40
|
|
|
|
|
41
|
|
|
public function __construct(LocalizeConfig $config) { |
|
42
|
|
|
parent::__construct(); |
|
43
|
|
|
$this->setConfig($config); |
|
44
|
|
|
$this->checkDependences(); |
|
45
|
|
|
$this->setLocale($this->config->defaultLocale); |
|
46
|
|
|
} |
|
47
|
|
|
public function getValues(){ |
|
48
|
|
|
return $this->values; |
|
49
|
|
|
} |
|
50
|
|
|
public function reloadConfig() { |
|
51
|
|
|
$this->locale = $this->config->defaultLocale; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
private function checkDependences() { |
|
55
|
|
|
if (!is_dir($this->config->resourceFolder)) { |
|
56
|
|
|
throw new \Tight\Modules\ModuleException("Resource directory not found"); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function setConfig(LocalizeConfig $config) { |
|
61
|
|
|
$this->config = $config; |
|
62
|
|
|
$this->reloadConfig(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function setLocale($locale) { |
|
66
|
|
|
$this->locale = $locale; |
|
67
|
|
|
$folder = \Tight\Utils::addTrailingSlash($this->config->resourceFolder); |
|
68
|
|
|
$fileName = $this->config->resourceFileName . $this->config->langSeparator . $locale . "." . $this->config->resourceFileType; |
|
69
|
|
|
$file = $folder . $fileName; |
|
70
|
|
|
if (is_file($file)) { |
|
71
|
|
|
echo $file; |
|
72
|
|
|
} else { |
|
73
|
|
|
$fileName = $this->config->resourceFileName . "." . $this->config->resourceFileType; |
|
74
|
|
|
$file = $folder . $fileName; |
|
75
|
|
|
if (!is_file($file)) { |
|
76
|
|
|
throw new \Tight\Modules\ModuleException("Resource file <strong>" . $file . "</strong> not found"); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
$this->values = json_decode(file_get_contents($file)); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Gets the module configuration |
|
84
|
|
|
* @return \Tight\Modules\Localize\LocalizeConfig |
|
85
|
|
|
*/ |
|
86
|
|
|
public function getConfig() { |
|
87
|
|
|
return $this->config; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function getLocales() { |
|
91
|
|
|
$output = []; |
|
92
|
|
|
$directory = $this->config->resourceFolder; |
|
93
|
|
|
$fileName = $this->config->resourceFileName; |
|
94
|
|
|
$fileType = $this->config->resourceFileType; |
|
|
|
|
|
|
95
|
|
|
$dir = opendir($directory); |
|
96
|
|
|
$files = []; |
|
97
|
|
|
while ($entry = readdir($dir)) { |
|
98
|
|
|
if (strpos($entry, $fileName) !== false) { |
|
99
|
|
|
$files[] = $entry; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
foreach ($files as $element) { |
|
103
|
|
|
//Removes extension |
|
104
|
|
|
$name = explode(".", $element)[0]; |
|
105
|
|
|
$explode = explode($this->config->langSeparator, $name); |
|
106
|
|
|
if (count($explode) > 1) { |
|
107
|
|
|
$output[] = $explode[count($explode) - 1]; |
|
108
|
|
|
} else { |
|
109
|
|
|
$output[] = $this->config->defaultLocale; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
return $output; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function get($key) { |
|
|
|
|
|
|
116
|
|
|
|
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
} |
|
120
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.