/** * {@inheritdoc} */ public function register(EventDispatcher $dispatcher) { $dispatcher->addListener(IncomingRequestEvent::class, function (IncomingRequestEvent $event) { $this->session->handleRequest($event->getRequest()); }); $dispatcher->addListener(ResponseReceivedEvent::class, function (ResponseReceivedEvent $event) { $this->session->handleResponse($event->getResponse()); }); }
public function register(\Brick\Event\EventDispatcher $dispatcher) { $dispatcher->addListener(NonResponseResultEvent::class, function (NonResponseResultEvent $event) { $response = new Response(); $response->setContent(json_encode($event->getControllerResult())); $response->setHeader('Content-Type', 'application/json'); $event->setResponse($response); }); }
/** * {@inheritdoc} */ public function register(EventDispatcher $dispatcher) { $dispatcher->addListener(ControllerReadyEvent::class, function (ControllerReadyEvent $event) { $controller = $event->getControllerInstance(); if ($controller instanceof OnRequestInterface) { $response = $controller->onRequest($event->getRequest()); if ($response instanceof Response) { $event->setResponse($response); } } }); $dispatcher->addListener(ResponseReceivedEvent::class, function (ResponseReceivedEvent $event) { $controller = $event->getControllerInstance(); if ($controller instanceof OnResponseInterface) { $controller->onResponse($event->getResponse()); } }); }
/** * {@inheritdoc} */ public function register(EventDispatcher $dispatcher) { $dispatcher->addListener(RouteMatchedEvent::class, function (RouteMatchedEvent $event) { $annotation = $this->getTransactionalAnnotation($event->getRouteMatch()); if ($annotation) { $this->connection->setTransactionIsolation($annotation->getIsolationLevel()); $this->connection->beginTransaction(); } }); $dispatcher->addListener(ControllerInvocatedEvent::class, function (ControllerInvocatedEvent $event) { $annotation = $this->getTransactionalAnnotation($event->getRouteMatch()); if ($annotation) { if ($this->connection->isTransactionActive()) { $this->connection->rollback(); } } }); }
/** * {@inheritdoc} */ public function register(EventDispatcher $dispatcher) { $dispatcher->addListener(RouteMatchedEvent::class, function (RouteMatchedEvent $event) { $controller = $event->getRouteMatch()->getControllerReflection(); $request = $event->getRequest(); $secure = $this->hasControllerAnnotation($controller, Secure::class); if ($secure && !$request->isSecure()) { $url = preg_replace('/^http/', 'https', $request->getUrl()); throw new HttpRedirectException($url, 301); } }); $dispatcher->addListener(ResponseReceivedEvent::class, function (ResponseReceivedEvent $event) { $controller = $event->getRouteMatch()->getControllerReflection(); /** @var Secure|null $secure */ $secure = $this->getControllerAnnotation($controller, Secure::class); if ($secure && $secure->hsts !== null && $event->getRequest()->isSecure()) { $event->getResponse()->setHeader('Strict-Transport-Security', $secure->hsts); } }); }
/** * {@inheritdoc} */ public function register(EventDispatcher $dispatcher) { $dispatcher->addListener(RouteMatchedEvent::class, function (RouteMatchedEvent $event) { $controller = $event->getRouteMatch()->getControllerReflection(); $annotation = $this->getControllerAnnotation($controller, Allow::class); if ($annotation instanceof Allow) { $method = $event->getRequest()->getMethod(); $allowedMethods = $annotation->getMethods(); if (!in_array($method, $allowedMethods)) { throw new HttpMethodNotAllowedException($allowedMethods); } } }); }
/** * {@inheritdoc} */ public function register(EventDispatcher $dispatcher) { $dispatcher->addListener(ControllerReadyEvent::class, function (ControllerReadyEvent $event) { $event->addParameters($this->getParameters($event->getRequest(), $event->getRouteMatch()->getControllerReflection())); }); }
/** * {@inheritdoc} */ public function register(EventDispatcher $dispatcher) { $dispatcher->addListener(ControllerReadyEvent::class, function (ControllerReadyEvent $event) { $event->addParameters($this->getParameters($event)); }); }
public function testListenerReceivesParameters() { $event = 'test'; $parameters = ['a', 'b', 'c']; $dispatcher = new EventDispatcher(); $dispatcher->addListener($event, function () use($parameters) { $this->assertSame($parameters, func_get_args()); }); array_unshift($parameters, $event); call_user_func_array([$dispatcher, 'dispatch'], $parameters); }