2016-09-19 03:31:19 +05:30
|
|
|
<?php
|
2019-02-21 01:28:52 +05:30
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-09-19 03:31:19 +05:30
|
|
|
namespace common\config;
|
|
|
|
|
|
|
|
use yii\helpers\ArrayHelper;
|
|
|
|
|
|
|
|
class ConfigLoader {
|
|
|
|
|
2017-12-03 00:45:00 +05:30
|
|
|
private const ROOT_PATH = __DIR__ . '/../..';
|
2016-09-19 03:31:19 +05:30
|
|
|
|
|
|
|
private $application;
|
|
|
|
|
|
|
|
public function __construct(string $application) {
|
|
|
|
$this->application = $application;
|
|
|
|
}
|
|
|
|
|
2018-04-18 02:17:25 +05:30
|
|
|
public function getEnvironment(): string {
|
2016-09-19 03:31:19 +05:30
|
|
|
return YII_ENV;
|
|
|
|
}
|
|
|
|
|
2018-04-18 02:17:25 +05:30
|
|
|
public function getConfig(): array {
|
2016-09-19 03:31:19 +05:30
|
|
|
$toMerge = [
|
|
|
|
require __DIR__ . '/config.php',
|
|
|
|
];
|
|
|
|
|
|
|
|
// Общие окружение-зависимые настройки
|
|
|
|
$path = __DIR__ . '/config-' . YII_ENV . '.php';
|
|
|
|
if (file_exists($path)) {
|
|
|
|
$toMerge[] = require $path;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Общие локальные настройки
|
|
|
|
$path = __DIR__ . '/config-local.php';
|
|
|
|
if (file_exists($path)) {
|
|
|
|
$toMerge[] = require $path;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Настройки конкретного приложения
|
|
|
|
$path = self::ROOT_PATH . '/' . $this->application . '/config/config.php';
|
|
|
|
if (file_exists($path)) {
|
|
|
|
$toMerge[] = require $path;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Настройки конкретного приложения для действующего окружения
|
2016-10-02 03:50:40 +05:30
|
|
|
$path = self::ROOT_PATH . '/' . $this->application . '/config/config-' . YII_ENV . '.php';
|
2016-09-19 03:31:19 +05:30
|
|
|
if (file_exists($path)) {
|
|
|
|
$toMerge[] = require $path;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Локальные настройки конкретного приложения
|
|
|
|
$path = self::ROOT_PATH . '/' . $this->application . '/config/config-local.php';
|
|
|
|
if (file_exists($path)) {
|
|
|
|
$toMerge[] = require $path;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ArrayHelper::merge(...$toMerge);
|
|
|
|
}
|
|
|
|
|
2018-04-18 02:17:25 +05:30
|
|
|
public static function load(string $application): array {
|
2016-09-19 03:31:19 +05:30
|
|
|
return (new static($application))->getConfig();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|