Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
streamconverter.php
1<?php declare(strict_types = 1);
2
4
6
7
8class StreamConverter extends \php_user_filter
9{
10 public const FILTER_IDENTIFIER = 'bx.localization.converter';
11
13 protected $code = '';
14
28 public function filter($in, $out, &$consumed, $closing): int
29 {
30 while ($bucket = stream_bucket_make_writeable($in))
31 {
32 $this->code .= $bucket->data;
33 $consumed += $bucket->datalen;
34 }
35
36 if ($closing || feof($this->stream))
37 {
38 $params = explode('/', str_replace(self::FILTER_IDENTIFIER.'.', '', $this->filtername));
39 $sourceEncoding = $params[0];
40 $targetEncoding = $params[1];
41
42 if ($sourceEncoding != $targetEncoding)
43 {
44 $this->code = Encoding::convertEncoding($this->code, $sourceEncoding, $targetEncoding);
45 }
46
47 $bucket = stream_bucket_new($this->stream, $this->code);
48 stream_bucket_append($out, $bucket);
49
50 return PSFS_PASS_ON;
51 }
52
53 return PSFS_FEED_ME;
54 }
55
60 public static function register(): bool
61 {
62 return stream_filter_register(
63 self::FILTER_IDENTIFIER.'.*',
64 self::class
65 );
66 }
67
78 public static function include(string $langPath, string $lang, string $targetEncoding = '', string $sourceEncoding = '')
79 {
81
82 if (empty($targetEncoding) || empty($sourceEncoding))
83 {
84 $checkPath = Translation::convertLangPath($langPath, $lang);
85
86 [, $checkTargetEncoding, $checkSourceEncoding] = Translation::getEncodings($lang, $checkPath);
87 if (empty($targetEncoding))
88 {
89 $targetEncoding = $checkTargetEncoding;
90 }
91 if (empty($sourceEncoding))
92 {
93 $sourceEncoding = $checkSourceEncoding;
94 }
95 }
96
97 return include (
98 'php://filter/read='.self::FILTER_IDENTIFIER.
99 ".{$sourceEncoding}%2F{$targetEncoding}".
100 '/resource='.$langPath
101 );
102 }
103}
filter($in, $out, &$consumed, $closing)
static convertLangPath($langFile, $language)
static getEncodings($language, $langFile)