Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Bundle.php
Go to the documentation of this file.
1 <?php
8 
13 
19 class Bundle
20 {
24  protected $assets = [];
25 
29  protected $assetsContent = [];
30 
34  protected $bundleConfig;
35 
39  protected $bundleNames = [
40  Manager::ASSET_TYPE_JS => 'jsbuild',
41  Manager::ASSET_TYPE_HTML => 'text'
42  ];
43 
47  protected $content = [];
48 
52  protected $minification;
53 
59  public function __construct(
63  ) {
64  $this->filesystem = $filesystem;
65  $this->bundleConfig = $bundleConfig;
66  $this->minification = $minification;
67  }
68 
73  public function addAsset(LocalInterface $asset)
74  {
75  $this->init($asset);
76  $this->add($asset);
77  }
78 
85  protected function add(LocalInterface $asset)
86  {
87  $partIndex = $this->getPartIndex($asset);
88  $parts = &$this->assets[$this->getContextCode($asset)][$asset->getContentType()];
89  if (!isset($parts[$partIndex])) {
90  $parts[$partIndex]['assets'] = [];
91  }
92  $parts[$partIndex]['assets'][$this->getAssetKey($asset)] = $asset;
93  }
94 
99  protected function init(LocalInterface $asset)
100  {
101  $contextCode = $this->getContextCode($asset);
102  $type = $asset->getContentType();
103 
104  if (!isset($this->assets[$contextCode][$type])) {
105  $this->assets[$contextCode][$type] = [];
106  }
107  }
108 
113  protected function getContextCode(LocalInterface $asset)
114  {
116  $context = $asset->getContext();
117  return $context->getAreaCode() . ':' . $context->getThemePath() . ':' . $context->getLocale();
118  }
119 
124  protected function getPartIndex(LocalInterface $asset)
125  {
126  $parts = $this->assets[$this->getContextCode($asset)][$asset->getContentType()];
127 
128  $maxPartSize = $this->getMaxPartSize($asset);
129  $minSpace = $maxPartSize;
130  $minIndex = -1;
131  if ($maxPartSize && count($parts)) {
132  foreach ($parts as $partIndex => $part) {
133  $space = $maxPartSize - $this->getSizePartWithNewAsset($asset, $part['assets']);
134  if ($space >= 0 && $space < $minSpace) {
135  $minSpace = $space;
136  $minIndex = $partIndex;
137  }
138  }
139  }
140 
141  return ($maxPartSize != 0) ? ($minIndex >= 0) ? $minIndex : count($parts) : 0;
142  }
143 
148  protected function getMaxPartSize(LocalInterface $asset)
149  {
150  return $this->bundleConfig->getPartSize($asset->getContext());
151  }
152 
160  protected function getSizePartWithNewAsset(LocalInterface $asset, $assets = [])
161  {
162  $assets[$this->getAssetKey($asset)] = $asset;
163  return mb_strlen($this->getPartContent($assets), 'utf-8') / 1024;
164  }
165 
172  protected function getAssetKey(LocalInterface $asset)
173  {
174  $result = (($asset->getModule() == '') ? '' : $asset->getModule() . '/') . $asset->getFilePath();
175  $result = $this->minification->addMinifiedSign($result);
176  return $result;
177  }
178 
185  protected function getPartContent($assets)
186  {
187  $contents = [];
188  foreach ($assets as $key => $asset) {
189  $contents[$key] = $this->getAssetContent($asset);
190  }
191 
192  $partType = reset($assets)->getContentType();
193  $content = json_encode($contents, JSON_UNESCAPED_SLASHES);
194  $content = "require.config({\n" .
195  " config: {\n" .
196  " '" . $this->bundleNames[$partType] . "':" . $content . "\n" .
197  " }\n" .
198  "});\n";
199 
200  return $content;
201  }
202 
209  protected function getAssetContent(LocalInterface $asset)
210  {
211  $assetContextCode = $this->getContextCode($asset);
212  $assetContentType = $asset->getContentType();
213  $assetKey = $this->getAssetKey($asset);
214  if (!isset($this->assetsContent[$assetContextCode][$assetContentType][$assetKey])) {
215  $content = $asset->getContent();
216  if (mb_detect_encoding($content) !== "UTF-8") {
217  $content = mb_convert_encoding($content, "UTF-8");
218  }
219  $this->assetsContent[$assetContextCode][$assetContentType][$assetKey] = $content;
220  }
221 
222  return $this->assetsContent[$assetContextCode][$assetContentType][$assetKey];
223  }
224 
228  protected function getInitJs()
229  {
230  return "require.config({\n" .
231  " bundles: {\n" .
232  " 'mage/requirejs/static': [\n" .
233  " 'jsbuild',\n" .
234  " 'buildTools',\n" .
235  " 'text',\n" .
236  " 'statistician'\n" .
237  " ]\n" .
238  " },\n" .
239  " deps: [\n" .
240  " 'jsbuild'\n" .
241  " ]\n" .
242  "});\n";
243  }
244 
248  public function flush()
249  {
250  foreach ($this->assets as $types) {
251  $this->save($types);
252  }
253  $this->assets = [];
254  $this->content = [];
255  $this->assetsContent = [];
256  }
257 
262  protected function save($types)
263  {
264  $dir = $this->filesystem->getDirectoryWrite(DirectoryList::STATIC_VIEW);
265 
266  $bundlePath = '';
267  foreach ($types as $parts) {
269  $assetsParts = reset($parts);
270  $context = reset($assetsParts['assets'])->getContext();
271  $bundlePath = empty($bundlePath) ? $context->getPath() . Manager::BUNDLE_PATH : $bundlePath;
272  $dir->delete($context->getPath() . DIRECTORY_SEPARATOR . Manager::BUNDLE_JS_DIR);
273  $this->fillContent($parts, $context);
274  }
275 
276  $this->content[max(0, count($this->content) - 1)] .= $this->getInitJs();
277 
278  foreach ($this->content as $partIndex => $content) {
279  $dir->writeFile($this->minification->addMinifiedSign($bundlePath . $partIndex . '.js'), $content);
280  }
281  }
282 
288  protected function fillContent($parts, $context)
289  {
290  $index = count($this->content) > 0 ? count($this->content) - 1 : 0 ;
291  foreach ($parts as $part) {
292  if (!isset($this->content[$index])) {
293  $this->content[$index] = '';
294  } elseif ($this->bundleConfig->isSplit($context)) {
295  ++$index;
296  $this->content[$index] = '';
297  }
298  $this->content[$index] .= $this->getPartContent($part['assets']);
299  }
300  }
301 }
getAssetContent(LocalInterface $asset)
Definition: Bundle.php:209
$contents
Definition: website.php:14
addAsset(LocalInterface $asset)
Definition: Bundle.php:73
add(LocalInterface $asset)
Definition: Bundle.php:85
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
getSizePartWithNewAsset(LocalInterface $asset, $assets=[])
Definition: Bundle.php:160
getMaxPartSize(LocalInterface $asset)
Definition: Bundle.php:148
fillContent($parts, $context)
Definition: Bundle.php:288
$type
Definition: item.phtml:13
init(LocalInterface $asset)
Definition: Bundle.php:99
getAssetKey(LocalInterface $asset)
Definition: Bundle.php:172
__construct(Filesystem $filesystem, Bundle\ConfigInterface $bundleConfig, Minification $minification)
Definition: Bundle.php:59
$filesystem
$index
Definition: list.phtml:44
getPartIndex(LocalInterface $asset)
Definition: Bundle.php:124