1 | <?php |
||||
2 | |||||
3 | namespace Alex19pov31\BitrixHelper; |
||||
4 | |||||
5 | class ComponentHelper |
||||
6 | { |
||||
7 | private static $componentStack; |
||||
8 | |||||
9 | /** |
||||
10 | * Название компонента |
||||
11 | * |
||||
12 | * @var string |
||||
13 | */ |
||||
14 | private $name; |
||||
15 | |||||
16 | /** |
||||
17 | * Шаблон компонента |
||||
18 | * |
||||
19 | * @var string |
||||
20 | */ |
||||
21 | private $template; |
||||
22 | |||||
23 | /** |
||||
24 | * Параметры компонента |
||||
25 | * |
||||
26 | * @var array |
||||
27 | */ |
||||
28 | private $params; |
||||
29 | |||||
30 | /** |
||||
31 | * Параметры родительского компонента |
||||
32 | * |
||||
33 | * @var array|null |
||||
34 | */ |
||||
35 | private $parentComponent; |
||||
36 | |||||
37 | /** |
||||
38 | * Undocumented variable |
||||
39 | * |
||||
40 | * @var array |
||||
41 | */ |
||||
42 | private $functionParams; |
||||
43 | |||||
44 | /** |
||||
45 | * Данные возвращаемые компонентом |
||||
46 | * |
||||
47 | * @var mixed |
||||
48 | */ |
||||
49 | private $returnedData; |
||||
50 | |||||
51 | /** |
||||
52 | * Данные для вывода компонента |
||||
53 | * |
||||
54 | * @var string |
||||
55 | */ |
||||
56 | private $output; |
||||
57 | |||||
58 | /** |
||||
59 | * Время кеширования |
||||
60 | * |
||||
61 | * @var int |
||||
62 | */ |
||||
63 | private $ttl = 0; |
||||
64 | |||||
65 | private $app; |
||||
66 | |||||
67 | /** |
||||
68 | * Ключ кеширования |
||||
69 | * |
||||
70 | * @var string|null |
||||
71 | */ |
||||
72 | private $cachekey = null; |
||||
73 | |||||
74 | /** |
||||
75 | * Список вызванных компонентов |
||||
76 | * |
||||
77 | * @return array|null |
||||
78 | */ |
||||
79 | public static function getStack() |
||||
80 | { |
||||
81 | return static::$componentStack; |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
82 | } |
||||
83 | |||||
84 | /** |
||||
85 | * Возвращает компонент из стека по ключу |
||||
86 | * |
||||
87 | * @param string $name |
||||
88 | * @return ComponentHelper|null |
||||
89 | */ |
||||
90 | public static function getByNameInStack(string $name) |
||||
91 | { |
||||
92 | $component = static::$componentStack[$name]; |
||||
0 ignored issues
–
show
|
|||||
93 | return !empty($component) ? $component : null; |
||||
94 | } |
||||
95 | |||||
96 | public function __construct(string $name, string $template = '', array $params = [], $parentComponent = null, $functionParams = [], $app = null) |
||||
97 | { |
||||
98 | $this->name = $name; |
||||
99 | $this->app = !is_null($app) ? $app : bxApp(); |
||||
100 | |||||
101 | $this->setTemplate($template); |
||||
102 | $this->setParams($params); |
||||
103 | $this->setParentComponent($parentComponent); |
||||
104 | $this->setFunctionParams($functionParams); |
||||
105 | } |
||||
106 | |||||
107 | public function getReturnedData() |
||||
108 | { |
||||
109 | return $this->returnedData; |
||||
110 | } |
||||
111 | |||||
112 | /** |
||||
113 | * Название компонента |
||||
114 | * |
||||
115 | * @return string |
||||
116 | */ |
||||
117 | public function getName(): string |
||||
118 | { |
||||
119 | return (string) $this->name; |
||||
120 | } |
||||
121 | |||||
122 | /** |
||||
123 | * Установить шаблон компонента |
||||
124 | * |
||||
125 | * @param string $template |
||||
126 | * @return ComponentHelper |
||||
127 | */ |
||||
128 | public function setTemplate(string $template = ''): ComponentHelper |
||||
129 | { |
||||
130 | $this->template = $template; |
||||
131 | return $this; |
||||
132 | } |
||||
133 | |||||
134 | /** |
||||
135 | * Шаблон компонента |
||||
136 | * |
||||
137 | * @return string |
||||
138 | */ |
||||
139 | public function getTemplate(): string |
||||
140 | { |
||||
141 | return (string) $this->template; |
||||
142 | } |
||||
143 | |||||
144 | /** |
||||
145 | * Указать ключ компонента в стеке |
||||
146 | * |
||||
147 | * @param string $name |
||||
148 | * @return ComponentHelper |
||||
149 | */ |
||||
150 | public function setNameInStack(string $name): ComponentHelper |
||||
151 | { |
||||
152 | static::$componentStack[$name] = &$this; |
||||
0 ignored issues
–
show
|
|||||
153 | return $this; |
||||
154 | } |
||||
155 | |||||
156 | /** |
||||
157 | * Установить параметры родительского компонента |
||||
158 | * |
||||
159 | * @param array|null $parentComponent |
||||
160 | * @return ComponentHelper |
||||
161 | */ |
||||
162 | public function setParentComponent($parentComponent): ComponentHelper |
||||
163 | { |
||||
164 | $this->parentComponent = $parentComponent; |
||||
165 | return $this; |
||||
166 | } |
||||
167 | |||||
168 | /** |
||||
169 | * Параметры родительского компонента |
||||
170 | * |
||||
171 | * @return array|null |
||||
172 | */ |
||||
173 | public function getParentComponent() |
||||
174 | { |
||||
175 | return $this->parentComponent; |
||||
176 | } |
||||
177 | |||||
178 | public function setFunctionParams(array $functionParams): ComponentHelper |
||||
179 | { |
||||
180 | $this->functionParams = $functionParams; |
||||
181 | return $this; |
||||
182 | } |
||||
183 | |||||
184 | public function getFunctionParams(): array |
||||
185 | { |
||||
186 | return (array) $this->functionParams; |
||||
187 | } |
||||
188 | |||||
189 | /** |
||||
190 | * Установить параметры компонента |
||||
191 | * |
||||
192 | * @param array $params |
||||
193 | * @return ComponentHelper |
||||
194 | */ |
||||
195 | public function setParams(array $params): ComponentHelper |
||||
196 | { |
||||
197 | $this->params = $params; |
||||
198 | return $this; |
||||
199 | } |
||||
200 | |||||
201 | /** |
||||
202 | * Параметры компонета |
||||
203 | * |
||||
204 | * @return array |
||||
205 | */ |
||||
206 | public function getParams(): array |
||||
207 | { |
||||
208 | return (array) $this->params; |
||||
209 | } |
||||
210 | |||||
211 | /** |
||||
212 | * Установить параметр компонента |
||||
213 | * |
||||
214 | * @param string $name |
||||
215 | * @param mixed $value |
||||
216 | * @return ComponentHelper |
||||
217 | */ |
||||
218 | public function setParam(string $name, $value): ComponentHelper |
||||
219 | { |
||||
220 | $this->params[$name] = $value; |
||||
221 | return $this; |
||||
222 | } |
||||
223 | |||||
224 | private function executeWithoutCache(): string |
||||
225 | { |
||||
226 | ob_start(); |
||||
227 | $this->returnedData = $this->app->IncludeComponent( |
||||
228 | $this->name, |
||||
229 | $this->template, |
||||
230 | $this->params, |
||||
231 | $this->parentComponent, |
||||
232 | $this->functionParams |
||||
233 | ); |
||||
234 | return ob_get_clean(); |
||||
235 | } |
||||
236 | |||||
237 | public function execute(): string |
||||
238 | { |
||||
239 | if ($this->ttl == 0) { |
||||
240 | return $this->executeWithoutCache(); |
||||
241 | } |
||||
242 | |||||
243 | return $this->output = cache($this->ttl, $this->getCacheKey(), '/components', 'cache', function () { |
||||
244 | return $this->executeWithoutCache(); |
||||
245 | }); |
||||
246 | } |
||||
247 | |||||
248 | /** |
||||
249 | * Вывод компонента |
||||
250 | * |
||||
251 | * @return void |
||||
252 | */ |
||||
253 | public function show(bool $cachedOutput = false) |
||||
254 | { |
||||
255 | if ($cachedOutput && !is_null($this->output)) { |
||||
256 | echo $this->output; |
||||
257 | } |
||||
258 | |||||
259 | echo $this->execute(); |
||||
260 | static::$componentStack[] = $this; |
||||
0 ignored issues
–
show
|
|||||
261 | } |
||||
262 | |||||
263 | public function getCacheKey(): string |
||||
264 | { |
||||
265 | if (!is_null($this->cachekey)) { |
||||
266 | return (string) $this->cachekey; |
||||
267 | } |
||||
268 | |||||
269 | $dataKey = [ |
||||
270 | $this->name, |
||||
271 | $this->template, |
||||
272 | $this->params, |
||||
273 | $this->parentComponent, |
||||
274 | $this->functionParams, |
||||
275 | ]; |
||||
276 | |||||
277 | return md5(json_encode($dataKey)); |
||||
278 | } |
||||
279 | |||||
280 | public function cache(int $minutes, $key = null) |
||||
0 ignored issues
–
show
The parameter
$key is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() |
|||||
281 | { |
||||
282 | $this->ttl = $minutes * 60; |
||||
283 | $this->cacheKey = null; |
||||
0 ignored issues
–
show
|
|||||
284 | return $this; |
||||
285 | } |
||||
286 | } |
||||
287 |