1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* MikoPBX - free phone system for small business |
4
|
|
|
* Copyright © 2017-2023 Alexey Portnov and Nikolay Beketov |
5
|
|
|
* |
6
|
|
|
* This program is free software: you can redistribute it and/or modify |
7
|
|
|
* it under the terms of the GNU General Public License as published by |
8
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
9
|
|
|
* (at your option) any later version. |
10
|
|
|
* |
11
|
|
|
* This program is distributed in the hope that it will be useful, |
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14
|
|
|
* GNU General Public License for more details. |
15
|
|
|
* |
16
|
|
|
* You should have received a copy of the GNU General Public License along with this program. |
17
|
|
|
* If not, see <https://www.gnu.org/licenses/>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
declare(strict_types=1); |
21
|
|
|
|
22
|
|
|
namespace MikoPBX\Common\Providers; |
23
|
|
|
|
24
|
|
|
use MikoPBX\Core\System\Util; |
25
|
|
|
use Phalcon\Di\DiInterface; |
26
|
|
|
use Phalcon\Di\ServiceProviderInterface; |
27
|
|
|
use function MikoPBX\Common\Config\appPath; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The MessagesProvider class is responsible for registering the messages service. |
31
|
|
|
* |
32
|
|
|
* @package MikoPBX\Common\Providers |
33
|
|
|
*/ |
34
|
|
|
class MessagesProvider implements ServiceProviderInterface |
35
|
|
|
{ |
36
|
|
|
public const SERVICE_NAME = 'messages'; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Register the messages service provider. |
40
|
|
|
* |
41
|
|
|
* @param DiInterface $di The DI container. |
42
|
|
|
*/ |
43
|
|
|
public function register(DiInterface $di): void |
44
|
|
|
{ |
45
|
|
|
$coreConfig = $di->getShared(ConfigProvider::SERVICE_NAME)->path('core'); |
46
|
|
|
$di->setShared( |
47
|
|
|
self::SERVICE_NAME, |
48
|
|
|
function () use ($di, $coreConfig) { |
49
|
|
|
$cacheKey = false; |
50
|
|
|
$language = $di->get(LanguageProvider::SERVICE_NAME); |
51
|
|
|
if (php_sapi_name() !== 'cli') { |
52
|
|
|
$version = PBXConfModulesProvider::getVersionsHash(); |
53
|
|
|
$cacheKey = 'LocalisationArray:' . $version . ':' . $language; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
// Check if translations exist in the cache |
57
|
|
|
if ($cacheKey) { |
58
|
|
|
$translates = $di->get(ManagedCacheProvider::SERVICE_NAME)->get($cacheKey, 3600); |
59
|
|
|
if (is_array($translates)) { |
60
|
|
|
return $translates; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
// Load English translations |
65
|
|
|
$translates = self::includeLanguageFile(appPath('/src/Common/Messages/en.php')); |
66
|
|
|
|
67
|
|
|
if ($language !== 'en') { |
68
|
|
|
// Check if translation file exists for the selected language |
69
|
|
|
$langFile = appPath("/src/Common/Messages/{$language}.php"); |
70
|
|
|
if (file_exists($langFile)) { |
71
|
|
|
$langArr = self::includeLanguageFile($langFile); |
72
|
|
|
if (!empty($langArr)) { |
73
|
|
|
$translates = array_merge($translates, $langArr); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
// Load English translations for extensions |
79
|
|
|
$extensionsTranslates = [[]]; |
80
|
|
|
$results = glob($coreConfig->modulesDir . '/*/{Messages}/en.php', GLOB_BRACE); |
81
|
|
|
foreach ($results as $path) { |
82
|
|
|
$langArr = self::includeLanguageFile($path); |
83
|
|
|
if (!empty($langArr)) { |
84
|
|
|
$extensionsTranslates[] = $langArr; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
if ($extensionsTranslates !== [[]]) { |
88
|
|
|
$translates = array_merge($translates, ...$extensionsTranslates); |
89
|
|
|
} |
90
|
|
|
if ($language !== 'en') { |
91
|
|
|
$additionalTranslates = [[]]; |
92
|
|
|
$results = glob( |
93
|
|
|
$coreConfig->modulesDir . "/*/{Messages}/{$language}.php", |
94
|
|
|
GLOB_BRACE |
95
|
|
|
); |
96
|
|
|
foreach ($results as $path) { |
97
|
|
|
$langArr = self::includeLanguageFile($path); |
98
|
|
|
if (!empty($langArr)) { |
99
|
|
|
$additionalTranslates[] = $langArr; |
100
|
|
|
$translates = array_merge($translates, ...$additionalTranslates); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
if ($cacheKey) { |
105
|
|
|
$di->get(ManagedCacheProvider::SERVICE_NAME)->set($cacheKey, $translates); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
// Return a translation object |
109
|
|
|
return $translates; |
110
|
|
|
} |
111
|
|
|
); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Includes the language file and returns its content as an array. |
116
|
|
|
* |
117
|
|
|
* @param string $path The path to the language file. |
118
|
|
|
* @return array The language array if successful, otherwise an empty array. |
119
|
|
|
*/ |
120
|
|
|
private static function includeLanguageFile(string $path): array |
121
|
|
|
{ |
122
|
|
|
try { |
123
|
|
|
// Try to include the language file and store its content in $langArr. |
124
|
|
|
$langArr = require $path; |
125
|
|
|
|
126
|
|
|
// Check if $langArr is an array and return it if successful. |
127
|
|
|
if (is_array($langArr)) { |
128
|
|
|
return $langArr; |
129
|
|
|
} |
130
|
|
|
} catch (\Throwable $e) { |
131
|
|
|
// If an error occurs while including the file, log the exception and error message. |
132
|
|
|
global $errorLogger; |
133
|
|
|
$errorLogger->captureException($e); |
134
|
|
|
Util::sysLogMsg(__METHOD__, $e->getMessage(), LOG_ERR); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
// Return an empty array if there was an error or $langArr is not an array. |
138
|
|
|
return []; |
139
|
|
|
} |
140
|
|
|
} |