Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
95.00% covered (success)
95.00%
38 / 40
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
AppStartup
95.00% covered (success)
95.00%
38 / 40
66.67% covered (warning)
66.67%
2 / 3
5
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setupCommon
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
1 / 1
2
 setupWebService
86.67% covered (warning)
86.67%
13 / 15
0.00% covered (danger)
0.00%
0 / 1
2.01
1<?php
2
3declare(strict_types=1);
4
5namespace PeServer\App\Models;
6
7use PeServer\App\Models\AppConfiguration;
8use PeServer\App\Models\AppCryptography;
9use PeServer\App\Models\AppDatabaseCache;
10use PeServer\App\Models\AppDatabaseConnection;
11use PeServer\App\Models\AppEmailInformation;
12use PeServer\App\Models\AppErrorHandler;
13use PeServer\App\Models\AppMailer;
14use PeServer\App\Models\AppRouteSetting;
15use PeServer\App\Models\AppRouting;
16use PeServer\App\Models\AppTemplate;
17use PeServer\App\Models\AppTemplateFactory;
18use PeServer\App\Models\AppUrl;
19use PeServer\App\Models\Domain\AccessLogManager;
20use PeServer\App\Models\Domain\AppArchiver;
21use PeServer\App\Models\Domain\AppEraser;
22use PeServer\Core\Collection\Arr;
23use PeServer\Core\ProgramContext;
24use PeServer\Core\CoreStartup;
25use PeServer\Core\Database\IDatabaseConnection;
26use PeServer\Core\StartupOptions;
27use PeServer\Core\DI\DiItem;
28use PeServer\Core\DI\IDiRegisterContainer;
29use PeServer\Core\Environment;
30use PeServer\Core\Http\HttpMethod;
31use PeServer\Core\Http\RequestPath;
32use PeServer\Core\IO\Path;
33use PeServer\Core\Log\ILogProvider;
34use PeServer\Core\Mail\Mailer;
35use PeServer\Core\Mvc\RouteRequest;
36use PeServer\Core\Mvc\RouteSetting;
37use PeServer\Core\Mvc\Routing;
38use PeServer\Core\Mvc\Template\ITemplateFactory;
39use PeServer\Core\Web\WebSecurity;
40use PeServer\Core\Store\SpecialStore;
41use PeServer\Core\Text;
42use PeServer\Core\Web\UrlHelper;
43
44class AppStartup extends CoreStartup
45{
46    /**
47     * 初期化。
48     *
49     * @param StartupOptions $startupOptions
50     */
51    public function __construct(
52        StartupOptions $startupOptions,
53    ) {
54        parent::__construct($startupOptions);
55    }
56
57    #region CoreStartup
58
59    protected function setupCommon(array $options, IDiRegisterContainer $container): void
60    {
61        parent::setupCommon($options, $container);
62
63        /** @var ILogProvider */
64        $logProvider = $container->get(ILogProvider::class);
65
66        $appConfig = new AppConfiguration(
67            $container->get(ProgramContext::class),
68            $options['url_helper'] ?? new UrlHelper(''),
69            $container->get(WebSecurity::class),
70            $options['special_store'] ?? new SpecialStore(),
71            $container->get(Environment::class)
72        );
73        $container->registerValue($appConfig);
74
75        $logging = $appConfig->setting->logging;
76        foreach ($logging->loggers as $name => $value) {
77            $logProvider->add($name, $value->loggerClass, $value->level, $value->format, $value->configuration);
78        }
79
80        $container->registerMapping(ITemplateFactory::class, AppTemplateFactory::class);
81        $container->registerMapping(IDatabaseConnection::class, AppDatabaseConnection::class);
82        $container->registerClass(AppCryptography::class);
83        $container->registerClass(AppDatabaseCache::class);
84        $container->registerMapping(Mailer::class, AppMailer::class);
85        $container->registerClass(AppTemplate::class);
86        $container->registerClass(AppArchiver::class);
87        $container->registerClass(AppEraser::class);
88        $container->registerClass(AppEmailInformation::class);
89        $container->registerClass(AppUrl::class);
90        $container->registerClass(AccessLogManager::class);
91    }
92
93    protected function setupWebService(array $options, IDiRegisterContainer $container): void
94    {
95        parent::setupWebService($options, $container);
96
97        /** @var AppConfiguration */
98        $appConfig = $container->get(AppConfiguration::class);
99        $container->registerValue($appConfig->stores);
100
101        /** @var SpecialStore */
102        $specialStore = $container->get(SpecialStore::class);
103
104        $method = HttpMethod::from(Text::toUpper(Text::trim($specialStore->getServer('REQUEST_METHOD'))));
105        $requestPath = new RequestPath(
106            $specialStore->getServer('REQUEST_URI'),
107            $options['url_helper'] ?? new UrlHelper('')
108        );
109        $container->registerValue(new RouteRequest($method, $requestPath));
110
111        $container->add(RouteSetting::class, DiItem::value($container->new(AppRouteSetting::class)));
112        $container->registerMapping(Routing::class, AppRouting::class);
113
114        if ($this->startupOptions->errorHandling) {
115            /** @var AppErrorHandler */
116            $appErrorHandler = $container->new(AppErrorHandler::class, [RequestPath::class => $requestPath]);
117            $appErrorHandler->register();
118        }
119    }
120
121    #endregion
122}