11 use \Magento\Framework\Filesystem\File\WriteInterface;
27 private $bundleConfig;
34 private $minification;
69 private $contentPools = [
89 private $fileContent = [];
98 private $bundleFileIndex = 0;
105 private $pathToBundleDir;
125 array $contentPools = []
128 $this->bundleConfig = $bundleConfig;
129 $this->minification = $minification;
132 $this->theme = $theme;
133 $this->locale = $locale;
134 $this->contentPools = array_merge($this->contentPools, $contentPools);
135 $this->pathToBundleDir = $this->area .
'/' . $this->theme .
'/' . $this->locale .
'/' .
self::BUNDLE_JS_DIR;
141 public function addFile($filePath, $sourcePath, $contentType)
144 $contentPoolName = isset($this->contentPools[$contentType]) ? $this->contentPools[$contentType] :
'text';
145 $this->files[$contentPoolName][$filePath] = $sourcePath;
154 $this->bundleFileIndex = 0;
157 foreach ($this->files as $contentPoolName => $files) {
162 $freeSpace = $this->getBundleFileMaxSize();
163 $bundleFile = $this->startNewBundleFile($contentPoolName);
164 foreach ($files as $filePath => $sourcePath) {
165 $fileContent = $this->getFileContent($sourcePath);
166 $size = mb_strlen($fileContent,
'utf-8') / 1024;
167 if ($freeSpace > $size) {
169 $content[$this->minification->addMinifiedSign($filePath)] = $fileContent;
171 $this->endBundleFile($bundleFile,
$content);
172 $freeSpace = $this->getBundleFileMaxSize();
175 $this->minification->addMinifiedSign($filePath) => $fileContent
177 $bundleFile = $this->startNewBundleFile($contentPoolName);
180 $this->endBundleFile($bundleFile,
$content);
184 $bundleFile->write($this->getInitJs());
195 $this->staticDir->delete($this->pathToBundleDir);
204 private function startNewBundleFile($contentPoolName)
206 $bundleFile = $this->staticDir->openFile(
207 $this->minification->addMinifiedSign($this->pathToBundleDir .
'/bundle' . $this->bundleFileIndex .
'.js')
209 $bundleFile->write(
"require.config({\"config\": {\n");
210 $bundleFile->write(
" \"{$contentPoolName}\":");
211 ++$this->bundleFileIndex;
226 $bundleFile->write(
"{$content}\n");
228 $bundleFile->write(
"{}\n");
230 $bundleFile->write(
"}});\n");
240 private function getFileContent($sourcePath)
242 if (!isset($this->fileContent[$sourcePath])) {
243 $content = $this->staticDir->readFile($this->minification->addMinifiedSign($sourcePath));
244 if (mb_detect_encoding(
$content) !==
"UTF-8") {
248 $this->fileContent[$sourcePath] =
$content;
250 return $this->fileContent[$sourcePath];
258 private function getBundleFileMaxSize()
260 return $this->bundleConfig->getBundleFileMaxSize($this->area, $this->theme);
268 private function getInitJs()
270 return "require.config({\n" .
272 " 'mage/requirejs/static': [\n" .
276 " 'statistician'\n" .
addFile($filePath, $sourcePath, $contentType)
__construct(Filesystem $filesystem, BundleConfig $bundleConfig, Minification $minification, $area, $theme, $locale, array $contentPools=[])