Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
webpackfile.php
1<?php
2
4
14
16{
17 protected const MODULE_ID = 'landing';
18 protected const DIR_NAME = 'assets';
19 protected const DEFAULT_NAME = 'assets_webpack';
20 protected const CORE_EXTENSION = 'ui.webpacker';
21 protected const LANG_RESOURCE = '/bitrix/js/landing/webpackassets/message_loader.js';
22
26 protected $fileController;
30 protected $landingId;
34 protected $fileId;
35
39 protected $package;
40
44 protected $profile;
45
50 protected $filename;
51
56 protected $packageHash;
57
61 protected static int $cacheTtl = 86400; // one day
62
66 public function __construct()
67 {
68 $this->fileController = new WebPacker\FileController();
69 $this->package = new WebPacker\Resource\Package();
70 $this->profile = new WebPacker\Resource\Profile();
71 }
72
77 public function setLandingId(int $lid): void
78 {
79 $this->landingId = $lid;
80 }
81
86 public function setPackageHash(string $hash): void
87 {
88 $this->packageHash = $hash;
89 }
90
95 public function setFileName(string $name): void
96 {
97 $this->filename = $name;
98 }
99
100 protected function getFileName(): string
101 {
102 if($this->packageHash)
103 {
104 $this->filename = self::DEFAULT_NAME . '_' . $this->packageHash . '_' . time() . '.js';
105 }
106 else
107 {
108 $this->filename = self::DEFAULT_NAME . '_' . Random::getString(16) . '.js';
109 }
110
111 return $this->filename;
112 }
113
117 public function addResource(string $resource): void
118 {
119 $this->package->addAsset(WebPacker\Resource\Asset::create($resource));
120 }
121
125 public function build(): void
126 {
127 $this->configureFile();
128 $this->configureResources();
129
130 // create new file
131 if (!$this->fileId)
132 {
133 $res = $this->fileController->build();
134 if ($res->isSuccess())
135 {
136 $this->fileId = $res->getId();
137 File::addToAsset($this->landingId, $this->fileId);
138
139 // tmp fixing agent for 149117
140 Agent::addUniqueAgent('checkFileExists', [$this->fileId], 86400, 60);
141 }
142 }
143 }
144
148 protected function configureFile(): void
149 {
150 if ($fileId = $this->findExistFile())
151 {
152 $this->fileId = $fileId;
153 $file = \CFile::GetByID($fileId)->Fetch();
154 $this->setFileName($file['ORIGINAL_NAME'] ?: $this->filename);
155 }
156
157 $this->fileController->configureFile(
158 $this->fileId,
159 self::MODULE_ID,
160 self::DIR_NAME,
161 $this->getFileName()
162 );
163 }
164
169 protected function findExistFile(): ?int
170 {
171 if ($this->landingId)
172 {
173 foreach(File::getFilesFromAsset($this->landingId) as $fileId)
174 {
175 if(
176 $fileId > 0
177 && $this->packageHash
178 && ($file = \CFile::GetByID($fileId)->Fetch())
179 && strpos($file['ORIGINAL_NAME'], self::DEFAULT_NAME . '_' . $this->packageHash) === 0
180 )
181 {
182 return $fileId;
183 }
184 }
185
186 return null;
187 }
188
189 // if have not landing ID - old variant, find something
190 $fileQuery = FileTable::query()
191 ->addSelect('ID')
192 ->addSelect('ORIGINAL_NAME')
193 ->where('MODULE_ID', self::MODULE_ID)
194 ->where('%ORIGINAL_NAME', self::DEFAULT_NAME)
195 ;
196 $file = $fileQuery->fetch();
197
198 return $file ? $file['ID'] : null;
199 }
200
201 public function setUseLang(): void
202 {
203 $this->profile->useAllLangs(true);
204 $this->addResource(self::LANG_RESOURCE);
205 }
206
207 protected function configureResources(): void
208 {
209 $this->fileController->addExtension(self::CORE_EXTENSION); // need core ext always
210 $this->fileController->addModule(
211 new WebPacker\Module(
212 self::DEFAULT_NAME,
213 $this->package,
214 $this->profile
215 )
216 );
217 }
218
224 public function getOutput(): string
225 {
226 return $this->fileController
227 ->getLoader()
228 ->setCacheTtl(self::$cacheTtl)
229 ->getString();
230 }
231
238 public static function markToRebuild($lid): void
239 {
240 if (!$lid || empty($lid))
241 {
242 throw new Main\ArgumentException('LID must be int or array of int', 'lid');
243 }
244
245 if (File::markAssetToRebuild($lid))
246 {
248 }
249 }
250
254 public static function markAllToRebuild(): void
255 {
257 {
259 }
260 }
261}
static addUniqueAgent(string $funcName, array $params=[], int $time=7200, ?int $nextExecDelay=null)
Definition agent.php:27
static markAssetToRebuild($assetId=[])
Definition file.php:466
static addToAsset($assetId, $fileId)
Definition file.php:426
static getFilesFromAsset($assetId)
Definition file.php:441
static clearCacheForLanding(int $lid)
Definition manager.php:1415