Issues (3627)

app/bundles/CoreBundle/Loader/ParameterLoader.php (1 issue)

1
<?php
2
3
/*
4
 * @copyright   2019 Mautic Contributors. All rights reserved
5
 * @author      Mautic
6
 *
7
 * @link        https://mautic.org
8
 *
9
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
10
 */
11
12
namespace Mautic\CoreBundle\Loader;
13
14
use Symfony\Component\Finder\Finder;
15
use Symfony\Component\HttpFoundation\ParameterBag;
16
17
class ParameterLoader
18
{
19
    /**
20
     * @var string
21
     */
22
    private $rootPath;
23
24
    /**
25
     * @var ParameterBag
26
     */
27
    private $parameterBag;
28
29
    /**
30
     * @var ParameterBag
31
     */
32
    private $localParameterBag;
33
34
    /**
35
     * @var array
36
     */
37
    private $localParameters = [];
38
39
    /**
40
     * @var array
41
     */
42
    private static $defaultParameters = [];
43
44
    public function __construct(string $configRootPath = __DIR__.'/../../../')
45
    {
46
        $this->rootPath = $configRootPath;
47
48
        $this->loadDefaultParameters();
49
        $this->loadLocalParameters();
50
        $this->createParameterBags();
51
    }
52
53
    public function getParameterBag(): ParameterBag
54
    {
55
        return $this->parameterBag;
56
    }
57
58
    public function getLocalParameterBag(): ParameterBag
59
    {
60
        return $this->localParameterBag;
61
    }
62
63
    public function loadIntoEnvironment()
64
    {
65
        $envVariables      = new ParameterBag();
66
        $defaultParameters = new ParameterBag(self::$defaultParameters);
67
68
        // Load from local configuration file first
69
        EnvVars\ConfigEnvVars::load($this->parameterBag, $defaultParameters, $envVariables);
70
71
        // Load special values used in Mautic configuration files in app/config
72
        EnvVars\ApiEnvVars::load($this->parameterBag, $defaultParameters, $envVariables);
73
        EnvVars\ElFinderEnvVars::load($this->parameterBag, $defaultParameters, $envVariables);
74
        EnvVars\MigrationsEnvVars::load($this->parameterBag, $defaultParameters, $envVariables);
75
        EnvVars\SAMLEnvVars::load($this->parameterBag, $defaultParameters, $envVariables);
76
        EnvVars\SessionEnvVars::load($this->parameterBag, $defaultParameters, $envVariables);
77
        EnvVars\SiteUrlEnvVars::load($this->parameterBag, $defaultParameters, $envVariables);
78
        EnvVars\TwigEnvVars::load($this->parameterBag, $defaultParameters, $envVariables);
79
80
        // Load the values into the environment for cache use
81
        $dotenv = new \Symfony\Component\Dotenv\Dotenv();
82
        $dotenv->populate($envVariables->all());
83
    }
84
85
    public static function getLocalConfigFile(string $root): string
86
    {
87
        $root = realpath($root);
88
89
        /** @var array $paths */
90
        include $root.'/config/paths.php';
91
92
        if (!isset($paths['local_config'])) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $paths seems to never exist and therefore isset should always be false.
Loading history...
93
            self::$defaultParameters['local_config_path'] = $root.'/config/local.php';
94
95
            return self::$defaultParameters['local_config_path'];
96
        }
97
98
        $paths['local_config'] = str_replace('%kernel.root_dir%', $root, $paths['local_config']);
99
100
        self::$defaultParameters['local_config_path'] = $paths['local_config'];
101
102
        // We need this for the file manager
103
        if (isset($paths['local_root'])) {
104
            self::$defaultParameters['local_root'] = $paths['local_root'];
105
        }
106
107
        return self::$defaultParameters['local_config_path'];
108
    }
109
110
    private function loadDefaultParameters(): void
111
    {
112
        if (self::$defaultParameters) {
113
            // This is loaded within and outside the container so use static variable to prevent recompiling
114
            // multiple times
115
            return;
116
        }
117
118
        $finder = (new Finder())
119
            ->files()
120
            ->followLinks()
121
            ->depth('== 0')
122
            ->in(__DIR__.'/../../../bundles/*/Config')
123
            ->in(__DIR__.'/../../../../plugins/*/Config')
124
            ->name('config.php');
125
126
        /** @var \SplFileInfo $file */
127
        foreach ($finder as $file) {
128
            /** @var array $config */
129
            $config = include $file->getPathname();
130
131
            $parameters              = $config['parameters'] ?? [];
132
            self::$defaultParameters = array_merge(self::$defaultParameters, $parameters);
133
        }
134
    }
135
136
    private function loadLocalParameters(): void
137
    {
138
        $compiledParameters = [];
139
        $localConfigFile    = self::getLocalConfigFile($this->rootPath);
140
141
        // Load parameters array from local configuration
142
        if (file_exists($localConfigFile)) {
143
            /** @var array $parameters */
144
            include $localConfigFile;
145
146
            // Override default with local
147
            $compiledParameters = array_merge($compiledParameters, $parameters);
148
        }
149
150
        // Force local specific params
151
        $localParametersFile = $this->getLocalParametersFile();
152
        if (file_exists($localParametersFile)) {
153
            /** @var array $parameters */
154
            include $localParametersFile;
155
156
            //override default with forced
157
            $compiledParameters = array_merge($compiledParameters, $parameters);
158
        }
159
160
        // Load from environment
161
        $envParameters = getenv('MAUTIC_CONFIG_PARAMETERS');
162
        if ($envParameters) {
163
            $compiledParameters = array_merge($compiledParameters, json_decode($envParameters, true));
164
        }
165
166
        $this->localParameters = $compiledParameters;
167
    }
168
169
    private function createParameterBags(): void
170
    {
171
        $this->localParameterBag = new ParameterBag($this->localParameters);
172
        $this->parameterBag      = new ParameterBag(array_merge(self::$defaultParameters, $this->localParameters));
173
    }
174
175
    private function getLocalParametersFile(): string
176
    {
177
        return $this->rootPath.'/config/parameters_local.php';
178
    }
179
}
180