示例#1
0
 /**
  * {@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());
     });
 }
示例#2
0
文件: benchmark.php 项目: brick/brick
 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);
     });
 }
示例#3
0
 /**
  * {@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());
         }
     });
 }
示例#4
0
 /**
  * {@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();
             }
         }
     });
 }
示例#5
0
 /**
  * {@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);
         }
     });
 }
示例#6
0
文件: AllowPlugin.php 项目: brick/app
 /**
  * {@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);
             }
         }
     });
 }
示例#7
0
 /**
  * {@inheritdoc}
  */
 public function register(EventDispatcher $dispatcher)
 {
     $dispatcher->addListener(ControllerReadyEvent::class, function (ControllerReadyEvent $event) {
         $event->addParameters($this->getParameters($event->getRequest(), $event->getRouteMatch()->getControllerReflection()));
     });
 }
示例#8
0
 /**
  * {@inheritdoc}
  */
 public function register(EventDispatcher $dispatcher)
 {
     $dispatcher->addListener(ControllerReadyEvent::class, function (ControllerReadyEvent $event) {
         $event->addParameters($this->getParameters($event));
     });
 }
示例#9
0
 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);
 }