ConfigPathResolver.php 2.08 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
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Framework\App\Config;

use Magento\Store\Model\ScopeInterface;

/**
 * Configures full path for configurations, including scope data and configuration type.
 */
class ConfigPathResolver
{
    /**
     * @var ScopeCodeResolver
     */
    private $scopeCodeResolver;

    /**
     * @param ScopeCodeResolver $scopeCodeResolver
     */
    public function __construct(ScopeCodeResolver $scopeCodeResolver)
    {
        $this->scopeCodeResolver = $scopeCodeResolver;
    }

    /**
     * Creates full config path for given params.
     * If $type variable was provided, it will be used as first part of path.
     *
     * @param string $path The path of configuration
     * @param string $scope The scope of configuration
     * @param string|int|null $scopeCode The scope code or its identifier. The values for this
     * field are taken from 'store' or 'store_website' tables, depends on $scope value
     * @param string|null $type The type of configuration.
     * The available types are declared in implementations of Magento\Framework\App\Config\ConfigTypeInterface
     * E.g.
     * ```php
     * const CONFIG_TYPE = 'system';
     * ```
     * @return string Resolved configuration path
     */
    public function resolve($path, $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $scopeCode = null, $type = null)
    {
        $path = trim($path, '/');
        $scope = rtrim($scope, 's');

        /** Scope name is currently stored in plural form. */
        if (in_array($scope, [ScopeInterface::SCOPE_STORE, ScopeInterface::SCOPE_WEBSITE])) {
            $scope .= 's';
        }

        $scopePath = $type ? $type . '/' . $scope : $scope;

        if ($scope !== ScopeConfigInterface::SCOPE_TYPE_DEFAULT) {
            if (is_numeric($scopeCode) || $scopeCode === null) {
                $scopeCode = $this->scopeCodeResolver->resolve($scope, $scopeCode);
            }

            $scopePath .= '/' . $scopeCode;
        }

        return $scopePath . ($path ? '/' . $path : '');
    }
}