Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
component.php
1<?php
3
8use Bitrix\Main\Page\Asset;
11
16final class Component extends Json implements Errorable
17{
18 const STATUS_SUCCESS = 'success';
19 const STATUS_DENIED = 'denied';
20 const STATUS_ERROR = 'error';
21
22 private $jsPathList = [];
23 private $cssPathList = [];
24 private $asset;
25
29 private $status;
30
34 private $errorCollection;
35
47 public function __construct($componentName, $templateName = '', $params = [], $additionalResponseParams = [], $parentComponent = [], $functionParams = [], $status = self::STATUS_SUCCESS, ErrorCollection $errorCollection = null)
48 {
49 $this->asset = Asset::getInstance();
50
51 // Temporary fix
52 $this->asset->disableOptimizeCss();
53 $this->asset->disableOptimizeJs();
54
55 $this->setHeaders(new HttpHeaders());
56 global $APPLICATION;
57 ob_start();
58 $APPLICATION->IncludeComponent(
59 $componentName,
60 $templateName,
61 $params,
62 $parentComponent,
63 $functionParams
64 );
65 $componentContent = ob_get_clean();
66
67 $this->status = $status?: self::STATUS_SUCCESS;
68 $this->errorCollection = $errorCollection?: new ErrorCollection;
69
70 $this->collectAssetsPathList();
71 $this->setData([
72 'status' => $this->status,
73 'data' => $componentContent,
74 'assets' => [
75 'js' => $this->getJsList(),
76 'css' => $this->getCssList(),
77 'string' => $this->getStringList()
78 ],
79 'additionalParams' => $additionalResponseParams,
80 'errors' => $this->getErrorsToResponse(),
81 ]);
82 }
83
84 private function collectAssetsPathList()
85 {
86 $this->asset->getCss();
87 $this->asset->getJs();
88 $this->asset->getStrings();
89
90 $this->jsPathList = $this->asset->getTargetList('JS');
91 $this->cssPathList = $this->asset->getTargetList('CSS');
92 }
93
97 private function getJsList()
98 {
99 $jsList = [];
100
101 foreach($this->jsPathList as $targetAsset)
102 {
103 $assetInfo = $this->asset->getAssetInfo($targetAsset['NAME'], AssetMode::ALL);
104 if (!empty($assetInfo['JS']))
105 {
106 $jsList = array_merge($jsList, $assetInfo['JS']);
107 }
108 }
109
110 return $jsList;
111 }
112
116 private function getCssList()
117 {
118 $cssList = [];
119
120 foreach($this->cssPathList as $targetAsset)
121 {
122 $assetInfo = $this->asset->getAssetInfo($targetAsset['NAME'], AssetMode::ALL);
123 if (!empty($assetInfo['CSS']))
124 {
125 $cssList = array_merge($cssList, $assetInfo['CSS']);
126 }
127 }
128
129 return $cssList;
130 }
131
135 private function getStringList()
136 {
137 $strings = [];
138 foreach($this->cssPathList as $targetAsset)
139 {
140 $assetInfo = $this->asset->getAssetInfo($targetAsset['NAME'], AssetMode::ALL);
141 if (!empty($assetInfo['STRINGS']))
142 {
143 $strings = array_merge($strings, $assetInfo['STRINGS']);
144 }
145 }
146
147 foreach($this->jsPathList as $targetAsset)
148 {
149 $assetInfo = $this->asset->getAssetInfo($targetAsset['NAME'], AssetMode::ALL);
150 if (!empty($assetInfo['STRINGS']))
151 {
152 $strings = array_merge($strings, $assetInfo['STRINGS']);
153 }
154 }
155
156 $strings[] = $this->asset->showFilesList();
157 return $strings;
158 }
159
163 protected function getErrorsToResponse()
164 {
165 $errors = [];
166 foreach ($this->errorCollection as $error)
167 {
169 $errors[] = [
170 'message' => $error->getMessage(),
171 'code' => $error->getCode(),
172 ];
173 }
174
175 return $errors;
176 }
177
182 public function getErrors()
183 {
184 return $this->errorCollection->toArray();
185 }
186
192 public function getErrorByCode($code)
193 {
194 return $this->errorCollection->getErrorByCode($code);
195 }
196}
setHeaders(Web\HttpHeaders $headers)
__construct($componentName, $templateName='', $params=[], $additionalResponseParams=[], $parentComponent=[], $functionParams=[], $status=self::STATUS_SUCCESS, ErrorCollection $errorCollection=null)
Definition component.php:47