Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
RequireJs.php
Go to the documentation of this file.
1 <?php
7 
11 use \Magento\Framework\Filesystem\File\WriteInterface;
14 
20 class RequireJs implements BundleInterface
21 {
27  private $bundleConfig;
28 
34  private $minification;
35 
41  private $staticDir;
42 
48  private $area;
49 
55  private $theme;
56 
62  private $locale;
63 
69  private $contentPools = [
70  'js' => 'jsbuild',
71  'html' => 'text'
72  ];
73 
79  private $files = [
80  'jsbuild' => [],
81  'text' => []
82  ];
83 
89  private $fileContent = [];
90 
98  private $bundleFileIndex = 0;
99 
105  private $pathToBundleDir;
106 
118  public function __construct(
120  BundleConfig $bundleConfig,
121  Minification $minification,
122  $area,
123  $theme,
124  $locale,
125  array $contentPools = []
126  ) {
127  $this->filesystem = $filesystem;
128  $this->bundleConfig = $bundleConfig;
129  $this->minification = $minification;
130  $this->staticDir = $filesystem->getDirectoryWrite(DirectoryList::STATIC_VIEW);
131  $this->area = $area;
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;
136  }
137 
141  public function addFile($filePath, $sourcePath, $contentType)
142  {
143  // all unknown content types designated to "text" pool
144  $contentPoolName = isset($this->contentPools[$contentType]) ? $this->contentPools[$contentType] : 'text';
145  $this->files[$contentPoolName][$filePath] = $sourcePath;
146  return true;
147  }
148 
152  public function flush()
153  {
154  $this->bundleFileIndex = 0;
155 
156  $bundleFile = null;
157  foreach ($this->files as $contentPoolName => $files) {
158  if (empty($files)) {
159  continue;
160  }
161  $content = [];
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) {
168  $freeSpace -= $size;
169  $content[$this->minification->addMinifiedSign($filePath)] = $fileContent;
170  } else {
171  $this->endBundleFile($bundleFile, $content);
172  $freeSpace = $this->getBundleFileMaxSize();
173  $freeSpace -= $size;
174  $content = [
175  $this->minification->addMinifiedSign($filePath) => $fileContent
176  ];
177  $bundleFile = $this->startNewBundleFile($contentPoolName);
178  }
179  }
180  $this->endBundleFile($bundleFile, $content);
181  }
182 
183  if ($bundleFile) {
184  $bundleFile->write($this->getInitJs());
185  }
186 
187  $this->files = [];
188  }
189 
193  public function clear()
194  {
195  $this->staticDir->delete($this->pathToBundleDir);
196  }
197 
204  private function startNewBundleFile($contentPoolName)
205  {
206  $bundleFile = $this->staticDir->openFile(
207  $this->minification->addMinifiedSign($this->pathToBundleDir . '/bundle' . $this->bundleFileIndex . '.js')
208  );
209  $bundleFile->write("require.config({\"config\": {\n");
210  $bundleFile->write(" \"{$contentPoolName}\":");
211  ++$this->bundleFileIndex;
212  return $bundleFile;
213  }
214 
222  private function endBundleFile(WriteInterface $bundleFile, array $contents)
223  {
224  if ($contents) {
225  $content = json_encode($contents, JSON_UNESCAPED_SLASHES);
226  $bundleFile->write("{$content}\n");
227  } else {
228  $bundleFile->write("{}\n");
229  }
230  $bundleFile->write("}});\n");
231  return true;
232  }
233 
240  private function getFileContent($sourcePath)
241  {
242  if (!isset($this->fileContent[$sourcePath])) {
243  $content = $this->staticDir->readFile($this->minification->addMinifiedSign($sourcePath));
244  if (mb_detect_encoding($content) !== "UTF-8") {
245  $content = mb_convert_encoding($content, "UTF-8");
246  }
247 
248  $this->fileContent[$sourcePath] = $content;
249  }
250  return $this->fileContent[$sourcePath];
251  }
252 
258  private function getBundleFileMaxSize()
259  {
260  return $this->bundleConfig->getBundleFileMaxSize($this->area, $this->theme);
261  }
262 
268  private function getInitJs()
269  {
270  return "require.config({\n" .
271  " bundles: {\n" .
272  " 'mage/requirejs/static': [\n" .
273  " 'jsbuild',\n" .
274  " 'buildTools',\n" .
275  " 'text',\n" .
276  " 'statistician'\n" .
277  " ]\n" .
278  " },\n" .
279  " deps: [\n" .
280  " 'jsbuild'\n" .
281  " ]\n" .
282  "});\n";
283  }
284 }
$contents
Definition: website.php:14
addFile($filePath, $sourcePath, $contentType)
Definition: RequireJs.php:141
__construct(Filesystem $filesystem, BundleConfig $bundleConfig, Minification $minification, $area, $theme, $locale, array $contentPools=[])
Definition: RequireJs.php:118
$filesystem