1 | <?php |
||
24 | final class RestEventSubscriber implements EventSubscriberInterface |
||
25 | { |
||
26 | /** |
||
27 | * @var Response |
||
28 | */ |
||
29 | private $response; |
||
30 | |||
31 | /** |
||
32 | * @var RestEvent |
||
33 | */ |
||
34 | private $restEvent; |
||
35 | |||
36 | /** |
||
37 | * @var ContainerInterface |
||
38 | */ |
||
39 | private $container; |
||
40 | |||
41 | /** |
||
42 | * @param Response $response Response |
||
43 | * @param RestEvent $restEvent Rest event |
||
44 | * @param ContainerInterface $container Container |
||
45 | */ |
||
46 | public function __construct(Response $response, RestEvent $restEvent, ContainerInterface $container) |
||
52 | |||
53 | /** |
||
54 | * Returns the subscribed events |
||
55 | * |
||
56 | * @return array $ret Events to subscribe |
||
|
|||
57 | */ |
||
58 | 10 | public static function getSubscribedEvents() |
|
59 | { |
||
60 | /** |
||
61 | * There is a priority flag to manage execution order. |
||
62 | * If we need more controll over this, we can use this flag or add |
||
63 | * pre/post Methods like described here: |
||
64 | * http://symfony.com/doc/current/components/event_dispatcher/introduction.html (StoreSubscriber) |
||
65 | */ |
||
66 | |||
67 | return array( |
||
68 | 10 | 'kernel.request' => array("onKernelRequest", 0), |
|
69 | 'kernel.response' => array("onKernelResponse", 0) |
||
70 | ); |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * Handler for kernel.request events |
||
75 | * |
||
76 | * @param GetResponseEvent $event Event |
||
77 | * @param string $name Event name |
||
78 | * @param EventDispatcherInterface $dispatcher Event dispatcher |
||
79 | * |
||
80 | * @return void |
||
81 | */ |
||
82 | public function onKernelRequest(GetResponseEvent $event, $name, EventDispatcherInterface $dispatcher) |
||
83 | { |
||
84 | $restEvent = $this->getEventObject($event); |
||
85 | if ($restEvent instanceof RestEvent) { |
||
86 | $dispatcher->dispatch("graviton.rest.request", $restEvent); |
||
87 | } |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * Get a RestEvent object -> put this in a factory |
||
92 | * |
||
93 | * @param Event $event Original event (kernel.request / kernel.response) |
||
94 | * |
||
95 | * @return RestEvent $restEvent |
||
96 | */ |
||
97 | private function getEventObject(Event $event) |
||
98 | { |
||
99 | // get the service name |
||
100 | list ($serviceName) = explode(":", $event->getRequest()->get('_controller')); |
||
101 | |||
102 | // get the controller which handles this request |
||
103 | if ($this->container->has($serviceName)) { |
||
104 | $controller = $this->container->get($serviceName); |
||
105 | $restEvent = $this->restEvent; |
||
106 | $restEvent->setRequest($event->getRequest()); |
||
107 | $restEvent->setResponse($this->response); |
||
108 | $restEvent->setController($controller); |
||
109 | |||
110 | $returnEvent = $restEvent; |
||
111 | } else { |
||
112 | $returnEvent = $event; |
||
113 | } |
||
114 | |||
115 | return $returnEvent; |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * Handler for kernel.response events |
||
120 | * |
||
121 | * @param FilterResponseEvent $event Event |
||
122 | * @param string $name Event name |
||
123 | * @param EventDispatcherInterface $dispatcher Event dispatcher |
||
124 | * |
||
125 | * @return void |
||
126 | */ |
||
127 | public function onKernelResponse(FilterResponseEvent $event, $name, EventDispatcherInterface $dispatcher) |
||
131 | } |
||
132 |
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.