1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
webpackfile.php
См. документацию.
1<?php
2
3namespace Bitrix\Landing\Assets;
4
5use Bitrix\Landing\Agent;
6use Bitrix\Landing\File;
7use Bitrix\Landing\Manager;
8use Bitrix\Main;
9use Bitrix\Main\FileTable;
10use Bitrix\Main\Security\Random;
11use Bitrix\Main\Web\WebPacker;
12
17{
18 protected const MODULE_ID = 'landing';
19 protected const DIR_NAME = 'assets';
20 protected const DEFAULT_NAME = 'assets_webpack';
21 protected const CORE_EXTENSION = 'ui.webpacker';
22 protected const LANG_RESOURCE = '/bitrix/js/landing/webpackassets/message_loader.js';
23
28
32 protected int $landingId;
33
37 protected ?int $fileId = null;
38
43
48
53 protected string $filename;
54
59 protected string $packageHash;
60
64 protected static int $cacheTtl = 86400; // one day
65
69 public function __construct()
70 {
71 $this->fileController = new WebPacker\FileController();
72 $this->package = new WebPacker\Resource\Package();
73 $this->profile = new WebPacker\Resource\Profile();
74 }
75
80 public function setLandingId(int $lid): void
81 {
82 $this->landingId = $lid;
83 }
84
89 public function setPackageHash(string $hash): void
90 {
91 $this->packageHash = $hash;
92 }
93
98 public function setFileName(string $name): void
99 {
100 $this->filename = $name;
101 }
102
103 protected function getFileName(): string
104 {
105 if($this->packageHash)
106 {
107 $this->filename = self::DEFAULT_NAME . '_' . $this->packageHash . '_' . time() . '.js';
108 }
109 else
110 {
111 $this->filename = self::DEFAULT_NAME . '_' . Random::getString(16) . '.js';
112 }
113
114 return $this->filename;
115 }
116
120 public function addResource(string $resource): void
121 {
122 $this->package->addAsset(WebPacker\Resource\Asset::create($resource));
123 }
124
128 public function build(): void
129 {
130 $this->configureFile();
131 $this->configureResources();
132
133 // create new file
134 if (!$this->fileId)
135 {
136 $res = $this->fileController->build();
137 if ($res->isSuccess())
138 {
139 $this->fileId = $res->getId();
140 File::addToAsset($this->landingId, $this->fileId);
141
142 // tmp fixing agent for 149117
143 Agent::addUniqueAgent('checkFileExists', [$this->fileId], 86400, 60);
144 }
145 }
146 }
147
151 protected function configureFile(): void
152 {
153 if ($fileId = $this->findExistFile())
154 {
155 $this->fileId = $fileId;
156 $file = \CFile::GetByID($fileId)->Fetch();
157 $this->setFileName($file['ORIGINAL_NAME'] ?: $this->filename);
158 }
159
160 $this->fileController->configureFile(
161 $this->fileId,
162 self::MODULE_ID,
163 self::DIR_NAME,
164 $this->getFileName()
165 );
166 }
167
172 protected function findExistFile(): ?int
173 {
174 if ($this->landingId)
175 {
176 foreach(File::getFilesFromAsset($this->landingId) as $fileId)
177 {
178 if(
179 $fileId > 0
180 && $this->packageHash
181 && ($file = \CFile::GetByID($fileId)->Fetch())
182 && strpos($file['ORIGINAL_NAME'], self::DEFAULT_NAME . '_' . $this->packageHash) === 0
183 )
184 {
185 return $fileId;
186 }
187 }
188
189 return null;
190 }
191
192 // if have not landing ID - old variant, find something
193 $fileQuery = FileTable::query()
194 ->addSelect('ID')
195 ->addSelect('ORIGINAL_NAME')
196 ->where('MODULE_ID', self::MODULE_ID)
197 ->where('%ORIGINAL_NAME', self::DEFAULT_NAME)
198 ;
199 $file = $fileQuery->fetch();
200
201 return $file ? $file['ID'] : null;
202 }
203
204 public function setUseLang(): void
205 {
206 $this->profile->useAllLangs(true);
207 $this->addResource(self::LANG_RESOURCE);
208 }
209
210 protected function configureResources(): void
211 {
212 $this->fileController->addExtension(self::CORE_EXTENSION); // need core ext always
213 $this->fileController->addModule(
214 new WebPacker\Module(
215 self::DEFAULT_NAME,
216 $this->package,
217 $this->profile
218 )
219 );
220 }
221
227 public function getOutput(): string
228 {
229 return $this->fileController
230 ->getLoader()
231 ->setCacheTtl(self::$cacheTtl)
232 ->getString();
233 }
234
241 public static function markToRebuild($lid): void
242 {
243 if (!$lid || empty($lid))
244 {
245 throw new Main\ArgumentException('LID must be int or array of int', 'lid');
246 }
247
248 if (File::markAssetToRebuild($lid))
249 {
251 }
252 }
253
257 public static function markAllToRebuild(): void
258 {
260 {
262 }
263 }
264}
$hash
Определения ajax_redirector.php:8
static addUniqueAgent(string $funcName, array $params=[], int $time=7200, ?int $nextExecDelay=null)
Определения agent.php:28
static markToRebuild($lid)
Определения webpackfile.php:241
WebPacker FileController $fileController
Определения webpackfile.php:27
addResource(string $resource)
Определения webpackfile.php:120
WebPacker Resource Profile $profile
Определения webpackfile.php:47
setPackageHash(string $hash)
Определения webpackfile.php:89
setFileName(string $name)
Определения webpackfile.php:98
setLandingId(int $lid)
Определения webpackfile.php:80
static int $cacheTtl
Определения webpackfile.php:64
WebPacker Resource Package $package
Определения webpackfile.php:42
static markAllToRebuild()
Определения webpackfile.php:257
static markAssetToRebuild($assetId=[])
Определения file.php:466
static addToAsset($assetId, $fileId)
Определения file.php:426
static getFilesFromAsset($assetId)
Определения file.php:441
static clearCacheForLanding(int $lid)
Определения manager.php:1473
static clearCache()
Определения manager.php:1435
$res
Определения filter_act.php:7
$name
Определения menu_edit.php:35