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
|
|
|
* Localize module for translations |
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 $resourceFileType; |
39
|
|
|
private $locale; |
40
|
|
|
private $values; |
41
|
|
|
|
42
|
|
|
public function __construct($config = []) { |
43
|
|
|
if (is_array($config)) { |
44
|
|
|
$config = new LocalizeConfig($config); |
45
|
|
|
} else if (!is_a($config, "\Tight\Modules\Localize\LocalizeConfig")) { |
46
|
|
|
throw new \InvalidArgumentException("Argument 1 passed to " . get_class($this) . " must be an array or an instance of Tight\Modules\Localize\LocalizeConfig"); |
47
|
|
|
} |
48
|
|
|
parent::__construct(); |
49
|
|
|
$this->setConfig($config); |
50
|
|
|
$this->setResourceFileType($this->config->resourceFileType); |
51
|
|
|
$this->checkDependences(); |
52
|
|
|
$this->setLocale($this->config->defaultLocale); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function setResourceFileType($resourceFileType) { |
56
|
|
|
$this->resourceFileType = $resourceFileType; |
57
|
|
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function getValues() { |
61
|
|
|
return $this->values; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function reloadConfig() { |
65
|
|
|
$this->locale = $this->config->defaultLocale; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
private function checkDependences() { |
69
|
|
|
if (!is_dir($this->config->resourceFolder)) { |
70
|
|
|
throw new \Tight\Modules\ModuleException("Resource directory not found"); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function setConfig(LocalizeConfig $config) { |
75
|
|
|
$this->config = $config; |
76
|
|
|
$this->reloadConfig(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function setLocale($locale) { |
80
|
|
|
$this->locale = $locale; |
81
|
|
|
$folder = \Tight\Utils::addTrailingSlash($this->config->resourceFolder); |
82
|
|
|
$fileName = $this->config->resourceFileName . $this->config->langSeparator . $locale . "." . $this->config->resourceFileType; |
83
|
|
|
$file = $folder . $fileName; |
84
|
|
|
if (is_file($file)) { |
85
|
|
|
$this->values = json_decode(file_get_contents($file), JSON_FORCE_OBJECT); |
86
|
|
|
} else { |
87
|
|
|
$fileName = $this->config->resourceFileName . "." . $this->config->resourceFileType; |
88
|
|
|
$file = $folder . $fileName; |
89
|
|
|
if (is_file($file)) { |
90
|
|
|
$this->values = json_decode(file_get_contents($file), JSON_FORCE_OBJECT); |
91
|
|
|
} else { |
92
|
|
|
throw new \Tight\Modules\ModuleException("Resource file <strong>" . $file . "</strong> not found"); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Gets the module configuration |
99
|
|
|
* @return \Tight\Modules\Localize\LocalizeConfig |
100
|
|
|
*/ |
101
|
|
|
public function getConfig() { |
102
|
|
|
return $this->config; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function getLocales() { |
106
|
|
|
$output = []; |
107
|
|
|
$directory = $this->config->resourceFolder; |
108
|
|
|
$fileName = $this->config->resourceFileName; |
109
|
|
|
$fileType = $this->config->resourceFileType; |
|
|
|
|
110
|
|
|
$dir = opendir($directory); |
111
|
|
|
$files = []; |
112
|
|
|
while ($entry = readdir($dir)) { |
113
|
|
|
if (strpos($entry, $fileName) !== false) { |
114
|
|
|
$files[] = $entry; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
foreach ($files as $element) { |
118
|
|
|
$file = \Tight\Utils::getSlicedFile($directory . $element); |
119
|
|
|
//Removes extension |
120
|
|
|
$name = $file["name"]; |
121
|
|
|
$explode = explode($this->config->langSeparator, $name); |
122
|
|
|
// Get the locale of the defined file type |
123
|
|
|
if ($file["ext"] == $this->config->resourceFileType) { |
124
|
|
|
if (count($explode) > 1) { |
125
|
|
|
$output[] = $explode[count($explode) - 1]; |
126
|
|
|
} else { |
127
|
|
|
$output[] = $this->config->defaultLocale; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
return $output; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function get($key) { |
135
|
|
|
if (isset($this->values[$key])) { |
136
|
|
|
return $this->values[$key]; |
137
|
|
|
} else { |
138
|
|
|
return ""; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
} |
143
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.