-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBootstrap.php
130 lines (112 loc) · 3.71 KB
/
Bootstrap.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
/**
* UMI.Framework (http://umi-framework.ru/)
*
* @link http://github.com/Umisoft/framework for the canonical source repository
* @copyright Copyright (c) 2007-2013 Umisoft ltd. (http://umisoft.ru/)
* @license http://umi-framework.ru/license/bsd-3 BSD-3 License
*/
use umi\config\entity\IConfig;
use umi\config\io\IConfigIO;
use umi\hmvc\component\IComponentFactory;
use umi\hmvc\component\request\IComponentRequestFactory;
use umi\http\request\IRequest;
use umi\spl\config\TConfigSupport;
use umi\toolkit\IToolkit;
use umi\toolkit\Toolkit;
/**
* Класс Bootstrap. Инициализирует приложение.
*/
class Bootstrap
{
use TConfigSupport;
const OPTION_TOOLKIT = 'toolkit';
const OPTION_SETTINGS = 'settings';
/** Символическое имя конфигурационного файла. */
const GENERAL_CONFIG = '~/general.php';
/**
* @var IToolkit $tools
*/
protected $toolkit;
/**
* @var IConfig $config
*/
protected $configuration;
/**
* Конструктор
* @param array $configuration boot-конфигурация
*/
public function __construct(array $configuration)
{
$this->initToolkit($configuration);
}
/**
* Создает компонент приложения.
*/
public function createApplication()
{
$appConfig = $this->configuration->get('application');
$appConfig = $this->configToArray($appConfig);
/** @var IComponentFactory $componentFactory */
$componentFactory = $this->toolkit->getService('umi\hmvc\component\IComponentFactory');
return $componentFactory->createComponent($appConfig);
}
/**
* Запускает приложение.
*/
public function runApplication()
{
$application = $this->createApplication();
try {
$application
->execute($this->getComponentRequest())
->send();
} catch (\Exception $e) {
throw new ErrorException(
'Unhandled exception thrown.', 0, 1, __FILE__, __LINE__, $e
);
}
}
/**
* Возвращает toolkit.
*
* @return IToolkit
*/
public function getToolkit()
{
return $this->toolkit;
}
/**
* Загружает и регистрирует конфигурацию тулбокса.
*/
protected function initToolkit($bootConfig)
{
$this->toolkit = new Toolkit();
if (isset($bootConfig[self::OPTION_TOOLKIT])) {
$this->toolkit->registerToolboxes($bootConfig[self::OPTION_TOOLKIT]);
}
if (isset($bootConfig[self::OPTION_SETTINGS])) {
$this->toolkit->setSettings($bootConfig[self::OPTION_SETTINGS]);
}
/**
* @var IConfigIO $configIO
*/
$configIO = $this->toolkit->getService('umi\config\io\IConfigIO');
$this->configuration = $configIO->read(self::GENERAL_CONFIG);
$this->toolkit->setSettings($this->configuration['toolkit'] ? : []);
}
protected function getComponentRequest()
{
/** @var IComponentRequestFactory $componentRequestFactory */
$componentRequestFactory = $this->toolkit->getService('umi\hmvc\component\request\IComponentRequestFactory');
/**
* @var IRequest $request
*/
$request = $this->toolkit->getService('umi\http\request\IRequest');
$requestUrl = parse_url($request->getRequestUri(), PHP_URL_PATH);
$requestUrl = substr($requestUrl, -1) == '/' ? substr($requestUrl, 0, -1) : $requestUrl;
return $componentRequestFactory->createComponentRequest(
$requestUrl
);
}
}