2016-09-19 01:01:19 +03:00
|
|
|
<?php
|
2019-02-20 22:58:52 +03:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-09-19 01:01:19 +03:00
|
|
|
namespace common\config;
|
|
|
|
|
|
|
|
use yii\helpers\ArrayHelper;
|
|
|
|
|
|
|
|
class ConfigLoader {
|
|
|
|
|
2017-12-02 22:15:00 +03:00
|
|
|
private const ROOT_PATH = __DIR__ . '/../..';
|
2016-09-19 01:01:19 +03:00
|
|
|
|
|
|
|
private $application;
|
|
|
|
|
|
|
|
public function __construct(string $application) {
|
|
|
|
$this->application = $application;
|
|
|
|
}
|
|
|
|
|
2018-04-17 23:47:25 +03:00
|
|
|
public function getEnvironment(): string {
|
2016-09-19 01:01:19 +03:00
|
|
|
return YII_ENV;
|
|
|
|
}
|
|
|
|
|
2018-04-17 23:47:25 +03:00
|
|
|
public function getConfig(): array {
|
2016-09-19 01:01:19 +03:00
|
|
|
$toMerge = [
|
|
|
|
require __DIR__ . '/config.php',
|
|
|
|
];
|
|
|
|
|
2019-07-15 01:59:56 +03:00
|
|
|
// Common env-dependent configuration
|
2016-09-19 01:01:19 +03:00
|
|
|
$path = __DIR__ . '/config-' . YII_ENV . '.php';
|
|
|
|
if (file_exists($path)) {
|
|
|
|
$toMerge[] = require $path;
|
|
|
|
}
|
|
|
|
|
2019-07-15 01:59:56 +03:00
|
|
|
// Common local configuration
|
2016-09-19 01:01:19 +03:00
|
|
|
$path = __DIR__ . '/config-local.php';
|
|
|
|
if (file_exists($path)) {
|
|
|
|
$toMerge[] = require $path;
|
|
|
|
}
|
|
|
|
|
2019-07-15 01:59:56 +03:00
|
|
|
// App-related base configuration
|
2016-09-19 01:01:19 +03:00
|
|
|
$path = self::ROOT_PATH . '/' . $this->application . '/config/config.php';
|
|
|
|
if (file_exists($path)) {
|
|
|
|
$toMerge[] = require $path;
|
|
|
|
}
|
|
|
|
|
2019-07-15 01:59:56 +03:00
|
|
|
// App-related env-dependent configuration
|
2016-10-02 01:20:40 +03:00
|
|
|
$path = self::ROOT_PATH . '/' . $this->application . '/config/config-' . YII_ENV . '.php';
|
2016-09-19 01:01:19 +03:00
|
|
|
if (file_exists($path)) {
|
|
|
|
$toMerge[] = require $path;
|
|
|
|
}
|
|
|
|
|
2019-07-15 01:59:56 +03:00
|
|
|
// App-related local configuration
|
2016-09-19 01:01:19 +03:00
|
|
|
$path = self::ROOT_PATH . '/' . $this->application . '/config/config-local.php';
|
|
|
|
if (file_exists($path)) {
|
|
|
|
$toMerge[] = require $path;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ArrayHelper::merge(...$toMerge);
|
|
|
|
}
|
|
|
|
|
2018-04-17 23:47:25 +03:00
|
|
|
public static function load(string $application): array {
|
2016-09-19 01:01:19 +03:00
|
|
|
return (new static($application))->getConfig();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|