-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApplication.php
297 lines (257 loc) · 8.52 KB
/
Application.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<?php
namespace RSL\MVC;
use RSL\Event\EventManagerAwareInterface;
use RSL\Event\EventManagerInterface;
use RSL\Service\ServiceManager;
use RSL\Stdlib\ResponseInterface;
class Application implements
ApplicationInterface,
EventManagerAwareInterface
{
const ERROR_CONTROLLER_CANNOT_DISPATCH
= 'error-controller-cannot-dispatch';
const ERROR_CONTROLLER_NOT_FOUND
= 'error-controller-not-found';
const ERROR_CONTROLLER_INVALID
= 'error-controller-invalid';
const ERROR_EXCEPTION
= 'error-exception';
const ERROR_ROUTER_NO_MATCH
= 'error-router-no-match';
/**
* @var array
*/
protected $configuration = null;
/**
* Default application event listeners
* Приемники событий по умолчанию приложений
*
* @var array
*/
protected $defaultListeners = array(
'RouteListener',
'DispatchListener',
'HttpMethodListener',
'ViewManager',
'SendResponseListener',
);
/**
* MVC event token
* Идентификатор события MVC
* @var MvcEvent
*/
protected $event;
/**
* @var EventManagerInterface
*/
protected $events;
/**
* @var \RSL\Stdlib\RequestInterface
*/
protected $request;
/**
* @var ResponseInterface
*/
protected $response;
/**
* @var ServiceManager
*/
protected $serviceManager = null;
/**
* Constructor
*
* @param mixed $configuration
* @param ServiceManager $serviceManager
*/
public function __construct(
$configuration,
ServiceManager $serviceManager
)
{
$this->configuration = $configuration;
$this->serviceManager = $serviceManager;
$this->setEventManager($serviceManager->get('EventManager'));
$this->request = $serviceManager->get('Request');
$this->response = $serviceManager->get('Response');
}
/**
* Retrieve the application configuration
* Получить конфигурацию приложения
*
* @return array|object
*/
public function getConfig()
{
return $this->serviceManager->get('Config');
}
/**
* Bootstrap the application
* Загрузка приложения
*
* Defines and binds the MvcEvent, and passes it the request, response, and
* router. Attaches the ViewManager as a listener. Triggers the bootstrap
* event.
*
* Определяет и связывает MvcEvent
* и передает ему запрос, ответ и маршрутизатор.
* Прикрепляет ViewManager как слушателя.
* Запускает событие бутстрапа.
*
* @param array $listeners List of listeners to attach.
* @return Application
*/
public function bootstrap(array $listeners = array())
{
$serviceManager = $this->serviceManager;
$events = $this->events;
$listeners = array_unique(
array_merge($this->defaultListeners,
$listeners)
);
foreach ($listeners as $listener)
{
$events->attach($serviceManager->get($listener));
}
// Setup MVC Event
// Настройка события MVC
$this->event = $event = new MvcEvent();
$event->setTarget($this);
$event->setApplication($this)
->setRequest($this->request)
->setResponse($this->response)
->setRouter($serviceManager->get('Router'));
// Trigger bootstrap events
// Триггерные загрузочные события
$events->trigger(MvcEvent::EVENT_BOOTSTRAP, $event);
return $this;
}
/**
* Retrieve the service manager
* Получить сервис-менеджера
*
* @return ServiceManager
*/
public function getServiceManager()
{
return $this->serviceManager;
}
/**
* Get the request object
* Получить объект [Запрос] Request
*
* @return \RSL\Stdlib\RequestInterface
*/
public function getRequest()
{
return $this->request;
}
/**
* Get the response object
* Получить объект [Ответ] Response
*
* @return ResponseInterface
*/
public function getResponse()
{
return $this->response;
}
/**
* Get the MVC event instance
* Получить экземпляр события MVC
*
* @return MvcEvent
*/
public function getMvcEvent()
{
return $this->event;
}
/**
* Set the event manager instance
* Установить экземпляр менеджера событий
*
* @param EventManagerInterface $eventManager
* @return Application
*/
public function setEventManager(
EventManagerInterface $eventManager
)
{
$eventManager->setIdentifiers(
array(
__CLASS__,
get_class($this),
)
);
$this->events = $eventManager;
return $this;
}
/**
* Retrieve the event manager
* Получить менеджер событий
*
* Lazy-loads an EventManager instance if none registered.
* Lazy загружает экземпляр EventManager,
* если он не зарегистрирован.
*
* @return EventManagerInterface
*/
public function getEventManager()
{
return $this->events;
}
/**
* Static method for quick and easy initialization of the Application.
* Статический метод быстрой и легкой инициализации приложения.
*
* If you use this init() method, you cannot specify a service with the
* name of 'ApplicationConfig' in your service manager config. This name is
* reserved to hold the array from application.config.php.
* Если вы используете этот метод init (),
* вы не можете указать службу с именем «ApplicationConfig»
* в конфигурации вашего менеджера сервисов.
* Это имя <b>зарезервировано</b> для хранения массива
* из application.config.php.
*
* The following services can only be overridden from application.config.php:
* Следующие сервисы можно переопределить только из application.config.php:
*
* - ModuleManager
* - SharedEventManager
* - EventManager & RSL\EventManager\EventManagerInterface
*
* All other services are configured after module loading, thus can be
* overridden by modules.
* Все остальные службы настраиваются после загрузки модуля,
* поэтому их можно переопределить модулями.
*
* @param array $configuration
* @return Application
*/
public static function init($configuration = array())
{
$smConfig = isset($configuration['service_manager']) ? $configuration['service_manager'] : array();
$serviceManager =
new ServiceManager(
new Service\ServiceManagerConfig($smConfig)
);
$serviceManager->setService(
'ApplicationConfig', $configuration);
$serviceManager->get('ModuleManager')->loadModules();
$listenersFromAppConfig =
isset($configuration['listeners'])
? $configuration['listeners']
: array();
$config = $serviceManager->get('Config');
$listenersFromConfigService =
isset($config['listeners'])
? $config['listeners']
: array();
$listeners = array_unique(
array_merge($listenersFromConfigService,
$listenersFromAppConfig)
);
return $serviceManager->get('Application')
->bootstrap($listeners);
}
} // End of class Application
?>