BundleConfig.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Deploy\Config;
  7. use Magento\Framework\View;
  8. use Magento\Framework\View\Design\Theme\ThemeProviderInterface;
  9. /**
  10. * Static files bundling configuration
  11. *
  12. * Use this to get configuration settings related to JavaScript built-in bundling
  13. */
  14. class BundleConfig
  15. {
  16. /**
  17. * Namespace of the bundling configuration
  18. */
  19. const VIEW_CONFIG_MODULE = 'Js_Bundle';
  20. /**
  21. * Name of the bundle file size configuration setting
  22. */
  23. const VIEW_CONFIG_BUNDLE_SIZE_NAME = 'bundle_size';
  24. /**
  25. * Interface provides theme configuration settings
  26. *
  27. * @var View\ConfigInterface
  28. */
  29. private $viewConfig;
  30. /**
  31. * Theme provider interface
  32. *
  33. * Allows to retrieve theme by the them full path: "{area}/{vendor}/{theme}/{locale}"
  34. *
  35. * @var ThemeProviderInterface
  36. */
  37. private $themeProvider;
  38. /**
  39. * Configuration object cache
  40. *
  41. * @var \Magento\Framework\Config\View[]
  42. */
  43. private $config = [];
  44. /**
  45. * BundleConfig constructor
  46. *
  47. * @param View\ConfigInterface $viewConfig
  48. * @param ThemeProviderInterface $themeProvider
  49. */
  50. public function __construct(
  51. View\ConfigInterface $viewConfig,
  52. ThemeProviderInterface $themeProvider
  53. ) {
  54. $this->viewConfig = $viewConfig;
  55. $this->themeProvider = $themeProvider;
  56. }
  57. /**
  58. * Max size of bundle files (in KB)
  59. *
  60. * @param string $area
  61. * @param string $theme
  62. * @return int
  63. */
  64. public function getBundleFileMaxSize($area, $theme)
  65. {
  66. $size = $this->getConfig($area, $theme)->getVarValue(
  67. self::VIEW_CONFIG_MODULE,
  68. self::VIEW_CONFIG_BUNDLE_SIZE_NAME
  69. );
  70. $unit = preg_replace('/[^a-zA-Z]+/', '', $size);
  71. $unit = strtoupper($unit);
  72. switch ($unit) {
  73. case 'KB':
  74. return (int)$size;
  75. case 'MB':
  76. return (int)$size * 1024;
  77. default:
  78. return (int)($size / 1024);
  79. }
  80. }
  81. /**
  82. * Get list of directories which must be excluded
  83. *
  84. * @param string $area
  85. * @param string $theme
  86. * @return array
  87. */
  88. public function getExcludedDirectories($area, $theme)
  89. {
  90. return $this->getConfig($area, $theme)->getExcludedDir();
  91. }
  92. /**
  93. * Get list of files which must be excluded from bundling
  94. *
  95. * @param string $area
  96. * @param string $theme
  97. * @return array
  98. */
  99. public function getExcludedFiles($area, $theme)
  100. {
  101. return $this->getConfig($area, $theme)->getExcludedFiles();
  102. }
  103. /**
  104. * Get View Configuration object related to the given area and theme
  105. *
  106. * @param string $area
  107. * @param string $theme
  108. * @return \Magento\Framework\Config\View
  109. */
  110. private function getConfig($area, $theme)
  111. {
  112. $themePath = $area . '/' . $theme;
  113. if (!isset($this->config[$themePath])) {
  114. $this->config[$themePath] = $this->viewConfig->getViewConfig([
  115. 'area' => $area,
  116. 'themeModel' => $this->themeProvider->getThemeByFullPath($themePath)
  117. ]);
  118. }
  119. return $this->config[$themePath];
  120. }
  121. }