| 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 | 6 | 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 | 13 | 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 | 13 | '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 | 6 | public function onKernelRequest(GetResponseEvent $event, $name, EventDispatcherInterface $dispatcher) |
|
|
1 ignored issue
–
show
|
|||
| 83 | { |
||
| 84 | 6 | $restEvent = $this->getEventObject($event); |
|
| 85 | 6 | if ($restEvent instanceof RestEvent) { |
|
| 86 | 6 | $dispatcher->dispatch("graviton.rest.request", $restEvent); |
|
| 87 | } |
||
| 88 | 6 | } |
|
| 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 | 6 | private function getEventObject(Event $event) |
|
| 98 | { |
||
| 99 | // get the service name |
||
| 100 | 6 | list ($serviceName) = explode(":", $event->getRequest()->get('_controller')); |
|
| 101 | |||
| 102 | // get the controller which handles this request |
||
| 103 | 6 | if ($this->container->has($serviceName)) { |
|
| 104 | 6 | $controller = $this->container->get($serviceName); |
|
| 105 | 6 | $restEvent = $this->restEvent; |
|
| 106 | 6 | $restEvent->setRequest($event->getRequest()); |
|
| 107 | 6 | $restEvent->setResponse($this->response); |
|
| 108 | 6 | $restEvent->setController($controller); |
|
| 109 | |||
| 110 | 6 | $returnEvent = $restEvent; |
|
| 111 | } else { |
||
| 112 | $returnEvent = $event; |
||
| 113 | } |
||
| 114 | |||
| 115 | 6 | 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 | 6 | public function onKernelResponse(FilterResponseEvent $event, $name, EventDispatcherInterface $dispatcher) |
|
| 131 | } |
||
| 132 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.