Bitrix-D7
23.9
Загрузка...
Поиск...
Не найдено
remotedictionary.php
1
<?php
9
namespace
Bitrix\Rest\Dictionary
;
10
11
use
Bitrix\Main\Application
;
12
use
Bitrix\Main\ArgumentException
;
13
use
Bitrix\Main\Event
;
14
use
Bitrix\Main\Loader
;
15
use
Bitrix\Main\Localization\Loc
;
16
use
Bitrix\Main\Type\Dictionary
;
17
use
Bitrix\Main\Web\HttpClient
;
18
use
Bitrix\Main\Web\Json
;
19
use
Bitrix\Main\Web\Uri
;
20
21
class
RemoteDictionary
extends
Dictionary
22
{
23
const
ID
=
'generic'
;
24
25
const
CACHE_TTL
= 86400;
26
const
CACHE_PREFIX
=
'rest_dictionary'
;
27
28
protected
$baseUrl
= array(
29
'ru'
=>
'https://www.bitrix24.ru/util/'
,
30
'ua'
=>
'https://www.bitrix24.eu/util/'
,
31
'by'
=>
'https://www.bitrix24.by/util/'
,
32
'in'
=>
'https://www.bitrix24.in/util/'
,
33
'en'
=>
'https://www.bitrix24.com/util/'
,
34
'de'
=>
'https://www.bitrix24.de/util/'
,
35
'kz'
=>
'https://www.bitrix24.kz/util/'
,
36
'br'
=>
'https://www.bitrix24.com.br/util/'
,
37
'pl'
=>
'https://www.bitrix24.pl/util/'
,
38
'fr'
=>
'https://www.bitrix24.fr/util/'
,
39
'la'
=>
'https://www.bitrix24.es/util/'
,
40
'eu'
=>
'https://www.bitrix24.eu/util/'
,
41
'cn'
=>
'https://www.bitrix24.cn/util/'
,
42
'tc'
=>
'https://www.bitrix24.cn/util/'
,
43
'sc'
=>
'https://www.bitrix24.cn/util/'
,
44
'tr'
=>
'https://www.bitrix24.com.tr/util/'
,
45
);
46
protected
$language
=
null
;
47
48
public
function
__construct
()
49
{
50
$this->language = LANGUAGE_ID;
51
52
$values
= $this->
init
();
53
54
parent::__construct(
$values
);
55
}
56
57
public
function
setLanguage
(
$language
)
58
{
59
if
(
$language
!== $this->language)
60
{
61
$this->language =
$language
;
62
$this->
set
($this->
init
());
63
}
64
}
65
66
protected
function
init
()
67
{
68
$managedCache =
Application::getInstance
()->getManagedCache();
69
if
($managedCache->read(static::CACHE_TTL, $this->getCacheId()))
70
{
71
$dictionary = $managedCache->get($this->
getCacheId
());
72
}
73
else
74
{
75
$dictionary = $this->
load
();
76
77
$managedCache->set($this->
getCacheId
(), $dictionary);
78
}
79
80
$event =
new
Event
(
'rest'
,
'onRemoteDictionaryLoad'
, array(
81
'ID'
=> static::ID,
82
'DICTIONARY'
=> &$dictionary
83
));
84
$event->send();
85
86
return
$dictionary;
87
}
88
89
protected
function
load
()
90
{
91
$httpClient =
new
HttpClient
();
92
93
$uri = $this->
getDictionaryUri
();
94
95
$httpResult = $httpClient->get($uri->getLocator());
96
97
try
98
{
99
$result = Json::decode($httpResult);
100
}
101
catch
(
ArgumentException
$e)
102
{
103
$result =
null
;
104
}
105
106
return
$result;
107
}
108
109
protected
function
getCacheId
()
110
{
111
return
static::CACHE_PREFIX.
'/'
.static::ID.
'/'
.
$this->language
;
112
}
113
118
protected
function
getDictionaryUri
()
119
{
120
if
(Loader::includeModule(
'bitrix24'
))
121
{
122
$lang = \CBitrix24::getLicensePrefix();
123
}
124
else
125
{
126
$lang =
$this->language
;
127
}
128
129
$baseUrl
= array_key_exists($lang, $this->baseUrl)
130
? $this->baseUrl[$lang]
131
: $this->baseUrl[
Loc::getDefaultLang
($lang)];
132
133
$uri =
new
Uri
(
$baseUrl
);
134
$uri->addParams(array(
135
'type'
=> static::ID,
136
'lng'
=> $this->language,
137
));
138
139
return
$uri;
140
}
141
}
Bitrix\Main\Application
Definition
application.php:28
Bitrix\Main\Application\getInstance
static getInstance()
Definition
application.php:95
Bitrix\Main\ArgumentException
Definition
exception.php:34
Bitrix\Main\Event
Definition
event.php:5
Bitrix\Main\Loader
Definition
loader.php:12
Bitrix\Main\Localization\Loc
Definition
loc.php:11
Bitrix\Main\Localization\Loc\getDefaultLang
static getDefaultLang($lang)
Definition
loc.php:460
Bitrix\Main\Type\Dictionary
Definition
dictionary.php:6
Bitrix\Main\Type\Dictionary\$values
$values
Definition
dictionary.php:10
Bitrix\Main\Web\HttpClient
Definition
httpclient.php:24
Bitrix\Main\Web\Json
Definition
json.php:11
Bitrix\Main\Web\Uri
Definition
uri.php:17
Bitrix\Rest\Dictionary\RemoteDictionary
Definition
remotedictionary.php:22
Bitrix\Rest\Dictionary\RemoteDictionary\ID
const ID
Definition
remotedictionary.php:23
Bitrix\Rest\Dictionary\RemoteDictionary\__construct
__construct()
Definition
remotedictionary.php:48
Bitrix\Rest\Dictionary\RemoteDictionary\getCacheId
getCacheId()
Definition
remotedictionary.php:109
Bitrix\Rest\Dictionary\RemoteDictionary\$baseUrl
$baseUrl
Definition
remotedictionary.php:28
Bitrix\Rest\Dictionary\RemoteDictionary\init
init()
Definition
remotedictionary.php:66
Bitrix\Rest\Dictionary\RemoteDictionary\load
load()
Definition
remotedictionary.php:89
Bitrix\Rest\Dictionary\RemoteDictionary\$language
$language
Definition
remotedictionary.php:46
Bitrix\Rest\Dictionary\RemoteDictionary\setLanguage
setLanguage($language)
Definition
remotedictionary.php:57
Bitrix\Rest\Dictionary\RemoteDictionary\CACHE_TTL
const CACHE_TTL
Definition
remotedictionary.php:25
Bitrix\Rest\Dictionary\RemoteDictionary\getDictionaryUri
getDictionaryUri()
Definition
remotedictionary.php:118
Bitrix\Rest\Dictionary\RemoteDictionary\CACHE_PREFIX
const CACHE_PREFIX
Definition
remotedictionary.php:26
Bitrix\Rest\Dictionary
Definition
booklet.php:9
modules
rest
lib
dictionary
remotedictionary.php
Создано системой
1.10.0