Options.php 4.19 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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
declare(strict_types=1);

namespace Magento\Framework\Locale\Deployed;

use Magento\Framework\App\DeploymentConfig;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\App\State;
use Magento\Framework\Config\ConfigOptionsListConstants as Constants;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Locale\AvailableLocalesInterface;
use Magento\Framework\Locale\ListsInterface;
use Magento\Framework\Locale\OptionInterface;
use Magento\Framework\View\DesignInterface;

/**
 * Returns options array of locales that have deployed static content.
 */
class Options implements OptionInterface
{
    /**
     * Application state class.
     *
     * @var State
     */
    private $state;

    /**
     * Operates with available locales.
     *
     * @var AvailableLocalesInterface
     */
    private $availableLocales;

    /**
     * Operates with magento design settings.
     *
     * @var DesignInterface
     */
    private $design;

    /**
     * Locales lists.
     *
     * @var ListsInterface
     */
    private $localeLists;

    /**
     * @var DeploymentConfig
     */
    private $deploymentConfig;

    /**
     * @param ListsInterface $localeLists locales list
     * @param State $state application state class
     * @param AvailableLocalesInterface $availableLocales operates with available locales
     * @param DesignInterface $design operates with magento design settings
     * @param DeploymentConfig $deploymentConfig
     */
    public function __construct(
        ListsInterface $localeLists,
        State $state,
        AvailableLocalesInterface $availableLocales,
        DesignInterface $design,
        DeploymentConfig $deploymentConfig = null
    ) {
        $this->localeLists = $localeLists;
        $this->state = $state;
        $this->availableLocales = $availableLocales;
        $this->design = $design;
        $this->deploymentConfig = $deploymentConfig ?: ObjectManager::getInstance()->get(DeploymentConfig::class);
    }

    /**
     * {@inheritdoc}
     */
    public function getOptionLocales(): array
    {
        return $this->filterLocales($this->localeLists->getOptionLocales());
    }

    /**
     * {@inheritdoc}
     */
    public function getTranslatedOptionLocales(): array
    {
        return $this->filterLocales($this->localeLists->getTranslatedOptionLocales());
    }

    /**
     * Filter list of locales by available locales for current theme and depends on running application mode.
     *
     * Applies filters only in production mode when flag 'static_content_on_demand_in_production' is not enabled.
     * For example, if the current design theme has only one generated locale en_GB then for given array of locales:
     * ```php
     *     $locales = [
     *        0 => [
     *           'value' => 'da_DK'
     *           'label' => 'Danish (Denmark)'
     *        ],
     *        1 => [
     *           'value' => 'de_DE'
     *           'label' => 'German (Germany)'
     *        ],
     *        2 => [
     *           'value' => 'en_GB'
     *           'label' => 'English (United Kingdom)'
     *        ],
     *    ]
     * ```
     * result will be:
     * ```php
     *    [
     *        2 => [
     *           'value' => 'en_GB'
     *           'label' => 'English (United Kingdom)'
     *        ],
     *    ]
     * ```
     *
     * @param array $locales list of locales for filtering
     * @return array of filtered locales
     */
    private function filterLocales(array $locales): array
    {
        if ($this->state->getMode() != State::MODE_PRODUCTION
            || $this->deploymentConfig->getConfigData(Constants::CONFIG_PATH_SCD_ON_DEMAND_IN_PRODUCTION)) {
            return $locales;
        }

        $theme = $this->design->getDesignTheme();
        try {
            $availableLocales = $this->availableLocales->getList($theme->getCode(), $theme->getArea());
        } catch (LocalizedException $e) {
            $availableLocales = [];
        }

        return array_filter($locales, function ($localeData) use ($availableLocales) {
            return in_array($localeData['value'], $availableLocales);
        });
    }
}