Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
AppConfiguration
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
2 / 2
2
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
1
 load
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace PeServer\App\Models;
6
7use PeServer\App\Models\Configuration\AppSetting;
8use PeServer\Core\ArrayUtility;
9use PeServer\Core\ProgramContext;
10use PeServer\Core\Environment;
11use PeServer\Core\I18n;
12use PeServer\Core\IO\Directory;
13use PeServer\Core\IO\File;
14use PeServer\Core\IO\Path;
15use PeServer\Core\Web\WebSecurity;
16use PeServer\Core\Serialization\Configuration;
17use PeServer\Core\Serialization\Mapper;
18use PeServer\Core\Store\SpecialStore;
19use PeServer\Core\Store\Stores;
20use PeServer\Core\Web\IUrlHelper;
21
22/**
23 * アプリ設定。
24 */
25class AppConfiguration
26{
27    #region variable
28
29    public readonly ProgramContext $context;
30
31    /**
32     * 設定データ。
33     */
34    public readonly AppSetting $setting;
35
36    /**
37     * URL ベースパス。
38     *
39     * @var IUrlHelper
40     */
41    public IUrlHelper $urlHelper;
42
43    /**
44     * 設定ファイル置き場。
45     *
46     * @var string
47     */
48    public string $settingDirectoryPath;
49
50    public Stores $stores;
51
52    #endregion
53
54    /**
55     * 初期化。
56     *
57     * @param ProgramContext $programContext
58     * @param SpecialStore $specialStore
59     */
60    public function __construct(ProgramContext $programContext, IUrlHelper $urlHelper, WebSecurity $webSecurity, SpecialStore $specialStore, Environment $environment)
61    {
62        $this->context = $programContext;
63
64        $this->settingDirectoryPath = Path::combine($this->context->applicationDirectory, 'config');
65
66        $appConfig = self::load($this->settingDirectoryPath, $this->context->rootDirectory, $this->context->applicationDirectory, $this->context->publicDirectory, $environment->get(), 'setting.json');
67        $i18nConfig = self::load($this->settingDirectoryPath, $this->context->rootDirectory, $this->context->applicationDirectory, $this->context->publicDirectory, $environment->get(), 'i18n.json');
68
69        $mapper = new Mapper();
70        $appSetting = new AppSetting();
71        $mapper->mapping($appConfig, $appSetting);
72
73        Directory::setTemporaryDirectory($appSetting->cache->temporary);
74
75        $storeOptions = StoreConfiguration::build($appSetting->store);
76        $stores = new Stores($specialStore, $storeOptions, $webSecurity);
77
78        I18n::initialize($i18nConfig);
79
80        $this->setting = $appSetting;
81        $this->urlHelper = $urlHelper;
82        $this->stores = $stores;
83    }
84
85    #region function
86
87    /**
88     * 設定ファイル読み込み。
89     *
90     * @param string $rootDirectoryPath
91     * @param string $publicDirectoryPath
92     * @param string $environment
93     * @return array<mixed>
94     */
95    private static function load(string $settingDirectoryPath, string $rootDirectoryPath, string $applicationDirectoryPath, string $publicDirectoryPath, string $environment, string $fileName): array
96    {
97        $configuration = new Configuration($environment);
98        $setting = $configuration->load($settingDirectoryPath, $fileName);
99
100        return $configuration->replace(
101            $setting,
102            [
103                'ROOT' => $rootDirectoryPath,
104                'APP' => $applicationDirectoryPath,
105                'PUBLIC' => $publicDirectoryPath,
106                'ENV' => $environment
107            ],
108            '$(',
109            ')'
110        );
111    }
112
113    #endregion
114}