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;
|
|
|
|
|
2024-12-02 15:10:55 +05:00
|
|
|
final readonly class ConfigLoader {
|
2016-09-19 01:01:19 +03:00
|
|
|
|
2024-12-02 15:10:55 +05:00
|
|
|
private const string ROOT_PATH = __DIR__ . '/../..';
|
2016-09-19 01:01:19 +03:00
|
|
|
|
2024-12-02 15:10:55 +05:00
|
|
|
public function __construct(private string $application) {
|
2016-09-19 01:01:19 +03:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-12-02 15:10:55 +05:00
|
|
|
// @phpstan-ignore arguments.count (Should be covered by Yii2 extension)
|
2016-09-19 01:01:19 +03:00
|
|
|
return ArrayHelper::merge(...$toMerge);
|
|
|
|
}
|
|
|
|
|
2024-12-02 15:10:55 +05:00
|
|
|
/**
|
|
|
|
* @return array<string, mixed>
|
|
|
|
*/
|
2018-04-17 23:47:25 +03:00
|
|
|
public static function load(string $application): array {
|
2024-12-02 15:10:55 +05:00
|
|
|
return (new self($application))->getConfig();
|
2016-09-19 01:01:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|