|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @package midcom.routing |
|
4
|
|
|
* @author CONTENT CONTROL http://www.contentcontrol-berlin.de/ |
|
5
|
|
|
* @copyright CONTENT CONTROL http://www.contentcontrol-berlin.de/ |
|
6
|
|
|
* @license http://www.gnu.org/licenses/gpl.html GNU General Public License |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace midcom\httpkernel; |
|
10
|
|
|
|
|
11
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
12
|
|
|
use midcom_response_styled; |
|
13
|
|
|
use midcom_baseclasses_components_handler; |
|
14
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
|
15
|
|
|
use midcom\routing\resolver; |
|
16
|
|
|
use midcom; |
|
17
|
|
|
use midcom_connection; |
|
18
|
|
|
use midcom_core_context; |
|
19
|
|
|
use Symfony\Component\HttpKernel\Event\RequestEvent; |
|
20
|
|
|
use Symfony\Component\HttpKernel\Event\ExceptionEvent; |
|
21
|
|
|
use Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent; |
|
22
|
|
|
use Symfony\Component\HttpKernel\Event\ViewEvent; |
|
23
|
|
|
use Symfony\Component\HttpKernel\Event\ControllerEvent; |
|
24
|
|
|
use Symfony\Component\HttpFoundation\StreamedResponse; |
|
25
|
|
|
use Symfony\Component\HttpKernel\Event\ResponseEvent; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @package midcom.httpkernel |
|
29
|
|
|
*/ |
|
30
|
|
|
class subscriber implements EventSubscriberInterface |
|
31
|
|
|
{ |
|
32
|
|
|
public static function getSubscribedEvents() |
|
33
|
|
|
{ |
|
34
|
|
|
return [ |
|
35
|
|
|
KernelEvents::REQUEST => ['on_request'], |
|
36
|
|
|
KernelEvents::CONTROLLER => ['on_controller'], |
|
37
|
|
|
KernelEvents::CONTROLLER_ARGUMENTS => ['on_arguments'], |
|
38
|
|
|
KernelEvents::VIEW => ['on_view'], |
|
39
|
|
|
KernelEvents::RESPONSE => ['on_response'], |
|
40
|
|
|
KernelEvents::EXCEPTION => ['on_exception'] |
|
41
|
|
|
]; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
351 |
|
public function on_request(RequestEvent $event) |
|
45
|
|
|
{ |
|
46
|
351 |
|
$request = $event->getRequest(); |
|
47
|
351 |
|
if ($event->isMainRequest()) { |
|
48
|
|
|
// Initialize Context Storage |
|
49
|
|
|
$context = midcom_core_context::enter(midcom_connection::get_url('uri')); |
|
50
|
|
|
$request->attributes->set('context', $context); |
|
51
|
|
|
|
|
52
|
|
|
// Initialize the UI message stack from session |
|
53
|
|
|
midcom::get()->uimessages->initialize($request); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
351 |
|
$resolver = new resolver($request); |
|
57
|
351 |
|
if ($resolver->process_midcom()) { |
|
58
|
1 |
|
return; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
350 |
|
$resolver->process_component(); |
|
62
|
350 |
|
$request->attributes->set('data', '__request_data__'); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
351 |
|
public function on_controller(ControllerEvent $event) |
|
66
|
|
|
{ |
|
67
|
351 |
|
$request = $event->getRequest(); |
|
68
|
351 |
|
$attributes = $request->attributes->all(); |
|
69
|
351 |
|
if (!empty($attributes['__viewer'])) { |
|
70
|
350 |
|
$controller = $event->getController(); |
|
71
|
350 |
|
$attributes['__viewer']->prepare_handler($controller[0], $attributes); |
|
72
|
350 |
|
unset($attributes['__viewer']); |
|
73
|
350 |
|
$request->attributes->set('handler_id', $attributes['_route']); |
|
74
|
350 |
|
$request->attributes->set('__handler', $controller); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
351 |
|
public function on_arguments(ControllerArgumentsEvent $event) |
|
79
|
|
|
{ |
|
80
|
351 |
|
$arguments = $event->getArguments(); |
|
81
|
351 |
|
$i = array_search('__request_data__', $arguments, true); |
|
82
|
351 |
|
if ($i !== false) { |
|
83
|
220 |
|
$context = $event->getRequest()->attributes->get('context'); |
|
84
|
220 |
|
$arguments[$i] =& $context->get_custom_key('request_data'); |
|
85
|
220 |
|
$event->setArguments($arguments); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
59 |
|
public function on_view(ViewEvent $event) |
|
90
|
|
|
{ |
|
91
|
59 |
|
$attributes = $event->getRequest()->attributes; |
|
92
|
59 |
|
$controller = $attributes->get('__handler'); |
|
93
|
59 |
|
if ($controller[0] instanceof midcom_baseclasses_components_handler) { |
|
94
|
59 |
|
$controller[0]->populate_breadcrumb_line(); |
|
95
|
|
|
} |
|
96
|
59 |
|
$event->setResponse(new midcom_response_styled($attributes->get('context'))); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
351 |
|
public function on_response(ResponseEvent $event) |
|
100
|
|
|
{ |
|
101
|
351 |
|
if ($event->isMainRequest()) { |
|
102
|
|
|
$response = $event->getResponse(); |
|
103
|
|
|
if ($response instanceof StreamedResponse) { |
|
104
|
|
|
// if we have a streamed response, we need to send right away |
|
105
|
|
|
// otherwise exceptions in the callback won't be caught by the kernel |
|
106
|
|
|
// which means e.g. that a midcom-exec script couldn't show a login screen |
|
107
|
|
|
$response->send(); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function on_exception(ExceptionEvent $event) |
|
113
|
|
|
{ |
|
114
|
|
|
$event->allowCustomResponseCode(); |
|
115
|
|
|
$handler = new \midcom_exception_handler($event->getThrowable()); |
|
116
|
|
|
$event->setResponse($handler->render()); |
|
117
|
|
|
} |
|
118
|
|
|
} |