示例#1
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);
     });
 }
示例#2
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());
     });
 }
示例#3
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);
             }
         }
     });
 }
示例#4
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());
         }
     });
 }
示例#5
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();
             }
         }
     });
 }
示例#6
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);
         }
     });
 }
示例#7
0
文件: Application.php 项目: brick/app
 /**
  * @param \Brick\Http\Request $request The request to handle.
  *
  * @return \Brick\Http\Response The generated response.
  *
  * @throws \Brick\Http\Exception\HttpException If a route throws such an exception, or no route matches the request.
  * @throws \UnexpectedValueException           If a route or controller returned an invalid value.
  */
 private function handleRequest(Request $request)
 {
     $event = new IncomingRequestEvent($request);
     $this->eventDispatcher->dispatch(IncomingRequestEvent::class, $event);
     $match = $this->route($request);
     $event = new RouteMatchedEvent($request, $match);
     $this->eventDispatcher->dispatch(RouteMatchedEvent::class, $event);
     $controllerReflection = $match->getControllerReflection();
     $instance = null;
     $this->valueResolver->setRequest($request);
     $this->valueResolver->setParameters($match->getClassParameters());
     if ($controllerReflection instanceof \ReflectionMethod) {
         $className = $controllerReflection->getDeclaringClass()->getName();
         $instance = $this->injector->instantiate($className);
         $callable = $controllerReflection->getClosure($instance);
     } elseif ($controllerReflection instanceof \ReflectionFunction) {
         $callable = $controllerReflection->getClosure();
     } else {
         throw new \UnexpectedValueException('Unknown controller reflection type.');
     }
     $event = new ControllerReadyEvent($request, $match, $instance);
     $event->addParameters($match->getFunctionParameters());
     $this->eventDispatcher->dispatch(ControllerReadyEvent::class, $event);
     $response = $event->getResponse();
     if ($response === null) {
         $this->valueResolver->setParameters($event->getParameters());
         try {
             $result = $this->injector->invoke($callable);
             if ($result instanceof Response) {
                 $response = $result;
             } else {
                 $event = new NonResponseResultEvent($request, $match, $instance, $result);
                 $this->eventDispatcher->dispatch(NonResponseResultEvent::class, $event);
                 $response = $event->getResponse();
                 if ($response === null) {
                     throw $this->invalidReturnValue('controller', Response::class, $result);
                 }
             }
         } catch (HttpException $e) {
             $response = $this->handleHttpException($e, $request);
         } finally {
             $event = new ControllerInvocatedEvent($request, $match, $instance);
             $this->eventDispatcher->dispatch(ControllerInvocatedEvent::class, $event);
         }
     }
     $event = new ResponseReceivedEvent($request, $response, $match, $instance);
     $this->eventDispatcher->dispatch(ResponseReceivedEvent::class, $event);
     return $response;
 }
示例#8
0
 /**
  * {@inheritdoc}
  */
 public function register(EventDispatcher $dispatcher)
 {
     $dispatcher->addListener(ControllerReadyEvent::class, function (ControllerReadyEvent $event) {
         $event->addParameters($this->getParameters($event->getRequest(), $event->getRouteMatch()->getControllerReflection()));
     });
 }
示例#9
0
 /**
  * {@inheritdoc}
  */
 public function register(EventDispatcher $dispatcher)
 {
     $dispatcher->addListener(ControllerReadyEvent::class, function (ControllerReadyEvent $event) {
         $event->addParameters($this->getParameters($event));
     });
 }
示例#10
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);
 }