BundleConfig.php 3.28 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Deploy\Config;

use Magento\Framework\View;
use Magento\Framework\View\Design\Theme\ThemeProviderInterface;

/**
 * Static files bundling configuration
 *
 * Use this to get configuration settings related to JavaScript built-in bundling
 */
class BundleConfig
{
    /**
     * Namespace of the bundling configuration
     */
    const VIEW_CONFIG_MODULE = 'Js_Bundle';

    /**
     * Name of the bundle file size configuration setting
     */
    const VIEW_CONFIG_BUNDLE_SIZE_NAME = 'bundle_size';

    /**
     * Interface provides theme configuration settings
     *
     * @var View\ConfigInterface
     */
    private $viewConfig;

    /**
     * Theme provider interface
     *
     * Allows to retrieve theme by the them full path: "{area}/{vendor}/{theme}/{locale}"
     *
     * @var ThemeProviderInterface
     */
    private $themeProvider;

    /**
     * Configuration object cache
     *
     * @var \Magento\Framework\Config\View[]
     */
    private $config = [];

    /**
     * BundleConfig constructor
     *
     * @param View\ConfigInterface $viewConfig
     * @param ThemeProviderInterface $themeProvider
     */
    public function __construct(
        View\ConfigInterface $viewConfig,
        ThemeProviderInterface $themeProvider
    ) {
        $this->viewConfig = $viewConfig;
        $this->themeProvider = $themeProvider;
    }

    /**
     * Max size of bundle files (in KB)
     *
     * @param string $area
     * @param string $theme
     * @return int
     */
    public function getBundleFileMaxSize($area, $theme)
    {
        $size = $this->getConfig($area, $theme)->getVarValue(
            self::VIEW_CONFIG_MODULE,
            self::VIEW_CONFIG_BUNDLE_SIZE_NAME
        );
        $unit = preg_replace('/[^a-zA-Z]+/', '', $size);
        $unit = strtoupper($unit);
        switch ($unit) {
            case 'KB':
                return (int)$size;
            case 'MB':
                return (int)$size * 1024;
            default:
                return (int)($size / 1024);
        }
    }

    /**
     * Get list of directories which must be excluded
     *
     * @param string $area
     * @param string $theme
     * @return array
     */
    public function getExcludedDirectories($area, $theme)
    {
        return $this->getConfig($area, $theme)->getExcludedDir();
    }

    /**
     * Get list of files which must be excluded from bundling
     *
     * @param string $area
     * @param string $theme
     * @return array
     */
    public function getExcludedFiles($area, $theme)
    {
        return $this->getConfig($area, $theme)->getExcludedFiles();
    }

    /**
     * Get View Configuration object related to the given area and theme
     *
     * @param string $area
     * @param string $theme
     * @return \Magento\Framework\Config\View
     */
    private function getConfig($area, $theme)
    {
        $themePath = $area . '/' . $theme;
        if (!isset($this->config[$themePath])) {
            $this->config[$themePath] = $this->viewConfig->getViewConfig([
                'area' => $area,
                'themeModel' => $this->themeProvider->getThemeByFullPath($themePath)
            ]);
        }
        return $this->config[$themePath];
    }
}