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;
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
final readonly class ConfigLoader {
|
2016-09-19 03:31:19 +05:30
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
private const string ROOT_PATH = __DIR__ . '/../..';
|
2016-09-19 03:31:19 +05:30
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function __construct(private string $application) {
|
2016-09-19 03:31:19 +05:30
|
|
|
}
|
|
|
|
|
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',
|
|
|
|
];
|
|
|
|
|
2019-07-15 04:29:56 +05:30
|
|
|
// Common env-dependent configuration
|
2016-09-19 03:31:19 +05:30
|
|
|
$path = __DIR__ . '/config-' . YII_ENV . '.php';
|
|
|
|
if (file_exists($path)) {
|
|
|
|
$toMerge[] = require $path;
|
|
|
|
}
|
|
|
|
|
2019-07-15 04:29:56 +05:30
|
|
|
// Common local configuration
|
2016-09-19 03:31:19 +05:30
|
|
|
$path = __DIR__ . '/config-local.php';
|
|
|
|
if (file_exists($path)) {
|
|
|
|
$toMerge[] = require $path;
|
|
|
|
}
|
|
|
|
|
2019-07-15 04:29:56 +05:30
|
|
|
// App-related base configuration
|
2016-09-19 03:31:19 +05:30
|
|
|
$path = self::ROOT_PATH . '/' . $this->application . '/config/config.php';
|
|
|
|
if (file_exists($path)) {
|
|
|
|
$toMerge[] = require $path;
|
|
|
|
}
|
|
|
|
|
2019-07-15 04:29:56 +05:30
|
|
|
// App-related env-dependent configuration
|
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;
|
|
|
|
}
|
|
|
|
|
2019-07-15 04:29:56 +05:30
|
|
|
// App-related local configuration
|
2016-09-19 03:31:19 +05:30
|
|
|
$path = self::ROOT_PATH . '/' . $this->application . '/config/config-local.php';
|
|
|
|
if (file_exists($path)) {
|
|
|
|
$toMerge[] = require $path;
|
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
// @phpstan-ignore arguments.count (Should be covered by Yii2 extension)
|
2016-09-19 03:31:19 +05:30
|
|
|
return ArrayHelper::merge(...$toMerge);
|
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
/**
|
|
|
|
* @return array<string, mixed>
|
|
|
|
*/
|
2018-04-18 02:17:25 +05:30
|
|
|
public static function load(string $application): array {
|
2024-12-02 15:40:55 +05:30
|
|
|
return (new self($application))->getConfig();
|
2016-09-19 03:31:19 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
}
|