SettingChecker.php 3.2 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
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Config\Model\Config\Reader\Source\Deployed;

use Magento\Config\Model\Config\Reader;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\DeploymentConfig;
use Magento\Config\Model\Placeholder\PlaceholderInterface;
use Magento\Config\Model\Placeholder\PlaceholderFactory;
use Magento\Framework\App\Config\ScopeCodeResolver;

/**
 * Class for checking settings that defined in config file
 * @api
 * @since 100.1.2
 */
class SettingChecker
{
    /**
     * @var DeploymentConfig
     */
    private $config;

    /**
     * @var PlaceholderInterface
     */
    private $placeholder;

    /**
     * @var ScopeCodeResolver
     */
    private $scopeCodeResolver;

    /**
     * @param DeploymentConfig $config
     * @param PlaceholderFactory $placeholderFactory
     * @param ScopeCodeResolver $scopeCodeResolver
     */
    public function __construct(
        DeploymentConfig $config,
        PlaceholderFactory $placeholderFactory,
        ScopeCodeResolver $scopeCodeResolver
    ) {
        $this->config = $config;
        $this->scopeCodeResolver = $scopeCodeResolver;
        $this->placeholder = $placeholderFactory->create(PlaceholderFactory::TYPE_ENVIRONMENT);
    }

    /**
     * Check that setting defined in deployed configuration
     *
     * @param string $path
     * @param string $scope
     * @param string|null $scopeCode
     * @return boolean
     * @since 100.1.2
     */
    public function isReadOnly($path, $scope, $scopeCode = null)
    {
        $config = $this->getEnvValue(
            $this->placeholder->generate($path, $scope, $scopeCode)
        );

        if (null === $config) {
            $config = $this->config->get($this->resolvePath($scope, $scopeCode) . "/" . $path);
        }

        return $config !== null;
    }

    /**
     * Check that there is value for generated placeholder
     *
     * Placeholder is generated from values of $path, $scope and $scopeCode
     *
     * @param string $path
     * @param string $scope
     * @param string $scopeCode
     * @param string|null $scopeCode
     * @return string|null
     * @since 100.1.2
     */
    public function getPlaceholderValue($path, $scope, $scopeCode = null)
    {
        return $this->getEnvValue($this->placeholder->generate($path, $scope, $scopeCode));
    }

    /**
     * Retrieve value of environment variable by placeholder
     *
     * @param string $placeholder
     * @return string|null
     * @since 100.1.2
     */
    public function getEnvValue($placeholder)
    {
        if ($this->placeholder->isApplicable($placeholder) && isset($_ENV[$placeholder])) {
            return $_ENV[$placeholder];
        }

        return null;
    }

    /**
     * Resolve path by scope and scope code
     *
     * @param string $scope
     * @param string $scopeCode
     * @return string
     */
    private function resolvePath($scope, $scopeCode)
    {
        $scopePath = 'system/' . $scope;

        if ($scope != ScopeConfigInterface::SCOPE_TYPE_DEFAULT) {
            $scopePath .= '/' . $this->scopeCodeResolver->resolve($scope, $scopeCode);
        }

        return $scopePath;
    }
}